Enterprise Library - Logging Application Block 学习手册(最新版) Part 3

it2022-05-05  91

本文演示Enterprise Library – Logging Application Block 日志管理模块的使用,以及如何创建和使用定制的TraceListener和LogFormatter。本文由 http://blog.entlib.com 开源ASP.NET博客平台小组根据EntLib HOL手册编译提供,欢迎交流。   练习三:创建和使用定制的Log Formatter 本文练习如何创建一个定制的LogFormatter,并应用到logging应用程序中。   1. 首先打开\Enterprise Library 4.1 HOL\CS\Logging\exercises\ex03\begin目录下的EnoughPI.sln项目文件,该应用程序用来计算pi值。   2. 创建定制的Log Formatter 选择Formatters\XmlFormatter.cs 文件,添加如下命名空间的引用: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging; using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration; using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;   添加如下的代码到XmlFormatter类中。     [ConfigurationElementType(typeof(CustomFormatterData))]     public class XmlFormatter : LogFormatter     {         private NameValueCollection Attributes = null;           public XmlFormatter(NameValueCollection attributes)         {             this.Attributes = attributes;         }           public override string Format(LogEntry log)         {             string prefix = this.Attributes["prefix"];             string ns = this.Attributes["namespace"];               using (StringWriter s = new StringWriter())             {                 XmlTextWriter w = new XmlTextWriter(s);                 w.Formatting = Formatting.Indented;                 w.Indentation = 2;                 w.WriteStartDocument(true);                 w.WriteStartElement(prefix, "logEntry", ns);                 w.WriteAttributeString("Priority", ns,                     log.Priority.ToString(CultureInfo.InvariantCulture));                 w.WriteElementString("Timestamp", ns, log.TimeStampString);                 w.WriteElementString("Message", ns, log.Message);                 w.WriteElementString("EventId", ns,                     log.EventId.ToString(CultureInfo.InvariantCulture));                 w.WriteElementString("Severity", ns, log.Severity.ToString());                 w.WriteElementString("Title", ns, log.Title);                 w.WriteElementString("Machine", ns, log.MachineName);                 w.WriteElementString("AppDomain", ns, log.AppDomainName);                 w.WriteElementString("ProcessId", ns, log.ProcessId);                 w.WriteElementString("ProcessName", ns, log.ProcessName);                 w.WriteElementString("Win32ThreadId", ns, log.Win32ThreadId);                 w.WriteElementString("ThreadName", ns, log.ManagedThreadName);                 w.WriteEndElement();                 w.WriteEndDocument();                   return s.ToString();             }         }     } 日志记录将格式化为XML,另外需要传入2个参数,分别为prefix和namespace。 编译整个项目,确保生成定制的log formatter的DLL文件。   3. 应用定制的log formatter 使用EntLib的配置管理工具打开app.config配置文件,选择Logging Application Block | Formatters节点,添加一个新的Custom Formatter,并设置如下属性: Name = Xml Formatter   选择Type属性,点击相应的按钮,显示Type Selector对话框,如下图所示。     选择前一步编译生成的EnoughPI.Logging.dll文件,从EnoughtPI.Logging 程序集中选择XmlFormatter类,然后点击OK按钮。   然后选择Attributes属性,点击后面的按钮,显示EditableKeyValue Collection Editor对话框,如下图所示。添加如下Key/Value值: Key = prefix, Value = x Key = namespace, Value = EnoughPI/2.0     你应该还记得XmlFormatter需要传入2个参数,分别为prefix和namespace,上述操作正是给这两个参数设定参数值。   然后选择 Logging Application Block | Trace Listeners | Custom TraceListener 节点,设置如下属性:Formatter = Xml Formatter     保存所有设置,然后再次运行范例程序,日志记录将再次显示在Console窗口,不过这次在窗口中显示的是XML格式。     现在,我们已经完成了创建和应用Custom Log Formatter。 http://www.entlib.com专业ASP.NET电子商务平台小组,欢迎你继续访问Validation Application Block学习手册。   参考文档: Logging Application Block Hands-On Labs for Enterprise Library

转载于:https://www.cnblogs.com/vibratea/archive/2010/09/15/1826695.html


最新回复(0)