Skip to content

Instantly share code, notes, and snippets.

@bobbychopra
Created June 8, 2012 17:42
Show Gist options
  • Save bobbychopra/2897163 to your computer and use it in GitHub Desktop.
Save bobbychopra/2897163 to your computer and use it in GitHub Desktop.
Log4Net Configuration in WPF
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="SmtpAppender"/>
<appender-ref ref="LogFileAppender"/>
<appender-ref ref="ColoredConsoleAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\tagfileuploader-log.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="[email protected]"/>
<from value="[email protected]"/>
<subject value="TagFileUploader ERROR"/>
<smtpHost value="prc-mn-ex01"/>
<bufferSize value="512"/>
<lossy value="true"/>
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
</log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
using System.Windows;
using log4net;
namespace Namespace
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected override void OnStartup(StartupEventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
//Log.Info("Hello World");
base.OnStartup(e);
}
}
}
@kmorcinek
Copy link

Thx, I forget that XmlConfigurator.Configure(); is needed.

@Tarren
Copy link

Tarren commented Feb 29, 2016

Thanks for this! I have a mental block around log4net.Config.XmlConfigurator.Configure();

@nasrinbanus-vg
Copy link

For me the log is writing in to the file

@srivatshan
Copy link

Thanks for this.I got output.

@westside
Copy link

The order is important, section should be before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment