This article demonstrates using Microsoft's Enterprise Library 4.1 to record logs in your application at minimal cost. Below are some details about how to log on to the Code for the first time:
Download and install Enterprise Library 4.1
Let's start, create a VS console program named HelloWorldEntLibLogging, reference Ent. Lib. logging assembly in the project ,:
Configure Logging for the application and add an App. config
Right-click the App. config file:
In the pop-up menu Editor, selectNew | Logging Application Block
In this case, a newLogging Application BlockThe configuration node is like this.:
Next, open the XML content of 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,