找回密码
 注册
搜索
热搜: java php web
查看: 263|回复: 1

[C#] Enterprise Lib 4.0 - Logging Application Block 学习笔记(二)

[复制链接]
发表于 2010-1-18 15:48:50 | 显示全部楼层 |阅读模式
Enterprise Lib 4.0 - Logging Application Block(二)1. 项目添加的reference:
     Microsoft.Practices.EnterpriseLibrary.Logging.dll,
     Microsoft.Practices.EnterpriseLibrary.Common.dll,
     Microsoft.Practices.EnterpriseLibrary.ObjectBuilder2.dll,
     Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll (如果想使用 DatabaseTraceListener,那么你还必须配置 Data Access Application Block)
     或是直接选择 .NET 组件中的 EnterpriseLibrary Logging Application Block
2.  using Microsoft.Practices.EnterpriseLibrary.Logging;
     using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;(可选,用于收集上下文或是堆栈或或是COM+的信息,可以把信息树存放在LogEntry.ExtendedProperties属性内)
     using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;(可选,如果你想增加自己的过滤器)
3.  代码中使用log的方法:
     LogEntry logEntry = new LogEntry();
     logEntry.EventId = 100;
     logEntry.Priority = 2;
     logEntry.Message = "Informational message";
     logEntry.Categories.Add("Trace");
     logEntry.Categories.Add("UI Events");
     Logger.Write(logEntry);
4.  扩展信息添加
     a. 别忘记了 using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation
     b. 定义一个dictionary对象用作保存你自己的Log内容
     c. 把内容添加进去后,直接赋给 LogEntry
     d. 例子:
     //获得关于安全相关的上下文信息
     Dictionary<string, object> dictionary = new Dictionary<string, object>();
     ManagedSecurityContextInformationProvider informationHelper = new ManagedSecurityContextInformationProvider();   
     informationHelper.PopulateDictionary(dictionary);
     //添加自己的信息
     string ci="Customer Message1"
     dictionary.Add("CM1", ci);
     //LogEntry的属性赋值
     logEntry.ExtendedProperties = dictionary;
5.  使用跟踪器来定位一个方法或是一段代码的效率,跟踪器可以在开始时自动创建拥有指定Category的LogEntry,并且在自己消亡时记录结束的日志
     using (new Tracer("Log Category"))
     {
                // Perform processing to be timed here
     }
     注:可以在这个影响的范围内,使用 Logger.write,相应的LogEntry中会包含这个Category
     using (new Tracer("UI Events"))
     {
         // 会记录一个同时具有 "UI Events" 和 "Debug" 分类的 日志
        Logger.Write("My message", "Debug");
     }
     using (new Tracer("UI Events"))
     {
        using (new Tracer("Data Access Events"))
        {
             LogEntry logEntry = new LogEntry();
             logEntry.Categories.Add("Troubleshooting");
             Logger.Write(logEntry); //这个logEntry 将同时具有 "UI Events","Data Access Events","Troubleshooting"三个分类
        }
    }
6.  使用在配置文件中定义的过滤器 Filter
    在你需要记录的代码中,可以使用Logger.ShouldLog(logEntry)
    LogEntry logEntry = new LogEntry();
    logEntry.Priority = 2;
    logEntry.Categories.Add("Trace");
    logEntry.Categories.Add("UI Events");
   if (Logger.ShouldLog(logEntry))
   {
    // Event will be logged according to current configuration. Perform operations (possibly
    // expensive) to gather additional information for the event to be logged.
   }
   else
   {
    // Event will not be logged. Your application can avoid the performance
    // penalty of collecting information for an event that will not be logged.
   }
7. 因此建议的用法是:
   a. 在配置文件中定义你的Filter,Category(专门放一个需要调试用的 Debug类), 以及Formatter(默认的Formatter是比较合适application的开发,但如果是asp.net的话,建议精简你的Formatter的输出)
   b. 在你自己开发调试的时候 统一用 using (new Trace("Debug")),在发布的时候,把这个Debug 取消
   c. 定义一个自己觉得有用的工具类方法, 例如
       public static void log(string message,string category,int pri)
       {
          LogEntry le=new LogEntry();
          le.Priority=pri;
          le.Category.Add(category);
          le.message=message;
          if (Logger.ShouldLog(le)) Logger.write(le);
       }
   d. 在你的代码中统一用自己的方法, Utils.log("这个是测试日志","Debug",0);
   e. 可以的话,多给工具类的方法重载点对自己常用的参数
发表于 2010-1-26 03:01:32 | 显示全部楼层
感谢分享!

评分

1

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|软晨网(RuanChen.com)

GMT+8, 2024-9-20 13:36

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表