标题: Enterprise Library2.0(2):Logging Application Block学习
- Terrylee 2006-03-22 15:22 阅读:7253
- 评论:20 查看评论 | 添加评论
[图片]
摘要:Logging Application Block可以使开发人员在其应用程序中集成日志监测功能,看看随着2.0的推出给我们带来了哪些改变。
一.改进的地方
1.Logging 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.可以在代码中查询哪些日志项将被过滤,例如:
4.配置文件的改变。在1.1下关于Logging & Instrumentation Application Block的信息记录在loggingconfiguration.config文件中,2.0下所有的信息都放在了Web.config或App.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()方法
三.记录日志项的扩展属性
使用基于泛型的Dictionary来记录,如下
[图片]// Create the dictionary to hold the extra information, and populate it
[图片]// with managed security information.
[图片]Dictionary<string, object> dictionary = new Dictionary<string, object>();
[图片]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对象
2.创建DoDataAccess方法
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.
[图片]}
六.创建自定义的Trace Listener
1.添加特性ConfigurationElementType,需要继承自CustomTraceListener
[图片][ConfigurationElementType(typeof(CustomTraceListenerData))]
[图片]public class DebugTraceListener : CustomTraceListener
[图片][图片][图片]{
[图片] //
[图片]}
2.覆写TraceData方法