5

I use log4net in my webapi project (using autofac and owin). I added this to my controller

LogManager.GetLogger(typeof(NotificationController));

But I have seen that there is no appender loaded. In my config I have this

 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
...    
<log4net>
        <root>
          <level value="ALL" />
          <appender-ref ref="aiAppender" />
          <appender-ref ref="TraceAppender" />
          <appender-ref ref="FileAppender" />
        </root>
        <appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message%newline" />
          </layout>
        </appender>
        <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />              
          </layout>
        </appender>
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
          <file value="log\server.log" />
          <appendToFile value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
          </layout>
        </appender>
      </log4net>

I have no build errors and my code is running without error if I call this

logger.Fatal("Error");

But I have no logs.

UPDATE

Added this to startup class but doesn't change

public class Startup
{
    private static readonly ILog logger = LogManager.GetLogger(typeof(Startup));

    public void Configuration(IAppBuilder app)
    { 
        HttpConfiguration config = new HttpConfiguration();

        logger.Info("Start application");
Community
  • 1
  • 1
cpiock
  • 1,275
  • 2
  • 17
  • 44

1 Answers1

9

Log4Net has to be told explicitly that you want it to read the XML configuration from App.config.

See: log4net only works when XmlConfigurator.Configure() is called

Either call the static method XmlConfigurator.Configure() in your application startup code, or add the following to AssemblyInfo.cs for all projects in the solution:

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

You may also find that you have to call LogManager.GetLogger() immediately after startup - it seems to make a difference even if the logger instance is not used for anything.

Alistair Green
  • 462
  • 3
  • 10
  • 1
    You must call GetLogger on startup when using assembly attributes, as per [the docs](https://logging.apache.org/log4net/release/manual/configuration.html#attributes) - _"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."**_ – stuartd Feb 28 '17 at 11:06