Log4net


Adding log4net

First, start by adding Log4net by downloading it with the nuget inside Visual Studio.

 

In the class where you want to log, add this row to a class:

 private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(WebApiApplication));

 

In web.config add following in the <configuration> section:

<log4net>
    <appender name="MyAppLogAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:/logs/mylogfile.txt" />
      <bufferSize value="1" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <datePattern value="yyyyMMdd" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="1MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="MyAppLogAppender" />
    </root>
  </log4net>

and also inside web.config in the <configSections> section

 

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

In Global.asax.cs you will need to add following to initalize. If you miss this, the logs will not write to the files!

protected void Application_Start()
{
     log4net.Config.XmlConfigurator.Configure();
}

 

 Trubleshooting log4net errors

When you run into problems with log4net, like the log file is not created or such, you can add the following config to web.config in order to get log messages of what log4net does while initalizing and if there is problems accessing files.

<system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add
            name="textWriterTraceListener"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="C:\temp\log4net.txt" />
      </listeners>
    </trace>
</system.diagnostics>

 

Exampel of several log files with different log levels

 This is another example of how you may use different files to log various levels with log4net.

<log4net debug="true">
      <appender name="MyAppDebugAndInfoAppender" type="log4net.Appender.RollingFileAppender">
        <file value="C:/logs/MyAppDebugAndInfoAppenderLog.txt" />
        <evaluator type="log4net.Core.LevelEvaluator">
          <threshold value="INFO"/>
        </evaluator>
        <filter type="log4net.Filter.LevelRangeFilter">
          <levelMin value="INFO" />
          <acceptOnMatch value="true" />
        </filter>
        <filter type="log4net.Filter.DenyAllFilter" />
        <bufferSize value="1"/>
        <appendToFile value="true"/>
        <rollingStyle value="Composite"/>
        <datePattern value="yyyyMMdd"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="1MB"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
        </layout>
    </appender>
    <appender name="MyAppErrorAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:/logs/MyAppErrorAppenderLog.txt" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="ERROR"/>
      </evaluator>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <acceptOnMatch value="true" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
      <bufferSize value="1"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="yyyyMMdd"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
      </layout>
    </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="MyAppDebugAndInfoAppender"  />
            <appender-ref ref="MyAppErrorAppender" />
        </root>
  </log4net>

Published: 2016-08-25