在其他地方读取Log4net的数据库连接

好多时候,在lognet中再写一次连接都是重复的,通过下面的方法可以读取在配置文件其它位置声明的数据库连接

None.gif using  System;
None.gif
using  log4net;
None.gif
using  log4net.Config;
None.gif
using  System.Configuration;
None.gif
using  System.Collections.Specialized;
None.gif
using  System.IO;
None.gif
namespace  EtpService.Core
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Log4net 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public sealed class Log4net
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        
//public static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
InBlock.gif        
//public static readonly ILog log = LogManager.GetLogger("EtpService.Core.AccountManager");
InBlock.gif
        
InBlock.gif        
InBlock.gif        
public Log4net()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 不好不知道怎么封装
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="loggerName"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public ILog GetLog(string loggerName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ILog log 
= LogManager.GetLogger(loggerName);    
InBlock.gif            
return log;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 初始化Log4net的配置文件,最好在程序开始时只运行一次
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="fileName">配置文件名:</param>

InBlock.gif        public static void Config(string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DOMConfigurator.Configure(
new FileInfo(fileName));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 通过Section和索引取得配置中的值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="section"></param>
InBlock.gif        
/// <param name="index"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static string GetConfig(string section,int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NameValueCollection nameValueCollection 
= new NameValueCollection();
InBlock.gif            nameValueCollection 
= (NameValueCollection)ConfigurationSettings.GetConfig(section);
InBlock.gif            
return nameValueCollection[index];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 配置连接字符串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="conString"></param>
InBlock.gif        
/// <param name="loggerName"></param>
ExpandedSubBlockEnd.gif        
/// <param name="appenderName"></param>

InBlock.gif        public static void ConfigConnection(string conString,string loggerName,string appenderName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{  
InBlock.gif                 log4net.Repository.Hierarchy.Hierarchy h 
= LogManager.GetLoggerRepository() as log4net.Repository.Hierarchy.Hierarchy;  
InBlock.gif                
if(h != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    log4net.Appender.AdoNetAppender adoAppender 
= (log4net.Appender.AdoNetAppender)h.GetLogger(loggerName,            
InBlock.gif                        h.LoggerFactory).GetAppender(appenderName);
InBlock.gif                    
if(adoAppender !=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        adoAppender.ConnectionString 
= conString;  
InBlock.gif                        adoAppender.ActivateOptions();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(NullReferenceException nfe) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{  
InBlock.gif                
throw new NullReferenceException("配置文件可能有错误", nfe);  
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

调用代码:
None.gif private   static   readonly  ILog log  =  LogManager.GetLogger( typeof (AccountManager));
None.gif
string  conString  =  Log4net.GetConfig( " nhibernate " , 3 );
None.gif            Log4net.ConfigConnection(conString,
" EtpService.Core.AccountManager " , " ADONetAppender " );
None.gif没有用上面的类生成Log,不知道怎么实现 类似static 
readonly  的功能

配置文件:
None.gif < configSections >
None.gif        
< section  name ="nhibernate"  type ="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"   />
None.gif        
< section  name ="log4net"  type ="log4net.Config.Log4NetConfigurationSectionHandler,log4net"   />
None.gif        
< section  name ="etp"  type ="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"   />
None.gif    
</ configSections >
None.gif
< nhibernate >
None.gif        
< add  key ="hibernate.connection.provider"  value ="NHibernate.Connection.DriverConnectionProvider"   />
None.gif        
< add  key ="hibernate.dialect"  value ="NHibernate.Dialect.MsSql2000Dialect"   />
None.gif        
< add  key ="hibernate.connection.driver_class"  value ="NHibernate.Driver.SqlClientDriver"   />
None.gif        
< add  key ="hibernate.connection.connection_string"  value ="Password=sa;uid=sa;Initial Catalog=Etp;Data Source=dinghao"   />
None.gif    
</ nhibernate >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值