Log4net
  1. Log4net
  2. LOG4NET-274

log4net doesn't log when running a .Net 4.0 Windows application built in Release mode

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 1.2.10
    • Fix Version/s: 1.2.11
    • Component/s: None
    • Labels:
      None
    • Environment:
      Visual Studio 2010, C#, Windows 7

      Description

      I've 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 even 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.
      That didn't work either. I was able to compile and run the service. But the result was the same; no logs when using the exe build in release mode.

      Not sure what's going on. Cannot deploy the application built in debug mode into production.

      Any help is greatly appreciated.

        Activity

        Hide
        Stefan Bodewig added a comment -

        Added an FAQ entry in svn revision 1178832

        Show
        Stefan Bodewig added a comment - Added an FAQ entry in svn revision 1178832
        Hide
        Stefan Bodewig added a comment -

        It likely really comes down to DLL load order and which one tries to use the logging system first.

        If you can make sure you really get the very first access by using something like Application_Start in ASP.NET or OnStart in your ServiceBase subclass you should be able to only use the attribute in the assembly that contains the access (the web application or your service host assembly).

        I'll update the docs to state that DLL load order may be different in DEBUG and RELEASE configurations and thus sometimes you need to search a little before you find the assembly that has to contain the attribute and that it may be different in different configurations.

        Show
        Stefan Bodewig added a comment - It likely really comes down to DLL load order and which one tries to use the logging system first. If you can make sure you really get the very first access by using something like Application_Start in ASP.NET or OnStart in your ServiceBase subclass you should be able to only use the attribute in the assembly that contains the access (the web application or your service host assembly). I'll update the docs to state that DLL load order may be different in DEBUG and RELEASE configurations and thus sometimes you need to search a little before you find the assembly that has to contain the attribute and that it may be different in different configurations.
        Hide
        Frank van de Laar added a comment -

        I ran into the same issue with an ASP.NET 4.0 application. I noticed the log4net documentation about the XmlConfigurator stated: "Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked."
        So I included a simple call "log.Info" in my Application_Start event (in global.asax). This solved the problem, without changing all referenced assemblies.
        Hope this helps...

        Show
        Frank van de Laar added a comment - I ran into the same issue with an ASP.NET 4.0 application. I noticed the log4net documentation about the XmlConfigurator stated: "Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked." So I included a simple call "log.Info" in my Application_Start event (in global.asax). This solved the problem, without changing all referenced assemblies. Hope this helps...
        Hide
        Petr Felzmann added a comment -

        I always used [assembly: XmlConfigurator(Watch = true)] in my exe assembly. But in my case I used also reference to another dll assembly which also use log4net to log. And I had to add this attribute also to this referenced assembly. After adding the attribute, log4net started to log again. Thanks Deepu!

        BTW You can reproduce it also for simple console app, windows service is not needed. Does anybody know why [assembly: XmlConfigurator(Watch = true)] attribute is now needed for every referenced assemblies for application built in .NET 4.0 Release mode to have log4net working?

        Show
        Petr Felzmann added a comment - I always used [assembly: XmlConfigurator(Watch = true)] in my exe assembly. But in my case I used also reference to another dll assembly which also use log4net to log. And I had to add this attribute also to this referenced assembly. After adding the attribute, log4net started to log again. Thanks Deepu! BTW You can reproduce it also for simple console app, windows service is not needed. Does anybody know why [assembly: XmlConfigurator(Watch = true)] attribute is now needed for every referenced assemblies for application built in .NET 4.0 Release mode to have log4net working?
        Hide
        Deepu Thomas Palathara added a comment -

        I suggest decorating your executing assembly with [assembly: log4net.Config.XmlConfigurator(Watch = true)].

        Check this out..

        http://stackoverflow.com/questions/4045203/log4net-doesnt-log-when-running-a-net-4-0-windows-application-built-in-release

        Show
        Deepu Thomas Palathara added a comment - I suggest decorating your executing assembly with [assembly: log4net.Config.XmlConfigurator(Watch = true)] . Check this out.. http://stackoverflow.com/questions/4045203/log4net-doesnt-log-when-running-a-net-4-0-windows-application-built-in-release
        Hide
        Ron Grabowski added a comment -

        Are you sure it isn't just that certain appenders aren't working? Does MemoryAppender:

        http://logging.apache.org/log4net/release/sdk/log4net.Appender.MemoryAppender.html

        work?

        Show
        Ron Grabowski added a comment - Are you sure it isn't just that certain appenders aren't working? Does MemoryAppender: http://logging.apache.org/log4net/release/sdk/log4net.Appender.MemoryAppender.html work?
        Hide
        Petr Felzmann added a comment -

        Sorry, I have to apologize.

        I double checked the solution from my previous comment and it is not working So the issue remains: log4net is not logging in windows service compiled for .NET FW 4.0 in Release mode.

        And Blocker priority is also the right one IMHO.

        Show
        Petr Felzmann added a comment - Sorry, I have to apologize. I double checked the solution from my previous comment and it is not working So the issue remains: log4net is not logging in windows service compiled for .NET FW 4.0 in Release mode. And Blocker priority is also the right one IMHO.
        Hide
        Petr Felzmann added a comment -

        I found the solution: the output type of your project must be Console Application instead of Windows Application. When you change this in your project settings then your application starts logging also after compilation in Release mode. And it is not related to whether the exe runs as windows service or not. You are able to reproduce this behavior with console app as well.

        I tried to put assembly attribute to the log4net code

        [assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]

        and recompile it with .NET FW 4.0, but this didn't help. So the only solution is to use Console Application as output type of your project. But still I don't have idea why the behavior is like this and what has changed in .NET FW 4.0 to cause it.

        So now the priority of this issue definitely isn't Blocker

        Show
        Petr Felzmann added a comment - I found the solution: the output type of your project must be Console Application instead of Windows Application. When you change this in your project settings then your application starts logging also after compilation in Release mode. And it is not related to whether the exe runs as windows service or not. You are able to reproduce this behavior with console app as well. I tried to put assembly attribute to the log4net code [assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)] and recompile it with .NET FW 4.0, but this didn't help. So the only solution is to use Console Application as output type of your project. But still I don't have idea why the behavior is like this and what has changed in .NET FW 4.0 to cause it. So now the priority of this issue definitely isn't Blocker
        Hide
        Petr Felzmann added a comment -

        I have the same problem and can reproduce it on Win7 as well as on Win2k3SP2: logging doesn't work for windows service compiled in Release mode in .NET FW 4.0. When I turn on logging of log4net itself then I get only those few lines in the log file:

        log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:\temp\log4net_test\log4net.dll]. (.NET Runtime [4.0.30319.1] on Microsoft Windows NT 5.2.3790 Service Pack 2)
        log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
        log4net: DefaultRepositorySelector: Creating repository for assembly [MyTestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
        log4net: DefaultRepositorySelector: Assembly [MyTestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\temp\log4net_test\MyTestLib.dll]
        log4net: DefaultRepositorySelector: Assembly [MyTestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
        log4net: DefaultRepositorySelector: Assembly [MyTestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
        log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]

        nothing more after this. When I stop the windows service the one more line in the log appears

        log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository]

        When I use common console application then logging works fine.

        Any guess what could be wrong?

        Show
        Petr Felzmann added a comment - I have the same problem and can reproduce it on Win7 as well as on Win2k3SP2: logging doesn't work for windows service compiled in Release mode in .NET FW 4.0. When I turn on logging of log4net itself then I get only those few lines in the log file: log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821] . Loaded from [C:\temp\log4net_test\log4net.dll] . (.NET Runtime [4.0.30319.1] on Microsoft Windows NT 5.2.3790 Service Pack 2) log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy] log4net: DefaultRepositorySelector: Creating repository for assembly [MyTestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] log4net: DefaultRepositorySelector: Assembly [MyTestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\temp\log4net_test\MyTestLib.dll] log4net: DefaultRepositorySelector: Assembly [MyTestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified. log4net: DefaultRepositorySelector: Assembly [MyTestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy] log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy] nothing more after this. When I stop the windows service the one more line in the log appears log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository] When I use common console application then logging works fine. Any guess what could be wrong?

          People

          • Assignee:
            Unassigned
            Reporter:
            Deepu Thomas Palathara
          • Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development