技术开发 频道

Enterprise Library Step By Step系列(八):日志和监测应用程序块

五.创建和使用自定义Sink

在日志和监测应用应用程序块里面,允许我们自定义一个Sink,而且使用方法要比其它的应用程序块中的自定义简单的多。下面我们具体看一下:

1.添加对应用程序块的引用:

Microsoft.Practices.EnterpriseLibrary.Configuration.dll

Microsoft.Practices.EnterpriseLibrary.Logging.dll

2.添加命名空间:

1using Microsoft.Practices.EnterpriseLibrary.Configuration; 2using Microsoft.Practices.EnterpriseLibrary.Logging; 3using Microsoft.Practices.EnterpriseLibrary.Logging.Sinks; 4using Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.Configuration;

3.编写代码:

我们的自定义Sink要继承LogSink这个基类,同时要重载Initialize()和SendMessageCore()这两个抽象方法

1using System; 2using Microsoft.Practices.EnterpriseLibrary.Configuration; 3using Microsoft.Practices.EnterpriseLibrary.Logging; 4using Microsoft.Practices.EnterpriseLibrary.Logging.Sinks; 5using Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.Configuration; 6 7namespace EnterpriseLogging 8{ 9 /**//// <summary> 10 /// 功能说明:自定义Sink 11 /// </summary> 12 public class ConsoleSink: LogSink 13 { 14 private LoggingConfigurationView configView; 15 16 public override void Initialize(ConfigurationView configurationView) 17 { 18 this.configView = configurationView as LoggingConfigurationView; 19 } 20 21 protected override void SendMessageCore(LogEntry logEntry) 22 { 23 CustomSinkData sinkData; 24 sinkData = (CustomSinkData)
this.configView.GetSinkData(this.ConfigurationName); 25 26 /**//// Delimit each log entry 27 Console.WriteLine((string) sinkData.Attributes["delimiter"]); 28 29 /**//// Write the formatted log entry to the Console 30 Console.WriteLine(FormatEntry(logEntry)); 31 } 32 } 33} 34

4.打开配置工具,并打开配置文件。在Sink节点上,我们选择Custom Sink。同时起名为Console Sink:


5.单击TypeName右边的省略号,打开Type Selector对话框。单击Load an Assembly …,并浏览选择我们工程文件的DLL。最后选择Console Sink类。


6.单击Attributes 打开NameValueItem Collection Editor。单击Add创建一个新的NameValueItem,起名为delimiter,并设置它的Value值(这个值可以随意设置,比如我们设置************************)。


7.选择General,创建一个Destination,并设置下列属性:

·   Formatter = Text Formatter,

·   Name = Console Destination

·   Sink = Console Sink

8.把程序输出的方式设为控制台的方式,运行程序。

六.创建和使用自定义Formatter

1.添加如下的命名空间:

1using System.Globalization; 2using System.IO; 3using System.Xml; 4using Microsoft.Practices.EnterpriseLibrary.Configuration; 5using Microsoft.Practices.EnterpriseLibrary.Logging; 6using Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.Configuration; 7using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;

2.以Hands On Lab里面的XmlFormatter为例,在自定义Formatter时我们需要继承ConfigurationProvider类和实现 IlogFormatter接口,并且需要重载Initialize()这个抽象方法。

1using System; 2using System.Globalization; 3using System.IO; 4using System.Xml; 5using Microsoft.Practices.EnterpriseLibrary.Configuration; 6using Microsoft.Practices.EnterpriseLibrary.Logging; 7using Microsoft.Practices.EnterpriseLibrary.Logging.Distributor.Configuration; 8using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters; 9 10namespace LoggingSink 11{ 12 /**//// <summary> 13 /// 功能说明:自定义Formatter 14 /// </summary> 15 public class XmlFormatter : ConfigurationProvider, ILogFormatter 16 { 17 private LoggingConfigurationView configView = null; 18 19 public override void Initialize(ConfigurationView configurationView) 20 { 21 this.configView = (LoggingConfigurationView) configurationView; 22 } 23 24 public string Format(LogEntry log) 25 { 26 using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture)) 27 { 28 XmlTextWriter w = new XmlTextWriter(sw); 29 w.Formatting = Formatting.Indented; 30 w.Indentation = 2; 31 w.WriteStartDocument(true); 32 w.WriteStartElement("logEntry"); 33 w.WriteAttributeString("Category", log.Category); 34 w.WriteAttributeString("Priority",
log.Priority.ToString(CultureInfo.InvariantCulture));
35 w.WriteElementString("Timestamp", log.TimeStampString); 36 w.WriteElementString("Message", log.Message); 37 w.WriteElementString("EventId",
log.EventId.ToString(CultureInfo.InvariantCulture));
38 w.WriteElementString("Severity",
log.Severity.ToString(CultureInfo.InvariantCulture));
39 w.WriteElementString("Title", log.Title); 40 w.WriteElementString("Machine", log.MachineName); 41 w.WriteElementString("AppDomain", log.AppDomainName); 42 w.WriteElementString("ProcessId", log.ProcessId); 43 w.WriteElementString("ProcessName", log.ProcessName); 44 w.WriteElementString("Win32ThreadId", log.Win32ThreadId); 45 w.WriteElementString("ThreadName", log.ManagedThreadName); 46 w.WriteEndElement(); 47 w.WriteEndDocument(); 48 return sw.ToString(); 49 } 50 } 51 } 52} 53

对于自定义的Formatter我们就介绍到这儿了,它的使用和一般的Formatter没有大的区别,只不过在创建Formatter时我们需要创建一个Custom Formatter,在这里我就不多写了。

 结束语:这篇日志和监测应用程序块的进阶篇文章就到这里了,希望能给初学的朋友一些帮助。后面我会写缓存应用程序块的使用。

0
相关文章