这篇稿子用演示花费最小的代价使用Microsoft’s Enterprise Library
4.1当你的应用程序中著录日志。下面是部分细节,第一次于如何以代码logging
Message:

下载安装Enterprise Library
4.1

 
让咱开,创一个VS控制台程序名为HelloWorldEntLibLogging,在品种蒙引用Ent.
Lib. logging assembly,如图:

XML 1

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

XML 2

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

XML 3

当弹出的菜谱编辑器选择New | Logging Application Block

XML 4

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

XML 5

接着,打开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:

XML 6

下一场记下来的底细信息是:

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

相关文章

网站地图xml地图