log4net doesn't log when running a .Net 4.0 Wi

2019-01-25 14:21发布

问题:

I have a .Net 4.0 windows service application that uses log4net.

Here's my log4net config.

<log4net>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <appender name="ColorConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
      <level value="ERROR" />
      <foreColor value="Red, HighIntensity" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <foreColor value="Red, HighIntensity" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <foreColor value="Blue, HighIntensity" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <forecolor value="White, HighIntensity" />
    </mapping>
    <mapping>
      <level value="DEBUG" />
      <forecolor value="Green, HighIntensity" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />
    <file value="Logs/Server.log" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
    <maximumFileSize value="10MB" />
    <rollingStyle value="Composite" />
    <staticLogFileName value="true" />
  </appender>

  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="ColorConsoleAppender" />
    <appender-ref ref="RollingFileAppender" />
  </root>

</log4net>

Logging works as expected when the service starts up while using the exe that was built in debug mode (log files are created at the right file path), but doesn't work when it is using the exe built in release mode.

I've tried using a recompiled log4net dll for .Net 4.0 following the steps mentioned at this blog.. http://tseonet.blogspot.com/2010/07/making-log4net-run-on-net-40.html.

But, that didn't work either.

Any help is greatly appreciated.

回答1:

I suggest to turn on internal debugging as explained here:

log4net - Appenders not working in IIS7.5



回答2:

Thanks Deepu

adding [assembly: log4net.Config.XmlConfigurator(Watch = true)] to the assemblyInfo file worked for me



回答3:

I stumbled upon the same issue today and after going through log4net docs did the following and it worked for me.

log4net.Config.XmlConfigurator.Configure();

Thought it will be worth sharing. Below is the logger class which uses log4net as provider

/// <summary>
/// Logger class using log4net library
/// </summary>
public class Log4NetLogger : ILogger
{
    public Log4NetLogger(Type configType)
    {
        log4net.Config.XmlConfigurator.Configure();
        this._logger = LogManager.GetLogger(configType);
    }

}

For me assembly attribute was not an option as I wrapped logging functionality into a separate assembly where log4net was one of the providers.