Enterprise Library2.0(2):Logging Application Block学习

摘要: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时记录一个日志项:

LogEntry logEntry = new LogEntry();
logEntry.EventId 
= 100;
logEntry.Priority 
= 2;
logEntry.Message 
= "Informational message";
//只能设置一个
logEntry.Categorys = "UI Events";

Logger.Write(logEntry);

2.0下可以添加多次:

LogEntry logEntry = new LogEntry();
logEntry.EventId 
= 100;
logEntry.Priority 
= 2;
logEntry.Message 
= "Informational message";
//设置多个Category
logEntry.Categories.Add("Trace");        
logEntry.Categories.Add(
"UI Events");

Logger.Write(logEntry);


3.可以在代码中查询哪些日志项将被过滤,例如:

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 currently configured filters.
}

else
{
  
// Event will not be logged. 
}


4.配置文件的改变。在1.1下关于Logging & Instrumentation  Application Block的信息记录在loggingconfiguration.config文件中,2.0下所有的信息都放在了Web.configApp.config中,如:

<configuration>
    
<configSections>
        
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
    
</configSections>
    
<loggingConfiguration tracingEnabled="true" defaultCategory="General">
        
<logFilters>
            
<add
                
name="Category"
                type
="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
                categoryFilterMode
="AllowAllExceptDenied">
        
<categoryFilters>
          
<add name="UI Events" />
        
</categoryFilters>
            
</add>
            
<add
                
name="Priority"
                type
="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging"
                minimumPriority
="2"
                    
/>
      
<add name="LogEnabled Filter"
        type
="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
        enabled
="true" 
           
/>
    
</logFilters>
</loggingConfiguration>
</configuration>

二.记录日志信息

1.添加相关的引用

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;


2.创建一个日志项

LogEntry log = new LogEntry();
log.EventId 
= 300;
log.Message 
= "Sample message";
log.Categories.Add(
"UI Events");
log.Severity 
= TraceEventType.Information;
log.Priority 
= 5;


3.调用Logger.Write()方法

Logger.Write(log);


三.记录日志项的扩展属性

使用基于泛型的Dictionary来记录,如下

// Create the dictionary to hold the extra information, and populate it
// with managed security information.  
Dictionary<stringobject> dictionary = new Dictionary<stringobject>();
ManagedSecurityContextInformationProvider informationHelper 
= new ManagedSecurityContextInformationProvider();

informationHelper.PopulateDictionary(dictionary);

// Add a custom property for screen resolution
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
string resolution = String.Format("{0}x{1}", width, height);

dictionary.Add(
"Screen resolution", resolution);

// Write the log entry that contains the extra information
Logger.Write("Log entry with extra information", dictionary);

 

四.跟踪活动并记录上下文信息

1.调用DoDataAccess方法,完成后释放Trace对象

using (new Tracer("Trace"))
{
    DoDataAccess();
}

 

2.创建DoDataAccess方法

private void DoDataAccess()
{
    
using (new Tracer("Data Access Events"))
    
{
        
// Peform work here

        
// Assume an error condition was detected - perform some troubleshooting.
        DoTroubleShooting();
    }

}

 

3.创建另一个方法DoTroubleShooting,并在其中创建LogEntry

private void DoTroubleShooting()
{
    
string logMessage = "Simulated troubleshooting message for Logging QuickStart. " +
      
"Current activity=\"" + Trace.CorrelationManager.ActivityId + "\"";

    LogEntry logEntry 
= new LogEntry();

    logEntry.Categories.Clear();
    logEntry.Categories.Add(
"Troubleshooting");
    logEntry.Priority 
= 5;
    logEntry.Severity 
= TraceEventType.Error;
    logEntry.Message 
= logMessage;

    Logger.Write(logEntry);
}

 

五.检测日志项是否被记录

创建一个日志项并设置它的信息,调用Logger.ShouldLog()方法

LogEntry logEntry = new LogEntry();
logEntry.Priority 
= 2;
logEntry.Categories.Add(
"Trace");
logEntry.Categories.Add(
"UI Events");

