dinghao

记录成长点滴

 

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

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

using System;
using log4net;
using log4net.Config;
using System.Configuration;
using System.Collections.Specialized;
using System.IO;
namespace EtpService.Core
{
    
/// <summary>
    
/// Log4net 的摘要说明。
    
/// </summary>

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

        
        
/// <summary>
        
/// 不好不知道怎么封装
        
/// </summary>
        
/// <param name="loggerName"></param>
        
/// <returns></returns>

        public ILog GetLog(string loggerName)
        
{
            ILog log 
= LogManager.GetLogger(loggerName);    
            
return log;
        }


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

        public static void Config(string fileName)
        
{
            DOMConfigurator.Configure(
new FileInfo(fileName));
        }


        
/// <summary>
        
/// 通过Section和索引取得配置中的值
        
/// </summary>
        
/// <param name="section"></param>
        
/// <param name="index"></param>
        
/// <returns></returns>

        public static string GetConfig(string section,int index)
        
{
            NameValueCollection nameValueCollection 
= new NameValueCollection();
            nameValueCollection 
= (NameValueCollection)ConfigurationSettings.GetConfig(section);
            
return nameValueCollection[index];
        }

        
        
        
/// <summary>
        
/// 配置连接字符串
        
/// </summary>
        
/// <param name="conString"></param>
        
/// <param name="loggerName"></param>
        
/// <param name="appenderName"></param>

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

                }

                
            }

            
catch(NullReferenceException nfe) 
            
{  
                
throw new NullReferenceException("配置文件可能有错误", nfe);  
            }
 

        }

    
    }

}


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

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

posted on 2006-01-12 16:08  思无邪  阅读(1097)  评论(0编辑  收藏  举报

导航