zoukankan      html  css  js  c++  java
  • Enterprise Library Logging 快速上手

          这篇文章将演示花费最小的代价使用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

  • 相关阅读:
    编程题#2: 魔兽世界之二:装备
    程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1
    【转】C++动态创建二维数组,二维数组指针
    HDU-2571命运
    HDU-1203 I NEED A OFFER!
    HDU-1003 Max Sum
    HDU2196-Computer
    HDU-1520 Anniversary party
    ChineseHelper(获取汉字字符串的首拼)
    车牌号正则表达式(新能源车牌)
  • 原文地址:https://www.cnblogs.com/wintersun/p/1672387.html
Copyright © 2011-2022 走看看