Tuesday, February 21, 2006

Enterprise Library Logging Application Block

I’ve downloaded the January 2006 Enterprise Library and installed it. I wanted to play with the logging application block for some things I’ve got in mind. It’s very easy to configure and use the blocks. Included in the download is the Enterprise Library Configuration Utility. You open your app.config or web.config file with the utility and the necessary sections and entries are saved there for you.



TraceListeners provide a location for the logging data to be stored. For example, the FlatFile TraceListener uses a text file with a name of trace.log by default. Other TraceListeners are provided for database, MSMQ, email, and the Windows event log, just to name a few; and you can even create your own. Adding one of the TraceListeners to a Category sends log data for that category to the appropriate store. Once the configuration is complete, it’s time to write some logging code.

Before writing the logging code there is one little bit of housekeeping that’s necessary. Add references to:
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
Microsoft.Practices.ObjectBuilder.dll

Add the following at the top of your source file:
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

Now, you are ready to write some code. The LogEntry class will contain the data that you want to log. So, create one, set some of its properties, and write it to the log. Logger is a static class that contains a bunch of overloaded static Write methods. It makes it all very easy.

LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Categories.Add("Runtime Data Collection");
logEntry.Priority = 6;
logEntry.Severity = System.Diagnostics.TraceEventType.Critical;
logEntry.Message = "Logging important information";
logEntry.Title = "Test Entry";
logEntry.MachineName = Environment.MachineName;
Logger.Write(logEntry);

That’s really all there is to basic logging. Running the application in Visual Studio 2005 produces the following log entry.

----------------------------------------
Timestamp: 2/22/2006 2:40:45 AM
Message: Logging important information
Category: Runtime Data Collection
Priority: 6
EventId: 100
Severity: Critical
Title:Test Entry
Machine: GODZILLA
Application Domain: LoggingApp.vshost.exe
Process Id: 4128
Process Name: C:\Documents and Settings\jbennett\My Documents\Visual Studio 2005\Projects\LoggingApp\LoggingApp\bin\Debug\LoggingApp.vshost.exe
Win32 Thread Id: 4144
Thread Name: UI Thread
Extended Properties:
----------------------------------------

Easy logging is only a download away. Get it here.

3 Comments:

At 8:07 AM, Blogger siva said...

I was working in the way you had explained but i got the following error......

{System.TypeInitializationException: The type initializer for 'Microsoft.Practices.EnterpriseLibrary.Logging.Logger' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null'
at Microsoft.Practices.EnterpriseLibrary.Logging.Logger..cctor()

=== Pre-bind state information ===
LOG: User = CORP\pittus
LOG: DisplayName = Microsoft.Practices.EnterpriseLibrary.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/ADC/Project/WordDocument/WordDocument/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\ADC\Project\WordDocument\WordDocument\bin\Debug\WordDocument.vshost.exe.config
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/ADC/Project/WordDocument/WordDocument/bin/Debug/Microsoft.Practices.EnterpriseLibrary.Common.DLL.
WRN: Comparing the assembly name resulted in the mismatch: PUBLIC KEY TOKEN
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

--- End of inner exception stack trace ---
at Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(LogEntry log)
at WordDocument.Form1.LogData() in C:\ADC\Project\WordDocument\WordDocument\Form1.cs:line 243}

Could you plase let me know about this error.

 
At 6:30 AM, Blogger Dr.Luiji said...

Hi Joe,
I'v a problem when i change the culture in my thread.
I use a rolling file with a
timestamp. When i change the culture thread the timestamp in the log change with this culture.

How i can set a fixed timestamp? I have tried the TimeStampPattern but seems useless. Thx in advice.

 
At 11:53 PM, Blogger ColinQ said...

Thanks for ur blog, i got my logging block working :D

 

Post a Comment

<< Home