if (Logger.GetFilter<CategoryFilter>().ShouldLog(logEntry))
{
  
// Event will be logged according to currently configured filters.
  
// 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.
}

 

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 currently configured filters.
}

else
{
  
// Event will not be logged. 
}

 

六.创建自定义的Trace Listener

1.添加特性ConfigurationElementType,需要继承自CustomTraceListener

[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class DebugTraceListener : CustomTraceListener
{
    
//
}

 

2.覆写TraceData方法

public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{
    
if (data is LogEntry && this.Formatter != null
    
{
        
this.WriteLine(this.Formatter.Format(data as LogEntry));
    }

    
else
    
{
        
this.WriteLine(data.ToString());
    }

}

 

3.覆写Write()WriteLine()方法

/// <summary>
/// Writes a message to the debug window 
/// </summary>
/// <param name="message">The string to write to the debug window</param>

public override void Write(string message)
{
    Debug.Write(message);
}


/// <summary>
/// Writes a message to the debug window 
/// </summary>
/// <param name="message">The string to write to the debug window</param>

public override void WriteLine(string message)
{
    Debug.WriteLine(message);
}

 

七.创建自定义的Formatter

1.在自定义的类上添加特性ConfigurationElementType,并实现接口ILogFormatter

[ConfigurationElementType(typeof(CustomFormatterData))]
public class MyFormatter : ILogFormatter
{
    
//
}

 

2.构造函数接受一个类型为NameValueCollection的参数

public MyFormatter (NameValueCollection attributes)
{
    
//
}

 

3.添加Format方法,它接受一个LogEntry的参数

public string Format(LogEntry log)
{
    
//
}

 

八.使用场景

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

 

参考资料:Enterprise Libaray –January 2006帮助文档

作者:TerryLee
出处:http://terrylee.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2006-03-22 15:22 TerryLee 阅读(9432) 评论(20) 编辑 收藏

 回复 引用   
#1楼 2006-03-22 17:19 AZ[未注册用户]
不错,正想学习……

多谢了

 回复 引用 查看   
#2楼 2006-03-22 19:20 Aero      
不错,

支持,期待介绍更多的MS Application Block

 回复 引用 查看   
#3楼 2006-03-22 20:29 阿不      
楼主对Enterprise Library 研究很深啊。1.1跟2.0的接口都能记得这么清楚
 回复 引用 查看   
#4楼[楼主] 2006-03-23 08:18 Terrylee      
谢谢几位的支持!

有时间我一定把其它几个Application Block都写完,呵呵~~~

 回复 引用   
#5楼 2006-04-06 17:57 !!![未注册用户]
希望10年后能有楼主现在的水平
 回复 引用 查看   
#6楼 2006-04-08 21:39 阿不      
再补充一点:如果企业库本身出现任何异常,会自动记录错误日志,但如果是运行用户没有足够权限就会抛出安全异常,提示无法访问注册表之类的信息。这种错误在1.1版的解决办法是运行InstallServices.bat安装所需服务。在2.0版中进行了修改可以配置异常时不记录日志,修改配置节值如下:
<instrumentationConfiguration performanceCountersEnabled="false"
eventLoggingEnabled="true" wmiEnabled="false" />
将eventLoggingEnabled="true"改为="false" 就行了。

 回复 引用 查看   
#7楼[楼主] 2006-04-09 08:49 Terrylee      
谢谢阿不的补充,这也是2.0里面对于Logging Application Block修改的地方
 回复 引用 查看   
#8楼 2006-04-17 13:23 梁广永      
期待下文,
重点是DataAccess 那段,

 回复 引用 查看   
#9楼 2006-12-24 23:01 突破自己      
兄弟的文章不错,学习。
 回复 引用 查看   
#10楼[楼主] 2006-12-25 11:39 TerryLee      
@突破自己
:)

 回复 引用 查看   
#11楼 2007-11-29 18:59 RyanYu      
学习了,感谢楼主辛勤付出~