Saturday, March 03, 2001 ..:: Home ::.. Register  Login
   TechTidBits (Blog)  

What are you using for logging?

May 12

Written by:
Tuesday, May 12, 2009 10:11 PM  RssIcon

Are you still doing your logging using Windows Event Viewer?  How 2000-ish!  What are you doing?  Get into the next decade and start using Log4net!

Last year I had to write a Windows Service and MAN, I did a lot of work to make sure I knew what was going on at all times by logging all kinds of things to the Event Log.  However, I quickly found out some problems using that method/tool.

  1. Required to use a specialized tool (EventViewer) to view logging data, haven't you heard of "a browser?"
  2. There was NO way to view the logging data remotely, I could have remoted in, but that's so 2000 or Citrix-like, blah!
  3. Administrator requirements to install the Event Source to properly log events, this isn't too big a deal cause you need to be an administrator to install a Windows Servcie anyways, so this one's a draw.
  4. There is no easy programatic way to "ship out" logging data to the developer (hello?  that's me :>) when the customer/BA is having problems, someone is required to be ON that server and use the specialized tool (Event Viewer) to save/copy/export that logging data to a file, THEN ship it out, what a pain
Ok, I could probably go on, but I think that's enough for now. 
So what can you do about all this?  Well, in one word, Log4net!  With one config file which can be dynamically reloaded (ala web.config kind of reloading) you can change the depth of logging (Debug, Info, none, All, Warn, Error), log to the Event Viewer, a text/flat file, xml file, a database, console, remote, you can even send emails!  This things's the cat's meow!  And it's all configurable!
The first time I was introduced to this was by a sharp java guy (DR) and six/seven years ago I thought it was just too complicated.  So when SG started poking around with it, I was skeptial about the .NET implementation.  But SG said just tried it out, trust me.  I did, and I'm a convert!  With one simple config file, I can quickly log anything, have it automagically roll over at the end of the month OR by filesize, tell it which file to log to, even give it a dynamically named filename AND I can even configure how the internals of the logging messages will look like.
Here's a sample of a log4net config file I'm using right now (yup, I'm putting it here cause I've already wanted to use this same config file four times in the last two weeks but had to reproduce the jist of it everytime......sounds like a blog post to me, and here we are, fancy that?! LOL)
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Logs/MyLogFilename.%date{yyyyMM}.log" />
      <appendToFile value="true" />
      <staticLogFileName value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <rollingStyle value="Composite"/>   <!--use both date and log size to decide when to rollover-->
      <datePattern value="yyyyMM"/>   <!--roll over by month-->
      <maximumFileSize value="10MB" />    <!--roll over when 10MB reached-->
      <maxSizeRollBackups value="12"/>  <!--default 12 = one years worth-->
      <layout type="log4net.Layout.PatternLayout">
        <!--<conversionPattern value="%date [%thread] %-5level %logger - %message [%location]%newline" />-->
        <conversionPattern value="%date %-5level - %message" />
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />

Now we're able to properly log, now it's time to grab a coffee and get coding!

Location: Blogs Parent Separator TechTidBits

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Add Comment   Cancel 
Copyright 1999-2012 by   Terms Of Use  Privacy Statement