Search
Tuesday, October 16, 2018 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
     
  
   TechTidBits (Blog)  

Logging Enhancements for Log4net!

May 14

Written by:
Thursday, May 14, 2009 8:57 PM  RssIcon

Today I realized the current way I was using Log4net was, well....suboptimal.  Read on for what the problem was and how I fixed it!

Two days ago I wrote about Log4net, a very cool logging utility which will cut hours/days off your development schedule!  Yes, it's that cool!  Well, today, I had another epiphany!  Thanks to SG!

Today, while debugging my webapp using VS and Log4net, I realized it was a bit of a PITA to have to goto another program (Notepad++) to view my log file.  Notepad++ does a great job of viewing a log file and automagically recognizing when a file's changed and require's reloading, but there has to be a better way!

SG and I were talking over coffee (most great discussions are over coffee LOL) and he mentioned about how Log4net can do multiple destinations for logging!  HUH?  What's that you ask?  JUUUUUUUUUUUUUst like delegates, you have have the same "event" or log message goto multiple places, like your event listeners.  WOW!!!!!!!!  That's cool!  And more over, you can target your logging, like having your INFO messages goto your log file, and ERRORS goto a DB and email, and your FATAL messages goto a networked drive and email.  VERY POWERFUL.

So without further adieu, here's a sample I'm currently using with VS08 which logs DEBUG, INFO and WARNings to VS' debug output window in one format (no need to see the line number and filename when it's just information) and when ERRORs or FATAL exceptions are logged, the offending filename and line number are logged as well.  This is truely cool IMHO!

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Logs/AccessProWeb.%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%newline" />
      </layout>
    </appender>
    <appender name="DebugErrorsAppender" type="log4net.Appender.DebugAppender" >
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="FATAL" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5level - %message [%location]%newline" />
      </layout>
    </appender>
    <appender name="DebugInfoWarnAppender" type="log4net.Appender.DebugAppender" >
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="WARN" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5level - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="DebugInfoWarnAppender" />
      <appender-ref ref="DebugErrorsAppender" />
    </root>
  </log4net>

You might be wondering why I'm using the DebugAppender and not the other OutputDebugStringAppender, it's cause the former will output directly to the VS Output Window instead of to the debug system where you would need another tool to view.  Personal choice, I'm in VS, I'm looking at the output window anyways, so why not?  Some people like the other app way of viewing the logging messages, more information viewable, kind of like the old UNIX tail program.

So, now that you're MUCH more efficient at logging, it's time to go grab a coffee and get coding!

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

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