📜 ⬆️ ⬇️

Logging with Microsoft Enterprise Library 4.1

This article describes how to work with the logging unit from the Microsoft Enterprise Library 4.1. I decided to understand after visiting Patterns & Practices Roadmap Kiev .

Installing Enterprise Library


Download Enterprise Library 4.1 from here . Remember that besides the logging block many other components are installed.


Creating a new project in Visual Studio


Open Viual Studio, create a new console project (console application) and name it, for example, HelloWorldEntLibLogging .
')
Next you need to add a reference to the library:

image

Logging configuration


Most of the EntLib functionality is configured, including the log block. You need to add a configuration file to the project and add information using the add-on for Visual Studio, which is installed along with the library.

Using the Solution Explorer, we add a new Application Configuration File, leaving the default file name app.config:

image

Next, right-click on the app.config and select Edit Enterprise Library Configuration:

image

In the opened editor, select New | Logging Application Block:

image

A new Logging Application Block should appear in the editor tree:

image

Close the editor, open the app.config:

1: <? xml version ="1.0" encoding ="utf-8" ? >
2: < configuration >
3: < configSections >
4: < section name ="loggingConfiguration" type ="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
5: < section name ="dataConfiguration" type ="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
6: </ configSections >
7: < loggingConfiguration name ="Logging Application Block" tracingEnabled ="true"
8: defaultCategory ="General" logWarningsWhenNoCategoriesMatch ="true" >
9: < listeners >
10: < add source ="Enterprise Library Logging" formatter ="Text Formatter"
11: log ="Application" machineName ="" listenerDataType ="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
12: traceOutputOptions ="None" filter ="All" type ="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
13: name ="Formatted EventLog TraceListener" />
14: </ listeners >
15: < formatters >
16: < add template ="Timestamp: {timestamp} Message: {message} Category: {category} Priority: {priority} EventId: {eventid} Severity: {severity} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id: {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties: {dictionary({key} - {value} )}"
17: type ="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
18: name ="Text Formatter" />
19: </ formatters >
20: < categorySources >
21: < add switchValue ="All" name ="General" >
22: < listeners >
23: < add name ="Formatted EventLog TraceListener" />
24: </ listeners >
25: </ add >
26: </ categorySources >
27: < specialSources >
28: < allEvents switchValue ="All" name ="All Events" />
29: < notProcessed switchValue ="All" name ="Unprocessed Category" />
30: < errors switchValue ="All" name ="Logging Errors & Warnings" >
31: < listeners >
32: < add name ="Formatted EventLog TraceListener" />
33: </ listeners >
34: </ errors >
35: </ specialSources >
36: </ loggingConfiguration >
37: </ configuration >


* This source code was highlighted with Source Code Highlighter .

Now there are a lot of different settings in the file, but they are not currently interested in us.

Add the logging code to the application


We connect the namespace:

using Microsoft.Practices.EnterpriseLibrary.Logging;

* This source code was highlighted with Source Code Highlighter .

Next, add the following code to Program.cs:

class Program
{
static void Main( string [] args)
{
LogEntry entry = new LogEntry()
{
Message = "Hello Ent. Lib. Logging"
};

Logger.Write(entry);
}
}


* This source code was highlighted with Source Code Highlighter .

This is all you need to do!

Running the application and checking the logging


Run the application, then run the Windows Event Viewer and watch the latest events. You should see the recorded information:

image

You can also specify Severity in your LogEntry :

LogEntry entry = new LogEntry()
{
Message = "Hello Ent. Lib. Logging" ,
Severity = TraceEventType.Critical
};


* This source code was highlighted with Source Code Highlighter .


If you run the application now, you will see a critical error Windows Event Viewer.

PS From myself I will add that, in addition to the logging block, EntLib includes a large number of other blocks, including Caching, Cryptography, Data Access, Exception Handling, Logging, Policy Injection, Security, Validation and Unity. More information can be found on the site http://entlib.codeplex.com/ .

Source: https://habr.com/ru/post/71078/


All Articles