EntLib 3.1学习笔记(4) : Logging Application Block

 



[zh] http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/dnpag2logging.mspx?mfr=true
[en] http://msdn2.microsoft.com/en-us/library/ms998162.aspx

    开发人员经常编写需要日志和规范功能的应用程序。通常,这些应用程序必须适当地格式化事件和记录事件,不论是在本地还是通过网络。在某些情况下,您可能需要对一台计算机上来自多个源的事件进行整理。

    日志应用程序块通过收集应用程序需要包含的多个最常见的日志和规范任务来简化应用程序的开发。每个任务都以一致的方式处理,并从特定的日志和规范提供程序中抽象应用程序代码。体系结构模型可让您通过更改配置来更改基础事件接收器和格式化程序,而无需更改应用程序代码。

1.应用程序可以使用日志块在多个位置记录事件:
(1) 事件日志
(2) 电子邮件
(3) 数据库
(4) 消息队列
(5) 文件
(6) WMI

2. 使用
(0) EntLib配置工具配置App.config/Web.config,New->Logging Application Block:
    a. 创建或定制Formatter(默认只有一个Text Formatter,我们可以定制其Template);
    b. 创建或定制Trace Listeners(默认只有一个Fomatted EventLog TraceListener,其可以将日志记录在系统日志中),并为其指定一个Formatter(上一步a中定义了的Formatter);不同的Trace Listener记录事件的位置(Email、EventLog、File、DB、MSMQ、WMI)不同
    c. 创建Category Source(默认只有一个General Category,其使用EventLog TraceListener将日志记录在系统日志中),并为其指定Trace Listener(上一步b中定义了的Trace Listener).
   
(1) 记录日志:

LogEntry log = new LogEntry();
log.EventId 
= 300;
log.Message 
= "Sample message";
logEntry.Categories.Clear();
log.Categories.Add(
"CategoryName");//参数为上面步骤c中定义的Category.Name,可以Add多个Category
log.Severity = TraceEventType.Information;
log.Priority 
= 5;
Logger.Write(log);


(2) 日志里面包含名-值对的字典

Dictionary<stringobject> dictionary = new Dictionary<stringobject>(); 
ManagedSecurityContextInformationProvider informationHelper 
= new ManagedSecurityContextInformationProvider();
informationHelper.PopulateDictionary(dictionary);
dictionary.Add(
"logInforName", strLogInfor);
Logger.Write(
"Log entry with extra information", dictionary);//使用默认的Genera Category,记录事件到系统日志中


(3) 跟踪活动并记录上下文信息
    LAB支持通过活动ID来跟踪一项活动,活动ID可以在代码中指定,也可以通过程序块来自动生成,LAB自动记录活动的起始时间和结束时间:

using (new Tracer("Category1"))
{
       
using (new Tracer("Category2"))
       
{
  LogEntry logEntry 
= new LogEntry();
  
//给LogEntry的属性赋值
                Logger.Write(logEntry);//此时,logEntry会写5条日志:
  
//1. Start Trace: Activity 'ActivityID' .. at .. ticks
  
//   Category: Category1
  
//2. Start Trace: Activity 'ActivityID(同上)'.. at .. ticks
  
//   Category: Category2, Category1
  
//3. Simulated General ..Activity='ActivityID(同上)'
  
//   Category: General, Category2, Category1
  
//4. End Trace: Activity 'ActivityID(同上)' at  ticks
  
//   Category: Category2, Category1
  
//5. End Trace: Activity 'ActivityID(同上)' at  ticks
  
//   Category: Category1
       }

}

(4) 创建过滤事件
Filters->New->可以创建Category Filter/Custom Filter/LogEnabled Filter/Priority Filter
Category Filter:根据Category的类别进行过滤(Deny all except: someCategory);
Priority Filter:只记录优先级在MinimumPriority和MaxmumPriority之间的事件;
LogEnabled Filter:记录(Enabled=true)或不记录(Enabled=false)所有的LogEntry。

if (Logger.GetFilter<LogEnabledFilter>().Enabled)
{
     
// Logging is enabled.
}

else
{
     
// Logging is not enabled.
}


if (Logger.GetFilter<CategoryFilter>().ShouldLog(categories))//ICollection<string> categories;
{
     
// Event will be logged.
}

else
{
     
// Event will not be logged.
}


if (Logger.GetFilter<PriorityFilter>().ShouldLog(priority))//int priority;
{
     
// Event will be logged.
}

else
{
     
// Event will not be logged.
}


if (Logger.ShouldLog(logEntry))//汇总上面的过滤结果,判断是否应该过滤掉该事件
{
    
// Perform possibly expensive operations gather information for the event to be logged.
}

else
{
    
// Event will not be logged.
}



3. Logging Application Block的设计:




posted @ 2009-03-08 16:54  貔貅  阅读(360)  评论(0编辑  收藏  举报