对配置数据的log4net使用应用程序配置文件 [英] Have log4net use application config file for configuration data

查看:543
本文介绍了对配置数据的log4net使用应用程序配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存在我的application.config文件log4net的配置数据。根据我的文档的理解,我做了以下内容:




  1. 添加引用log4net.dll


  2. 在添加了的AssemblyInfo.cs以下行:

      [汇编:log4net的.Config.XmlConfigurator(手表= TRUE)] 


  3. 初始化记录如下:

     私有静态只读的ILog日志= LogManager.GetLogger(typeof运算(frmWizard)); 


  4. 我在我的app.config下面的代码:




<预类=郎咸平的XML prettyprint-覆盖> < configSections>
<节名称=log4net的TYPE =log4net.Config.Log4NetConfigurationSectionHandler,log4net的/>
< / configSections>
<&log4net的GT;
<追加程序名称=EventLogAppenderTYPE =log4net.Appender.EventLogAppender>
<布局类型=log4net.Layout.PatternLayout>
< conversionPattern值=%DATE [%线程]%-5level%记录[%产权{} NDC - %讯息%换行/>
< /布局>
< /附加器>
<根和GT;
<电平值=INFO/>
<附加目的地-REF REF =是ConsoleAppender/>
< /根>
< / log4net的>



然而,当我运行应用程序,我得到的控制台上出现以下错误:




没有名为[是ConsoleAppender]追加程序可以找到。




我怎样才能得到log4net的读取配置文件的设置?



谢谢!


解决方案

行添加到您的app.config在configSections元素



<预类=郎咸平的XML prettyprint-覆盖> < ; configSections>
<节名称=log4net的
型=log4net.Config.Log4NetConfigurationSectionHandler,log4net的,版本= 1.2.10.0,
区域性=中性公钥= 1b44e1d426115821/>
< / configSections>



再后来添加log4net的部分,而是委托给实际的log4net的配置文件别处...



<预类=郎咸平的XML prettyprint-覆盖> < log4net的configSource =Config\Log4Net.config/>

在您的应用程序代码中,当您创建日志,写

 私有静态的ILog GetLog(字符串LOGNAME)
{
ILog的日志= LogManager.GetLogger(LOGNAME);
返回日志;
}


I would like to store log4net config data in my application.config file. Based on my understanding of the documentation, I did the following:

  1. Add a reference to log4net.dll

  2. Add the following line in AssemblyInfo.cs:

    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    

  3. Initialize the logger as follows:

    private static readonly ILog log = LogManager.GetLogger(typeof(frmWizard));
    

  4. I have the following code in my app.config:

    <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
      <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
      </appender>
      <root>
        <level value="INFO" />
        <appender-ref ref="ConsoleAppender" />
      </root>
    </log4net>

However, when I run the application, I get the following error on the console:

No appender named [Consoleappender] could be found.

How can I get log4net to read settings from the config file?

Thanks!

解决方案

Add a line to your app.config in the configSections element

<configSections>
 <section name="log4net" 
   type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, 
         Culture=neutral, PublicKeyToken=1b44e1d426115821" />
</configSections>

Then later add the log4Net section, but delegate to the actual log4Net config file elsewhere...

<log4net configSource="Config\Log4Net.config" />

In your application code, when you create the log, write

private static ILog GetLog(string logName)
{
    ILog log = LogManager.GetLogger(logName);
    return log;
}

这篇关于对配置数据的log4net使用应用程序配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