开发者

Log4Net not logging (probably not initialised)

开发者 https://www.devze.com 2023-03-29 06:25 出处:网络
First time playing with Log4Net and I\'m running into trouble.I\'ve followed various tutorials but I\'ve been unable to get it to log anything out.Let me show you my code and hopefully you can tell me

First time playing with Log4Net and I'm running into trouble. I've followed various tutorials but I've been unable to get it to log anything out. Let me show you my code and hopefully you can tell me what I'm doing wrong.

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" 
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>  
  <log4net>    
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="CurrentLog.txt"/>
      <staticLogFileName value="true"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date" />
      <datePattern value="yyyyMMdd"/>
      <filter type="log4net.Filter.LevelRangeFilter">
        <acceptOnMatch value="true" />
        开发者_StackOverflow中文版<levelMin value="DEBUG" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd MMM yyy HH:mm:ss} %level %message. %newline %exception" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</configuration>

AssemblyInfo.cs

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

Presenter.cs

At the top of the class I have this:

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Then I try to use the log variable later in the class:

bool isDebugEnabled = log.IsDebugEnabled;
log.Debug("Failed to save", e);

Whenever I inspect the isDebugEnabled variable, it is false, as are all of the other isBlahEnabled if I inspect the log variable.

My suspicion is that I have not hooked up my app.config file correctly because this is the first time I have tried to use one. I created by right clicking on the project in solution explorer, adding a new item, choosing Application Configuration File and naming it app.config.


This one works for me:

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <log4net>
      <appender name="Main" type="log4net.Appender.RollingFileAppender">
        <file value="${USERPROFILE}\My Documents\MyApp\Logs\Main.log" />
        <appendToFile value="false" />
        <maximumFileSize value="1GB" />
        <maxSizeRollBackups value="3" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date{ABSOLUTE} %-5level %-18logger %message%newline" />
        </layout>
      </appender>
      <root>
        <level value="DEBUG" />
        <appender-ref ref="Main" />
      </root>
    </log4net>
</configuration>

Also be sure the build action on app.config is set to None and Copy to output dir is set to "Copy if newer". You can set these settings in the file properties.

Program.cs

public static ILog Log;

static void Main(string[] args)
{
    // Setup Logging
    log4net.Config.XmlConfigurator.Configure();
    Log = LogManager.GetLogger("MyAwesomeApp");

    // ...
}


Just to add my 2 cents: I had the same Problem, but I use the above config in separate log4net.config (so I have on all apps the same config), but I simply forgot to set the file property on build 'Copy when newer' (or similar wording, have German Version).


Just to add my 2p in case this helps anyone else searching for a resolution to this problem:

In my case I was using the NServicebus.Logging package in addition to Log4net, and this package contains types with the exact same names as those in Log4net, differing only in the namespace.

Fixing the 'using' statements or fully-qualifying the type names resolved it, but it was very hard to spot the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号