BrainDump
  Home arrow BrainDump arrow Page 3 - Working with Log4net
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

Working with Log4net
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-08-09

    Table of Contents:
  • Working with Log4net
  • Getting Started
  • Using log4net
  • Configuring log4net

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with Log4net - Using log4net


    (Page 3 of 4 )

    The use you will make of log4net will depend on whether you are a developer (as we assume to be the case, since you’re reading this book) or an administrator (a role you may fill from time to time):

    For developers

    Logging aids debugging during development by showing the flow through the application. Logging tools complement other development tools, such as debuggers and profilers. Logging should never be removed from an application; if it was useful once, it may prove useful again.

    For system administrators

    Logging is an invaluable tool for aiding system administrators in the diagnosis of problems with their applications in a production environment. Administrators are better able to communicate with the development team when they have extensive logs available with which to detect and diagnose issues.

    The log4net framework makes use of three key concepts and provides the mechanisms for configuring each:

    Loggers

    Loggers are named objects that a developer can use to output contextual logging information from within an application. The loggers are in a hierarchy based on their names, such that a logger with the namefoois the parent offoo.bar. The most commonly used scheme is to use one logger per type and to name the logger with its namespace and type name. There is always arootlogger, which exists at the top of the hierarchy.

    Loggers expose an API enabling output at one of the following levels of severity:DEBUG,INFO,WARN,ERROR,orFATAL. Each logger can be configured with a severity level; loggers that are not explicitly configured inherit the level from their closest ancestor. Therootlogger must be explicitly assigned a severity level.

    This structure allows the developer to insert logging code into an application, knowing that he can control the output so that only messages at and above a certain level of severity will be logged. Crucially, he can achieve this without recompiling, or even restarting, the application or component.

    Appender

    It’s easy to output logging messages from applications and to control which messages are logged, but where do the messages end up, and how can you view them? The purpose of log4net appenders is to handle the outputting of messages. Table 4-1 lists the most commonly used appenders currently included in the standard log4net distribution. Developers can also create their own appenders.

    Table 4-1. Popular logging appenders

    Appender Description
    AdoNetAppender Writes logging events to a database using either prepared statements or stored procedures.
    ConsoleAppender Writes logging events to the application’s console. The events can go to either the standard out stream or the standard error stream.
    EventLogAppender Writes logging events to the Windows Event Log.
    RemoteSyslogAppender Writes logging events to a remote syslog service using UDP networking.
    RemotingAppender Writes logging events to a remoting sink using .NET remoting.
    RollingFileAppender Writes logging events to a file in the filesystem. TheRollingFileAppendercan be configured to log to multiple files based upon date or file-size constraints.
    SmtpAppender Sends logging events to an email address.

    A major benefit of using log4net overSystem.Console.WriteLine()is that log4net can output messages to multiple destinations. In combination with message severity levels, logging to multiple destinations can be extremely useful.

    Consider an application running in production. Administrators are generally interested only in monitoring errors. It would be ideal if they could be actively notified if something goes very badly wrong, but it would also be nice to have some detailed information about the circumstances causing the failure. To achieve this, you can use a three-appender log4net configuration such as this:

    EventLogAppender

    Output all messages atERROR andFATALlevels to the Windows Application Event Log.

    RollingFileAppender 

    Output all messages atINFO,WARN,ERROR, and  FATALlevels to a set of rolling files, and set a file-size limit on these files to prevent excessive disk space usage.

    SmtpAppender

    Output messages atFATALlevel for delivery to a notification email address.

    This configuration provides notification of fatal conditions by email, stores a persistent record of all errors in the Windows Event Log, and keeps a detailed activity trace for a short period of time to facilitate problem diagnosis.

    Finally, the log4net framework can be configured so that loggers output to specific appenders, enabling messages from different aspects of an application to be handled appropriately.

    Layout

    Appenders use layouts to customize the output format of messages. log4net includes thePatternLayout, which enables the user to specify the message output format according to conversion patterns similar to theprintf()function in C. For example, the pattern:

      %timestamp [%thread] %-5level %logger - %message%newline

    outputs messages formatted like this:

      234 [main] WARN story.children.littleredridinghood – There is a wolf in
      grandma's bed!

    Table 4-2 lists the most popular layouts currently included in the standard log4net distribution. Of course, developers can also create their own layouts.

    Table 4-2. Popular layouts

    Layout

    Description

    PatternLayout

    Formats the logging event according to a flexible set of formatting flags

    SimpleLayout

    Formats the logging event very simply ([level] -[message])

    table 4-2. Popular layouts (continued)

    Layout

    Description

    XmlLayout

    Formats the logging event as an XML element

    XmlLayoutSchemaLog4j

    Formats the logging event as an XML element that complies with the log4j event DTD

    More BrainDump Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Windows Developer Power Tools," published...
     

    Buy this book now. This article is excerpted from chapter four of Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly, 2006; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.

    BRAINDUMP ARTICLES

    - Internet Explorer 8 Review
    - Nilpo`s Top Windows Add-Ons
    - Beginning Silverlight 2.0 Development using ...
    - Fixing Vista`s Troubles
    - Preparing Windows Images for Mass Deployment
    - The Trouble With Vista
    - Slipstreamed and Unattended Windows Installa...
    - Microsoft Office SharePoint Server
    - Microsoft Office SharePoint Designer
    - Microsoft Windows SharePoint Services 3.0
    - Microsoft Live Mesh Overview
    - XAML Brushes and Silverlight
    - Silverlight and XAML Basics
    - Immortal XP
    - XAML Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT