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

转载于:https://www.cnblogs.com/zhaozhan/archive/2010/08/26/1809612.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值