BrainDump
  Home arrow BrainDump arrow Page 2 - 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 - Getting Started


    (Page 2 of 4 )

    Setting up your project to use log4net is straightforward. Just download the distribution from the Downloads link on the tool’s home page, extract the log4net distribution, and follow these four steps.

    Step 1: Referencing the assembly

    You must select the correct build of log4net for your environment. For example, if you are using version 1.1 of the .NET Framework, you should select the log4net.dll file located in the bin\net\1.1\release folder in the log4net distribution. To add a reference to your Visual Studio project, select Add Reference from the Project menu.

    Step 2: Configuring log4net

    log4net can be configured in the app.config file or via an external XML file. To configure log4net in the app.config file, add the following fragment to the file under the configuration element:

      <configSections>
          <section name="log4net" 
                   type="log4net.Config. Log4NetConfigurationSectionHandler, log4net" />
      </configSections>

      <log4net>
          <appender name="MyAppender1" type="log4net.Appender.DebugAppender" >
              <layout type="log4net.Layout.PatternLayout 
                     
    value="%date [%thread]
    %-5level %logger - %message%newline" />
          </appender>
          <root>
             
    <level value="DEBUG" />
              <appender-ref ref="MyAppender1" />
          </root>
      </log4net>

    To configure log4net in an external XML file, or to review configuration options and examples, please see the log4net web site.

    Step 3: Initializing logging within the application

    There are two ways to initialize logging within your application. The first is to directly configure log4net in your code; this call should be made as early as possible during application initialization. Use a code fragment like the following that is appropriate for your language:

      log4net.Config.XmlConfigurator.Configure();

    There are a number of overloaded versions of this method that you can use to supply parameters such as the name of a configuration file. Further details are available in the log4net documentation.

    Alternatively, you can create an attribute on the application’s main assembly specifying the location of the log4net configuration settings. log4net will then automatically configure itself when the first logger is created. The assembly attribute should be specified in a project source file, such as AssemblyInfo.cs or AssemblyInfo.vb, as follows:

      [assembly: log4net.Config.XmlConfigurator()]

    Step 4: Adding logging messages to the code

    To output logging messages from code, you must first create a logger. You can do this by adding the following line of code to each of your types (in this example, we assume that the type is named Form1): 

      private static readonly ILog log = LogManager.GetLogger(typeof(Form1));

    Logging messages can then be added as follows:

      // Simple example
      log.Debug("Something happened");

      // Example showing variable substitution
      log.DebugFormat("Something happened because {0} is less then {1}", var1, var2);

      // More complex example demonstrating level test
      if (log.IsDebugEnabled)
      {
        
    string s = ComplicatedMethod();
        
    log.DebugFormat("The value of ComplicatedMethod is {0}", s);
      }

    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 6 hosted by Hostway
    Stay green...Green IT