9

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?

3
  • 1
    This works for me. Are you placing the code for AssemblyInfo.cs into the library project, or the main project?
    – matth
    Nov 13, 2013 at 17:49
  • 1
    At the library project. Nov 13, 2013 at 17:54
  • 1
    What's this line for? <add key="log4net.config" value="config.log4net"/>
    – stuartd
    Nov 14, 2013 at 11:21

3 Answers 3

23

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");
1
  • 2
    If the Console is a project with an interface ILogService and Library is a Log4Net implementation to the service declared in the Console application, as per the above solution, we have to add reference to Log4Net in Console application. Is it a best practice?
    – CSR
    Dec 9, 2017 at 3:32
4

Incase anyone is still looking at this and to add to the previous answers, you need to have instantiated a logger in your host application before you can log from the class library.

1
  • 1
    This is my problem. I am trying to execute a .dll class for an installation script. It needs to run BEFORE my main app runs. I think this is why it is not logging.
    – nikotromus
    Jul 19, 2018 at 15:44
3

If you look at the log4net documentation for assembly attributes it says this:

"Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. 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."

When the assembly attributes are defined in a class library - i.e. an external assembly - it gets tricky. Can you use log4net.Config.XmlConfigurator.Configure(path) instead?

1
  • 1
    I tried to use log4net.Config.XmlConfigurator.Configure() but then i received this error stack: log4net:ERROR Failed to find configuration section 'log4net' in the application's .config file. Nov 13, 2013 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.