Thursday, December 11, 2008

Log4Net ConversionPattern Strings

Several months ago we began to transition from an in-house logging framework to Log4Net in our primary web applications. For the last couple of days I have been trying to troubleshoot some production problems in the two of these applications and I noticed that my Log4Net logs were using a 12hr clock for the timestamp, and didn't include an AM/PM indicator. It also used old style parameter names rather than the newer verbose parameters.

Seeing as how I tend to cut and paste code for things like that, and I know other programmers do as well, I figured I would blog about what I am switching to, as well as a couple of other options. In case you are not really familiar with log4net, this setting is changed by editing the value string for the ConversionPattern param in the - log4net --appender - section of the web or app config file:

<param name="ConversionPattern" value="your string here"/>

The old:
value="%-5p %d{yyyy-MM-dd hh:mm:ss} - %m%n"
Results in:
ERROR 2008-12-03 01:28:38 - login.aspx submitted for 7234

Besides the timestamp problem, the format string is difficult to understand. It is not at all obvious what 'p', 'd', 'm' and 'n' stand for. Well, maybe the fact that 'd' is followed by a date format string you could guess that it stood for Date.

The following link lists both the shortcut and the verbose fomat values from the Apache log4net documentation.

The one I am switching to:
value="%-5level %date{yyyy-MM-dd HH:mm:ss} - %message%newline">

Note the more obvious setting names. This value string results in:
INFO 2008-12-11 14:06:59 - login.aspx submitted for 7234

Other choices:
value="%-5level %date{G} - %message%newline">
Results in
INFO 12/11/2008 2:15:07 PM - login.aspx submitted for 7234

value="%-5level %date{yyyy-MM-dd hh:mm:ss t} - %message%newline">
Results in
INFO 2008-12-11 02:06:59 P - login.aspx submitted for 7234

value="%-5level %date{G} - %message%newline">
Results in
INFO 12/11/2008 2:15:07 PM - login.aspx submitted for 7234

value="%-5level %date{u} - %message%newline"
Results in (not this is UTC time)
INFO 2008-12-11 14:06:59Z - login.aspx submitted for 7234

Those of you familiar with date string formats in the .Net framework will recognize some of these strings. The Log4Net framework allows you to use any date format that is valid in a ToString() call in the .Net framework. For additional format strings see the MSDN Format String Documentation.

Monday, December 01, 2008

Adding A New Label

I am long past due for an update to this blog. Part of my lack of motivation is knowing that most of the people who subscribe via various readers came here for the cycling posts. Unfortunately, other than my sporadic attempts to attend my local spinning class, I have reverted to near non-cyclist status.

This is not my preferred lifestyle, but represents a trade-off based on life long passions and priorities. Perhaps a little history is in order...

Many, many years ago in the very late years of the fabulous decade commonly referred to as the '80s', I met a fabulous woman. This woman became my wife, and led me to exclaim jokingly that in my life, she ranked right up there with Basketball and Subway Sandwiches. While she is clearly (at least I hope it's clear to people other than me, and to her most of all) at the top of my list, basketball remains a deep love, one that after years of devotion has come to return my favor.

My New Gig
About two months ago I was approached by the head coach of my son's high school basketball team about the possibility of joining his staff. It took several weeks of negotiations with family, bosses and my own insecurities, but approximately 3 weeks ago I became an assistant basketball coach and the head (read 'only') sophomore basketball coach for the high school. I run practice for 2-2.5 hours after my regular job each day, and will be on the bench for 20 soph, 21 JV and 21 varsity games.

This is truly a labor of love, because so far all I have been paid is 4 t-shirts and a pair of shorts. I am sure that the income will never come close to the costs. It probably won't even cover my gas money for the season. But already, having lost our first game by 20, and watching as the varsity lost by 50 to one of the premier programs in the state, I can't help but smile and say to myself, "Tomorrow is going to be a great day...I get to coach basketball again."