posts - 930,  comments - 588,  views - 402万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

      这篇文章将演示花费最小的代价使用Microsoft’s Enterprise Library 4.1在你的应用程序中记录日志。下面是一些细节,第一次如何在代码logging Message:

下载安装Enterprise Library 4.1

  让我们开始,创一个VS控制台程序名叫HelloWorldEntLibLogging,在项目中引用Ent. Lib. logging assembly,如图:

entliblogging1

为应用程序配制Logging,增加一个App.config

entliblogging2

在App.config文件上单击右键:

entliblogging3

在弹出的菜单编辑器选择New | Logging Application Block

entliblogging4

这时一个新的Logging Application Block 配制节点是这样的:

entliblogging5

接着,打开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}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
  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 &amp; 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:

entliblogging6

然后记下来的细节信息是:

Timestamp: 1/20/2009 11:20:08 PM
Message: Hello Ent. Lib. Logging

Category: 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

posted on   PetterLiu  阅读(4305)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示