這篇文章將演示花費最小的代價使用Microsoft’s Enterprise Library 4.1在你的應用程序中記錄日志。下面是一些細節,第一次如何在代碼logging Message:
下載安裝Enterprise Library 4.1
讓我們開始,創一個VS控制台程序名叫HelloWorldEntLibLogging,在項目中引用Ent. Lib. logging assembly,如圖:
為應用程序配制Logging,增加一個App.config
在App.config文件上單擊右鍵:
在彈出的菜單編輯器選擇New | Logging Application Block
這時一個新的Logging Application Block 配制節點是這樣的:
接著,打開App.config的XML內容是:
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,