newtelligence poweredRSS 2.0
# Friday, December 18, 2009

I have been a big fan of log4net for the past several years. It’s a light weight, reliable, open source logging platform. Today I needed to create FileAppender that created a log file that had the current date in ISO 8601 format.

After playing around with various options for the RollingFileAppender, I finally figured out the correct way to accomplish my goal. By adding the type="log4net.Util.PatternString" to the file element in configuration file, you can change the name of the log file at runtime based on properties within log4net. It just so happens, that one of the properties is the current date and time.

Here is my appender that I ended up with:

<appender name="MyAppender" type="log4net.Appender.FileAppender">
            <file type="log4net.Util.PatternString" value="logs\%date{yyyyMMddTHHmmss}.txt" />
            <appendToFile value="true"/>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %-5level %logger - %message%newline"/>
            </layout>
</appender>

I had just never seen the type attribute in the file element in any of the samples I’ve worked with before, so it took me awhile to figure out the correct search terms to narrow things down. I also found a site that makes it a lot easier to search the user mailing list: http://old.nabble.com/Log4net---Users-f154.html

Friday, December 18, 2009 4:44:01 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Programming
Archive
<December 2009>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Copyright 2010
Adam Salvo
Sign In
Statistics
Total Posts: 234
This Year: 13
This Month: 1
This Week: 0
Comments: 34
Themes
Pick a theme:
All Content 2010, Adam Salvo
DasBlog theme 'Business' created by Christoph De Baene (delarou)