ConversionPattern of Log4net

One of the things I like about log4net is that you can dramatically change logging contents by just fiddling with its configuration. It would otherwise be a very tedious task to revisit every place log function was called in your code.

In one of my ASP.Net applications, I found that the information from the error log was not sufficient enough for me to have a clearer idea what has gone wrong. ideally I would like to know which file this exception is in, at which line, by which class etc. then I came across the conversionpattern parameters setting in log4net talking about generating more caller information through configurable parameters.

Here is my version of log4net config in web.config

 <parameter>
       <parametername value="@log_date"></parametername>
       <dbtype value="DateTime"></dbtype>
       <layout type="log4net.Layout.RawTimeStampLayout"></layout>
     </parameter>
     <parameter>
       <parametername value="@thread"></parametername>
       <dbtype value="String"></dbtype>
       <size value="255"></size>
       <layout type="log4net.Layout.PatternLayout">
         <conversionpattern value="type:%class thread:%thread"></conversionpattern>
       </layout>
     </parameter>
     <parameter>
       <parametername value="@log_level"></parametername>
       <dbtype value="String"></dbtype>
       <size value="50"></size>
       <layout type="log4net.Layout.PatternLayout">
         <conversionpattern value="level:%level indetity:%identity"></conversionpattern>
       </layout>
     </parameter>
     <parameter>
       <parametername value="@logger"></parametername>
       <dbtype value="String"></dbtype>
       <size value="255"></size>
       <layout type="log4net.Layout.PatternLayout">
         <conversionpattern value="%logger filename:%file linenumber:%line"></conversionpattern>
       </layout>
     </parameter>
     <parameter>
       <parametername value="@message"></parametername>
       <dbtype value="String"></dbtype>
       <size value="4000"></size>
       <layout type="log4net.Layout.PatternLayout">
         <conversionpattern value="location:%location method
name:%method message:%message"></conversionpattern>
       </layout>
     </parameter>
     <parameter>
       <parametername value="@exception"></parametername>
       <dbtype value="String"></dbtype>
       <size value="2000"></size>
       <layout type="log4net.Layout.ExceptionLayout"></layout>
     </parameter>

As I am using a database logger, so it might look bit different from a file logger, it catches those information not only for a message log but also an exception log, it makes sense when these information are caught in regard of a caller either it is a message or an exception. It is no strange that location and line numbers information can be found from an exception object, it took me by some surprise that those information also present for message dumps in log4net.

Most of those parameters are self explanatory, in my ASP.NET project, the %indentity returned with the login user name of form authentication, %location returned with namespaces.class(plus file name and line number).

This entry was posted on Wednesday, July 7th, 2010 at 7:51 am and is filed under ASP.NET, SQLServer. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*