Skip to content

Instantly share code, notes, and snippets.

@adilsoncarvalho
Last active May 27, 2016 01:30
Show Gist options
  • Save adilsoncarvalho/3958594 to your computer and use it in GitHub Desktop.
Save adilsoncarvalho/3958594 to your computer and use it in GitHub Desktop.
Simple Log4Net Configuration File

Level priority is defined as:

ALL --> DEBUG --> INFO --> WARN --> ERROR --> FATAL --> OFF

It means that if you choose INFO you will get all INFO WARN ERROR and FATAL

It means that if you choose ERROR you will get all ERROR and FATAL

Got it? :)

Appenders

ColoredConsoleAppender

Result:

screen shot on 2016-05-26 at 10-02-59

Using with NUnit/MSTest

Go see this documentation on which you can see how to capture the output of the logging to the test result.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<log4net>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="FATAL" />
<foreColor value="White, HighIntensity" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<mapping>
<level value="Info" />
<foreColor value="Green" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="White" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
</log4net>
</configuration>
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log\choose-your-name-wisely.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="1000" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
using System.Reflection;
using System.Runtime.InteropServices;
//
// C# yada yada yada
//
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
using System;
using log4net;
public class MyClass
{
protected ILog logger = LogManager.GetLogger(typeof(MyClass));
// ... life goes on
public bool MyMethod()
{
try {
// something bad happened!!! :(
}
catch (Exception e)
{
logger.error("Something really bad happened", e);
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment