摘要:Logging Application Block可以使开发人员在其应用程序中集成日志监测功能,看看随着2.0的推出给我们带来了哪些改变。
一.改进的地方
1Logging Application Block首先带来的是名称上的改变,在1.1下它的全称应该是Logging and Instrumentation Application Block,一般把它翻译为日志和检测应用程序块,而2.0下却完全变成了日志应用程序块。
2.在1.1下,每个LogEntry只能被记录到一个Sink,而这种情况在2.0下已经不复存在,对于每个LogEntry对象,我们都可以通过Category指定很多的Sink。回忆一下在1.1时记录一个日志项:
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogEntry logEntry = new LogEntry();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.EventId 
= 100;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Priority 
= 2;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Message 
= "Informational message";
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
//只能设置一个
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
logEntry.Categorys = "UI Events";
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogger.Write(logEntry);
2.0下可以添加多次:
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogEntry logEntry = new LogEntry();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.EventId 
= 100;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Priority 
= 2;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Message 
= "Informational message";
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
//设置多个Category
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
logEntry.Categories.Add("Trace");        
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Categories.Add(
"UI Events");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogger.Write(logEntry);

3.可以在代码中查询哪些日志项将被过滤,例如:
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogEntry logEntry = new LogEntry();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Priority 
= 2;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Categories.Add(
"Trace");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Categories.Add(
"UI Events");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
if (Logger.ShouldLog(logEntry))
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_24
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// Event will be logged according to currently configured filters.
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}

Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
else
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_30
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// Event will not be logged. 
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}

4.配置文件的改变。在1.1下关于Logging & Instrumentation  Application Block的信息记录在loggingconfiguration.config文件中,2.0下所有的信息都放在了Web.configApp.config中,如:
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl<configuration>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl    
<configSections>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl        
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl    
</configSections>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl    
<loggingConfiguration tracingEnabled="true" defaultCategory="General">
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl        
<logFilters>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl            
<add
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl                
name="Category"
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl                type
="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl                categoryFilterMode
="AllowAllExceptDenied">
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl        
<categoryFilters>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl          
<add name="UI Events" />
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl        
</categoryFilters>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl            
</add>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl            
<add
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl                
name="Priority"
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl                type
="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl                minimumPriority
="2"
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl                    
/>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl      
<add name="LogEnabled Filter"
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl        type
="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl        enabled
="true" 
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl           
/>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl    
</logFilters>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
</loggingConfiguration>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
</configuration>
二.记录日志信息
1.添加相关的引用
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlusing Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
using Microsoft.Practices.EnterpriseLibrary.Logging;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

2.创建一个日志项
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogEntry log = new LogEntry();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllog.EventId 
= 300;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllog.Message 
= "Sample message";
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllog.Categories.Add(
"UI Events");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllog.Severity 
= TraceEventType.Information;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllog.Priority 
= 5;

3.调用Logger.Write()方法
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogger.Write(log);

三.记录日志项的扩展属性
使用基于泛型的Dictionary来记录,如下
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl// Create the dictionary to hold the extra information, and populate it
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
// with managed security information.  
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Dictionary<stringobject> dictionary = new Dictionary<stringobject>();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlManagedSecurityContextInformationProvider informationHelper 
= new ManagedSecurityContextInformationProvider();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlinformationHelper.PopulateDictionary(dictionary);
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
// Add a custom property for screen resolution
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
int width = Screen.PrimaryScreen.Bounds.Width;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
int height = Screen.PrimaryScreen.Bounds.Height;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
string resolution = String.Format("{0}x{1}", width, height);
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBldictionary.Add(
"Screen resolution", resolution);
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
// Write the log entry that contains the extra information
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Logger.Write("Log entry with extra information", dictionary);
 
四.跟踪活动并记录上下文信息
1.调用DoDataAccess方法,完成后释放Trace对象
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlusing (new Tracer("Trace"))
Enterprise Library2.0(2):Logging Application Block学习_职场_90
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    DoDataAccess();
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28}
 
2.创建DoDataAccess方法
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlprivate void DoDataAccess()
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_96
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    
using (new Tracer("Data Access Events"))
Enterprise Library2.0(2):Logging Application Block学习_职场_100    
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27        
// Peform work here
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27        
// Assume an error condition was detected - perform some troubleshooting.
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
        DoTroubleShooting();
Enterprise Library2.0(2):Logging Application Block学习_休闲_107    }

Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28}
 
3.创建另一个方法DoTroubleShooting,并在其中创建LogEntry
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlprivate void DoTroubleShooting()
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_110
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    
string logMessage = "Simulated troubleshooting message for Logging QuickStart. " +
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27      
"Current activity=\"" + Trace.CorrelationManager.ActivityId + "\"";
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    LogEntry logEntry 
= new LogEntry();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    logEntry.Categories.Clear();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    logEntry.Categories.Add(
"Troubleshooting");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    logEntry.Priority 
= 5;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    logEntry.Severity 
= TraceEventType.Error;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    logEntry.Message 
= logMessage;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    Logger.Write(logEntry);
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28}
 
五.检测日志项是否被记录
创建一个日志项并设置它的信息,调用Logger.ShouldLog()方法
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogEntry logEntry = new LogEntry();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Priority 
= 2;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Categories.Add(
"Trace");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Categories.Add(
"UI Events");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
if (Logger.GetFilter<CategoryFilter>().ShouldLog(logEntry))
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_132
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// Event will be logged according to currently configured filters.
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// Perform operations (possibly expensive) to gather additional information 
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// for the event to be logged. 
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}

Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
else
Enterprise Library2.0(2):Logging Application Block学习_休闲_140
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// Event will not be logged. Your application can avoid the performance
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// penalty of collecting information for an event that will not be
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// logged.
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}
 
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlLogEntry logEntry = new LogEntry();
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Priority 
= 2;
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Categories.Add(
"Trace");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBllogEntry.Categories.Add(
"UI Events");
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
if (Logger.ShouldLog(logEntry))
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_24
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// Event will be logged according to currently configured filters.
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}

Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
else
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_30
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27  
// Event will not be logged. 
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}
 
六.创建自定义的Trace Listener
1.添加特性ConfigurationElementType,需要继承自CustomTraceListener
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl[ConfigurationElementType(typeof(CustomTraceListenerData))]
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
public class DebugTraceListener : CustomTraceListener
Enterprise Library2.0(2):Logging Application Block学习_休闲_166
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    
//
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}
 
2.覆写TraceData方法
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlpublic override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_172
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    
if (data is LogEntry && this.Formatter != null
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_176    
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27        
this.WriteLine(this.Formatter.Format(data as LogEntry));
Enterprise Library2.0(2):Logging Application Block学习_休闲_107    }

Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    
else
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_182    
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27        
this.WriteLine(data.ToString());
Enterprise Library2.0(2):Logging Application Block学习_休闲_107    }

Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28}
 
3.覆写Write()WriteLine()方法
Enterprise Library2.0(2):Logging Application Block学习_休闲_188/// <summary>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
/// Writes a message to the debug window 
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
/// </summary>
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
/// <param name="message">The string to write to the debug window</param>

Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlpublic override void Write(string message)
Enterprise Library2.0(2):Logging Application Block学习_休闲_194
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    Debug.Write(message);
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28}

Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
Enterprise Library2.0(2):Logging Application Block学习_休闲_200
/// <summary>
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
/// Writes a message to the debug window 
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27
/// </summary>
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
/// <param name="message">The string to write to the debug window</param>

Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlpublic override void WriteLine(string message)
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_206
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    Debug.WriteLine(message);
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28}
 
七.创建自定义的Formatter
1.在自定义的类上添加特性ConfigurationElementType,并实现接口ILogFormatter
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl[ConfigurationElementType(typeof(CustomFormatterData))]
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl
public class MyFormatter : ILogFormatter
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_213
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    
//
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}
 
2.构造函数接受一个类型为NameValueCollection的参数
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlpublic MyFormatter (NameValueCollection attributes)
Enterprise Library2.0(2):Logging Application Block学习_职场_219
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    
//
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}
 
3.添加Format方法,它接受一个LogEntry的参数
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBlpublic string Format(LogEntry log)
Enterprise Library2.0(2):Logging Application Block学习_休闲_225
{
Enterprise Library2.0(2):Logging Application Block学习_LoggingApplicationBl_27    
//
Enterprise Library2.0(2):Logging Application Block学习_EnterpriseLibrary2.0_28
}

 
八.使用场景
如果你的应用程序需要挟日志到Event Log, E-mail, Database, Message Queue, Windows Management Instrumentation (WMI), TextFile,你就应该考虑使用日志组件来提供这些功能,特别如果你需要基于分类和优先级来过滤日志消息,需要格式化消息,或者需要不改动代码的情况下改变消息的目的地。日志组件同时被设计成可扩展的,包括方便的创建客户订制的FormatterTraceListener
 
参考资料:Enterprise Libaray –January 2006帮助文档