Log4Net does not log from a class library

Vinicius Seganfredo picture Vinicius Seganfredo · Nov 13, 2013 · Viewed 11.8k times · Source

I am working on .NET Framework 4.0 using C# in Windows 7, and trying to log from a class library but it's not working. I'm running my application without errors, but also nothing happens to my log file, neither to my console.

So, here is my code:

This is my App.config file:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>


  <add key="log4net.config" value="config.log4net"/>

  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG"/>
        <levelMax value="FATAL"/>
      </filter>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="MyLogFile.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="5"/>
      <maximumFileSize value="10MB"/>
      <staticLogFileName value="true"/>
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="debug"/>
      </filter>
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="error"/>
      </filter>
      <filter type="log4net.Filter.DenyAllFilter"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <logger name="MyApplication">
      <level value="DEBUG"/>
      <appender-ref ref="ConsoleAppender"/>
      <appender-ref ref="RollingFileAppender"/>
    </logger>
  </log4net>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

</configuration>

This is what I put into my AssemblyInfo.cs:

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

This what is at the file i'm trying to log:

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


log.Debug("Testing");

When i run my program nothing happens. Someone knows why?

Answer

matth picture matth · Nov 13, 2013

Similar to this answer, both the app.config, log4net configuration and the AssemblyInfo.cs configurator needs to be placed in the host application.

Lets say I have project Console as a console project that I will run, and Library as a Library project. Console will need to have an app.config file with the above log4net configuration, and the AssemblyInfo.cs will need the XmlConfigurator code inside of it. Library does not need either of these, and will work by using the LogManager call.

Console                 > This is the project that you will run.
    Properties
        AssemblyInfo.cs > Place [assembly: log4net.Config.XmlConfigurator(Watch = true)] here.
    app.config          > Place log4net XML configuration here.
    Program.cs          > Start of application.

Library
    Properties
        AssemblyInfo.cs > Leave this file alone.
    Foo.cs              > Create the private readonly property.
        SomeMethod()    > log.Debug("Test");