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

本文演示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
 
loggingApplicationBlock_3_1.gif

选择Type属性,点击相应的按钮,显示Type Selector对话框,如下图所示。
 
loggingApplicationBlock_3_2.gif
 
选择前一步编译生成的EnoughPI.Logging.dll文件,从EnoughtPI.Logging 程序集中选择XmlFormatter类,然后点击OK按钮。
 
然后选择Attributes属性,点击后面的按钮,显示EditableKeyValue Collection Editor对话框,如下图所示。添加如下Key/Value值:
Key = prefix, Value = x
Key = namespace, Value = EnoughPI/2.0
 
loggingApplicationBlock_3_3.gif
 
你应该还记得XmlFormatter需要传入2个参数,分别为prefix和namespace,上述操作正是给这两个参数设定参数值。
 
然后选择 Logging Application Block | Trace Listeners | Custom TraceListener 节点,设置如下属性:Formatter = Xml Formatter
 
loggingApplicationBlock_3_4.gif
 
保存所有设置,然后再次运行范例程序,日志记录将再次显示在Console窗口,不过这次在窗口中显示的是XML格式。
 
loggingApplicationBlock_3_5.gif
 
现在,我们已经完成了创建和应用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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Practices.EnterpriseLibrary.Logging 是一个日志记录库,可以帮助开发人员记录应用程序中的异常、警告、信息等信息。要使用 Practices.EnterpriseLibrary.Logging,需要按照以下步骤操作: 1. 在 Visual Studio 中,创建一个新的控制台应用程序或 Web 应用程序。 2. 通过 NuGet 安装 Enterprise Library Logging 包。 3. 在 App.config 或 Web.config 文件中,添加以下配置节: ``` <configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" requirePermission="true" /> </configSections> <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General"> <listeners> <add name="Console Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.ConsoleTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.ConsoleTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging" traceOutputOptions="None" /> </listeners> <categorySources> <add switchValue="All" name="General"> <listeners> <add name="Console Trace Listener" /> </listeners> </add> </categorySources> </loggingConfiguration> ``` 4. 在代码中,使用以下代码记录日志: ``` Logger.Write("This is a message", "General"); ``` 以上是 Practices.EnterpriseLibrary.Logging 的基本使用方法,可以根据实际需求进行配置和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值