Skip to main content
 首页 » 编程设计

log4net 不会从 app.config 读取

2024年11月24日18myhome

我为 log4net 配置了两个相同的项目。一个项目记录良好;但是,另一个根本不记录。
Logger在不记录的项目中返回 IsFatalEnabled = false , IsErrorEnabled = false , IsWarnEnabled = false , IsInforEnabled = falseIsDebugEnabled = false .

我已经从一个项目复制并粘贴到另一个项目,完全替换了文件并尝试删除所有空格。

什么可能导致一个项目无法正确读取 app.config 中的正确级别?

app.config:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
  <configSections> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 
  </configSections> 
  <log4net> 
    <appender name="FileAppender" type="log4net.Appender.FileAppender"> 
      <file value="logfile.txt" /> 
      <appendToFile value="true" /> 
      <layout type="log4net.Layout.PatternLayout"> 
        <conversionPattern value="%date: %-5level – %message%newline" /> 
      </layout> 
    </appender> 
    <root> 
      <level value="DEBUG" /> 
      <appender-ref ref="FileAppender" /> 
    </root> 
  </log4net> 
</configuration> 

程序.cs

using log4net; 
 
class Program 
{ 
    private static readonly ILog Log = LogManager.GetLogger("SO"); 
 
    static void Main(string[] args) 
    { 
        Log.Info("SO starting"); 
    } 
} 

请您参考如下方法:

app.config 文件似乎没有配置为由 log4net 监视。

我将以下行添加到 AssemblyInfo.cs现在启用日志记录:

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

奇怪,因为我没有将此行添加到正在运行的项目中。

编辑:

看起来正在工作的项目毕竟确实有这条线。我一定忘记了。
// This will cause log4net to look for a configuration file  
// called [ThisApp].exe.config in the application base  
// directory (i.e. the directory containing [ThisApp].exe)  
// The config file will be watched for changes.  
[assembly: log4net.Config.XmlConfigurator(Watch = true)]