Enterprise Library 5 Logging (vs2010)

     下载安装Microsoft Enterprise Library 5.0

     创建LoggingTest控制台程序(选择.net framework 4).添加对Microsoft.Practices.EnterpriseLibrary.Logging的引用:

    image

     添加App.config,右键选择Edit EnterpriseLibrary V5 Configuration。

     image

     添加Logging Settings:

    image

    生成的App.config:

 1:  <?xml version="1.0"?>
 2:  <configuration>
 3:      <configSections>
 4:          <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
 5:      </configSections>
 6:      <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
 7:          <listeners>
 8:              <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
 9:                  listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
10:                  source="Enterprise Library Logging" formatter="Text Formatter"
11:                  log="" machineName="." traceOutputOptions="None" />
12:          </listeners>
13:          <formatters>
14:              <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
15:                  template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
16:                  name="Text Formatter" />
17:          </formatters>
18:          <categorySources>
19:              <add switchValue="All" name="General">
20:                  <listeners>
21:                      <add name="Event Log Listener" />
22:                  </listeners>
23:              </add>
24:          </categorySources>
25:          <specialSources>
26:              <allEvents switchValue="All" name="All Events" />
27:              <notProcessed switchValue="All" name="Unprocessed Category" />
28:              <errors switchValue="All" name="Logging Errors &amp; Warnings">
29:                  <listeners>
30:                      <add name="Event Log Listener" />
31:                  </listeners>
32:              </errors>
33:          </specialSources>
34:      </loggingConfiguration>
35:  </configuration>
36:  
37:  

 

 

 

 

 

     简单测试代码:

 1:  using System;
 2:  using System.Collections.Generic;
 3:  using System.Text;
 4:  using Microsoft.Practices.EnterpriseLibrary.Logging;
 5:   
 6:  namespace LoggingTest
 7:  {
 8:      class Program
 9:      {
10:          static void Main(string[] args)
11:          {
12:              LogEntry entry = new LogEntry();
13:              entry.TimeStamp = DateTime.Now;
14:              entry.Message = "Test EnterpriseLibrary Logging";
15:              Logger.Write(entry);
16:          }
17:      }
18:  }
19:   
20:   

 

     运行后查看日志文件:

image

posted @ 2010-08-26 23:31  Asharp  阅读(2327)  评论(0编辑  收藏  举报