Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net appenders always empty

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");
like image 851
cpiock Avatar asked Feb 27 '17 21:02

cpiock


1 Answers

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.

like image 113
Alistair Green Avatar answered Nov 14 '22 20:11

Alistair Green