Log4Net Config to Make Your Log4Net Loggin' Kickin' [Part 2]

So in my first Log4Net Config post a weee time back, I talked about a hot way to configure your log4net settings using a composite, rolling logging model.

Here, I have that same log4net config, but done in code! It doesn't have the same flexibility of the web.config or an xml file if you're frequently changing values--since you'd have to push code for any changes--but it does make any logic easier.

It begs the usage of preprocessor directives for setting File [location], PatternLayout, or Threshold.

log4net.Appender.RollingFileAppender a = 
new log4net.Appender.RollingFileAppender();
a.Name = "RollingFileAppender";
a.File = @"c:\blah.com\log.txt";
a.AppendToFile = true;
a.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Composite;
a.MaxSizeRollBackups = 14;
a.CountDirection = 1;
a.MaximumFileSize = "15000KB";
a.StaticLogFileName = true;
a.Layout = new log4net.Layout.PatternLayout(@"{%level}%date{MM/dd HH:mm:ss} - %message%newline");
a.ActivateOptions();
a.Threshold = log4net.Core.Level.Debug;
log4net.Config.BasicConfigurator.Configure(a);

(Thanks to my buddy J.A. for showing me this).

Enjoy!

kick it on DotNetKicks.com

Tags:

About Brian


profile for bluevoodoo1 on Stack Exchange, a network of free, community-driven Q&A sites

Brian Canzanella brings you nifty tips and tricks for most things .NET. read more...

Readers / Stuff