views:

40

answers:

1

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.

A: 

I suggest to turn on internal debugging as explained here:

http://stackoverflow.com/questions/3121975/log4net-appenders-not-working-in-iis7-5/3126675#3126675

Stefan Egli
Thank you very much Stefan. That helped. Looking at the log4net debug messages I could see that the repository was not being configured as log4net could not find the configuration. The order in which the assemblies were loaded in the debug and release modes were different. Not sure if that was the issue here.
Deepu
I could see that log4net creates the default repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy] and then tries to configure the repository using the configuration if the assembly was decorated with the attribute [assembly: log4net.Config.XmlConfigurator(Watch = true)]. In case of the application built in Release mode, log4net could not find the configuration as the assembly loaded was not decorated with this attribute. Adding this attribute to the AssemblyInfo file solved the problem.
Deepu