Log4Net Config to Make Your Log4Net Loggin' Rockin'

Tired of seeing 10,343 error emails when your application blows up? So you finally took a hint and got around to logging your web application. Good work, you're another step closer to being a rock and roll geek dude.

Log4Net is a great choice for all of your logging needs. I'll assume that you have log4net correctly installed and it's giving you some sort of output. If not, the mighty Phil Haack has written about configuring log4net numerous times. Rock your Loggin'

I have two Log4Net xml config files that I switch between using debug and release preprocessor directives (Too lazy to change one for releases [They're virtually the same except for priority value]). Having the log4net config outside of web.config won't make the app restart if you need to make a change to it while it's in the wild (hot) (though won't show updates until app restart).

There are a bunch of log4net example configs available for download on the Log4Net website. I have included here the log4net config file that I am using on my latest project.

The Reasons:

Here is why I like the following log4net setup:

  • The current log keeps its static name of "log-file.txt." Paired with something like Log4Net Viewer or baretail (my preference) and the scrolling appender option, it is WICKED easy to watch the logging action as it happens in real time.
  • It will create a new log for each day or if the max size is reached (I have it set to 15mb here and 14 logs).
  • Logs are named in such a way that viewing past logs is easy... log-file.txtYYYYMMDD is what I have here where YYYYMMDD will be replaced by the actual year, month, day. (In the case where you have might have multiple logs per day, a number will be appended to the file name log-file.txtYYYYMMDD.1, log-file.txtYYYYMMDD.2, etc.).
  • What is sexier than logging?

On to the goodies...

The Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<root>
<priority value="Info"/>
<appender-ref ref="RollingFileAppender"/>
</root>

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\Path\\To\\Your\\log-file.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="14" />
<maximumFileSize value="15000KB" />
<datePattern value="yyyyMMdd" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="{%level}%date{MM/dd HH:mm:ss} - %message%newline"/>
</layout>
</appender>
</log4net>
</configuration>

The End:

In closing, I hope you have some sort of logging strategy for your application, using my log4net config or not, it will lead you even closer to being rocktaclar, and that is veddie nice.

Thumbs Up!
I like the log! Niiiiice.

kick it on DotNetKicks.com

How Do I configure various levels?

How Do I configure an smtp appender to only show errors (1 per email), console appender to show all, and file appender to only show warnings (& above) ?

Submitted by Anonymous (not verified) on Mon, 12/05/2011 - 6:55pm.
very good program.

.NET and C# is the very good language. they are more user friendly than the other languages.the program log4net example configs is the very good for us to get relief from error message. the program code that you share on the page is very good.its an accurate code done by you.thanks for your support.

Submitted by alext smith (not verified) on Fri, 04/01/2011 - 10:27am.
This log4net is very helpful

this log4net is very easy to use. I like it too :)

Submitted by Balo (not verified) on Wed, 03/30/2011 - 2:54pm.
Path of log file

This is really good.I have used same way but the log file is not creating in the specified path.Is there any way to specify?
where is the problem to solve..I am using Visualstudio2008 ,C# windows application,.Net 3.5..Let me know if anybody knows on this..

Submitted by Anonymous (not verified) on Thu, 06/24/2010 - 10:26am.
brian's picture
preprocessor...

If you check out this post... http://www.csharptocsharp.com/log4net-configuration-for-rockin-loggin-pa...

you'll see that you can just wrap the File property with preprocessor directives for any release configurations you might have.

Submitted by brian on Fri, 01/08/2010 - 1:31pm.
debug and release preprocessor directives

Hi,
can u provide some examples of how u automatically change the log4net.xml file whenever RELEASE or DEBUG is the active configuration?

BTW Thx for this cool article

Submitted by Anonymous (not verified) on Mon, 01/04/2010 - 10:06am.
Very good post, thanks a

Very good post, thanks a lot.

Submitted by dido (not verified) on Tue, 12/29/2009 - 10:43am.
Log in XML File

I am not able to log in XML file. The following message displayed when I open the XML file:

Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------
The operation completed successfully. Error processing resource 'file:///C:/AppLog3.xml'. Line 1, Position 363

I would really appreciate it if you can help me to resolve this issue.

Thanks.

Submitted by Anonymous (not verified) on Thu, 09/25/2008 - 7:52pm.
brian's picture
UDP

Yes, the udp appender is pretty hot! I've fooled with it a bit. A log4net Color Console for ASP.Net is a great guide/post for it.

Submitted by brian on Thu, 01/17/2008 - 5:50am.
brian's picture
!!

Thanks for the tip!

Submitted by brian on Thu, 01/17/2008 - 5:47am.
Oops, I mean ${TEMP}

Oops, you have to specify the variables like ${TEMP}, not %TEMP%.

Submitted by Anonymous on Tue, 01/15/2008 - 10:25pm.
Environment Variables

The only thing I would add is that you can use %-delimited environment variables in the log file-name path. This is necessary, because you can seldom put a log file in an arbitrary spot (unless your users run as admin). I like %TEMP% myself.

Submitted by Anonymous on Fri, 01/11/2008 - 7:42pm.
I can't believe I ever used Entlib

In 1.1, I used Traces, then in 2 I used Entlib - which was a huge mistake. I still don't understand how the conf for entlib logging works.

log4net was a breathe of fresh air, and it's so easy to customize. Not only that, without much effort, you can log into a in-memory appender and read it at your leisure.

I dig the UDP appender too.

Submitted by Anonymous on Fri, 01/11/2008 - 1:39am.

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