这篇文章将演示花费最小的代价使用Microsoft’s Enterprise Library 4.1在你的应用程序中记录日志。下面是一些细节,第一次如何在代码logging Message:
让我们开始,创一个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, 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>
好的,让我们写一些Code:
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: LogEntry entry = new LogEntry()
6: {
7: Message = "Hello Ent. Lib. Logging"
8: };
9: Logger.Write(entry);
10: }
11: }
Ok,F5运行,打开Event Viewer:
然后记下来的细节信息是:
Timestamp: 1/20/2009 11:20:08 PM
Message: Hello Ent. Lib. LoggingCategory: General
Priority: -1
EventId: 0
Severity: Information
Title:
Machine: BIGRED
Application Domain: HelloEntLibLogging.vshost.exe
Process Id: 5384
Process Name: C:\projects\sandbox\HelloEntLibLogging\HelloEntLibLogging\bin\Debug\HelloEntLibLogging.vshost.exe
Win32 Thread Id: 4512
Thread Name:
Extended Properties:
完了,希望这篇Post对您有帮助。
Author: Petter Liu http://wintersun.cnblogs.com