• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Welcome to Leon's Blog
博客园    首页    新随笔    联系   管理    订阅  订阅

Logger实例程序

   这是我写的第一个关于Logger的实例程序:

Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
namespace LoggerExample
{
    
class Program
    {
        
static void Main(string[] args)
        {
            LogEntry logEntry 
= new LogEntry();
            logEntry.EventId 
= 100;
            logEntry.Priority 
= 2;
            logEntry.Message 
= "Sample message";

           
// logEntry.Categories.Add("UI Events");
           
// logEntry.Severity = TraceEventType.Information;
           
// logEntry.Priority = 5;
            Logger.Write(logEntry);

            Console.WriteLine(
"The logger has been recorded.");
            Console.ReadLine();
        }
    }
}

其中参考了:http://blog.csdn.net/snlei/archive/2008/03/07/2155925.aspx

http://www.devx.com/dotnet/Article/36184/0/page/1

http://blog.csdn.net/flyingdream123/archive/2009/01/31/3855542.aspx

还有TerryLee的文章。

重点不在程序如何去写,关键是配置文件,使用微软自带的工具,可以很方便的编辑配置文件:

 

Code
 1<configuration>
 2  <configSections>
 3    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
 4  </configSections>
 5  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
 6    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
 7    <listeners>
 8      <add fileName="MyTrace.log" header="----------------------------------------"
 9        footer="----------------------------------------" formatter="Text Formatter"
10        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
11        traceOutputOptions="DateTime" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
12        name="FlatFile TraceListener" />
13      <add source="Enterprise Library Logging" formatter="Text Formatter"
14        log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
15        traceOutputOptions="DateTime" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
16        name="EventLog TraceListener" />
17    </listeners>
18    <formatters>
19      <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;)}"
20        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
21        name="Text Formatter" />
22    </formatters>
23    <logFilters>
24      <add categoryFilterMode="AllowAllExceptDenied" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
25        name="My Category Filters" />
26    </logFilters>
27    <categorySources>
28      <add switchValue="All" name="General">
29        <listeners>
30          <add name="FlatFile TraceListener" />
31          <add name="EventLog TraceListener" />
32        </listeners>
33      </add>
34    </categorySources>
35    <specialSources>
36      <allEvents switchValue="All" name="All Events" />
37      <notProcessed switchValue="All" name="Unprocessed Category" />
38      <errors switchValue="All" name="Logging Errors &amp; Warnings">
39        <listeners>
40          <add name="FlatFile TraceListener" />
41        </listeners>
42      </errors>
43    </specialSources>
44  </loggingConfiguration>
45</configuration>

 

posted @ 2009-05-26 22:05  Leon916  阅读(450)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3