Tuesday, January 30, 2007

* Enterprise Library : Using Logging Application Block

The main objectives of this block are :

  • Consistent Logging practices
  • Provides Tracing Framework
  • Provides Auditing Framework
  • Configurable what, when and where to log options
  • Ability to develop and use custom trace listeners

To use Logging application block following assemblies need to be referenced :

  • Microsoft.Practices.EnterpriseLibrary.Common.dll
  • Microsoft.Practices.EnterpriseLibrary.Logging.dll
  • Microsoft.Practices.ObjectBuilder.dll

These assemblies get installed with enterprise library.

Consider the sample code using Logging application block

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.ObjectBuilder;

namespace LoggingAppBlock
{
class Program
{
static void Main(string[] args)
{
LogEntry log = new LogEntry();
log.EventId=111;
log.Message = "Sample Log Message";
log.Categories.Add("Trace");
Logger.Write(log);
Console.WriteLine("log written");
}
}
}








LogEntry is the class used to create a log entry while Logger class is used to log the entry. The configuration of the logging application block can be done using Enterprise Library Configuration Tool (EntLibConfig.exe) or manually. For the above application the configuration of the block needs to be copied to the application configuration file.

The configuration of logging block mainly involves configuring three items :



  • Categories : each log entry can be assigned to one or more groups and based on category, the entry can be logged to different trace listeners

  • Trace Listeners: these are the destination where logs need to be created e.g. event log, flat file, email or custom listener.

  • Formatters : The format of the log entry. A default text formatter comes with library.


Event Log entry for above code



----------------------------------------



Event Type: Information
Event Source: Enterprise Library Logging
Event Category: None
Event ID: 111
Date: 1/29/2007
Time: 10:51:11 PM
User: N/A
Computer: MACHINE21254
Description:
Timestamp: 1/29/2007 5:21:11 PM
Message: Sample Log Message
Category: Trace
Priority: -1
EventId: 111
Severity: Information
Title:
Machine: MACHINE21254
Application Domain: LoggingAppBlock.exe
Process Id: 504
Process Name: D:\Vikas\vikas\tech\code\LoggingAppBlock\LoggingAppBlock\bin\Debug\LoggingAppBlock.exe
Win32 Thread Id: 3496
Thread Name:
Extended Properties:

----------------------------------------
Flat file entry using default text formatter



----------------------------------------
Timestamp: 1/29/2007 5:21:11 PM
Message: Sample Log Message
Category: Trace
Priority: -1
EventId: 111
Severity: Information
Title:
Machine: MACHINE21254
Application Domain: LoggingAppBlock.exe
Process Id: 504
Process Name: D:\Vikas\vikas\tech\code\LoggingAppBlock\LoggingAppBlock\bin\Debug\LoggingAppBlock.exe
Win32 Thread Id: 3496
Thread Name:
Extended Properties:
----------------------------------------


Pls download the tutorial from following post.




Other Posts



1 comment:

  1. i created log file where it will store how to view the file. i written fow configuration file in web.config plz reply me mail id easwari_tsv@yahoo.co.in
    Regards
    venkateswari

    ReplyDelete