在本系列的技巧(1技巧(2中分别介绍了使用外部配置文件,使用数据库记录配置信息两种方法,不知道大家有没有想过不使用任何配置文件,也不使用数据库而直接用编程的方法来实现呢?本文将会展示如何使用编程的方法来配置Logging Application Block。首先我们需要了解一下Logging Application Block中比较重要的几个对象:

1LogFormatter

格式化对象,LogFormatterTextFormatterBinaryFormatter两种,多数情况下我们会使用TextFormatter,它通过一个Template来创建,一个完整的Template格式如下:

Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockTimestamp: {timestamp}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockMessage: {message}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockCategory: {category}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockPriority: {priority}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockEventId: {eventid}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockSeverity: {severity}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockTitle:{title}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockMachine: {machine}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockApplication Domain: {appDomain}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockProcess Id: {processId}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockProcess Name: {processName}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockWin32 Thread Id: {win32ThreadId}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockThread Name: {threadName}{newline}
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockExtended Properties: {dictionary({key} - {value})}{newline}


这里就不具体解释每一项的含义,大家可以参考有关文档,示例代码:


Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Blockconst string Template = "Timestamp: {timestamp}{newline}" +
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block                            
"Message: {message}{newline}" +
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block                            
"Category: {category}{newline}" +
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block                            
"Machine: {machine}{newline}";
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockTextFormatter formatter 
= new TextFormatter(Template);

2TraceListener
TraceListener提供了日志记录服务,它指定的是日志将被记录到何处,数据库中或者是文本文件,Enterprise Library提供了7

TraceListenerDatabase TraceListener、Email TraceListener、Flat File TraceListener、Formatter Event Log TraceListener、Msmq TraceListener、System Diagnostics TraceListener、WMI Trace Listener。每一种TraceListener都需要一个LogFormatter来对记录的信息进行格式化,例如创建一个FlatFileTraceListener实例:

Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Blockconst string LogFilePath = @"d:\\share\\messages.log";
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockFlatFileTraceListener logFileListener 
=
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block            
new FlatFileTraceListener(LogFilePath,
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block                                       
"----------",
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block                                       
"----------",
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block                                       formatter);

这里的formatter就是在上面创建的TextFormatter对象。

3LogSource

LogSource其实就是TraceListener的集合,Enterprise Library允许针对不同的日志信息记录到不同地方,因此可以在LogSource中加入多个TraceListener

Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockLogSource mainLogSource =
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block            
new LogSource("MainLogSource", SourceLevels.All);
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block        mainLogSource.Listeners.Add(logFileListener);

4LogFilter

过滤器,对日志信息进行过滤,Enterprise Library默认提供了三种过滤器,用户也可以定义自己的过滤器,示例代码:

Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block// 创建一个类别过滤器
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block

Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockICollection
<string> categoryfilters = new List<string>();
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Blockcategoryfilters.Add(DebugCategory);
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockCategoryFilter categoryFilter 
= new CategoryFilter("CategoryFilter", categoryfilters, CategoryFilterMode.AllowAllExceptDenied);
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block 
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
// 加入类别过滤器到集合中
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block

Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application BlockICollection
<ILogFilter> filters = new List<ILogFilter>();

了解了这四个对象,其实我们就已经知道了该如何去用编程的方法配置Logging Application Block,下面给出一个简单的例子,先写一个MyLogger静态类:

Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Blockusing System;
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
using System.Collections.Generic;
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
using System.Diagnostics;
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
using Microsoft.Practices.EnterpriseLibrary.Logging;
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
public static class MyLogger
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
}

我们再来写一个简单的测试,注意上面的代码中我们过滤掉了Debug类别的日志信息,这样记录到文本文件中的日志信息应该只有My Error一条:

Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Blockpublic partial class _Default : System.Web.UI.Page 
Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
}

文本文件中输出的结果为:

----------

Timestamp: 2006-7-8 3:45:05

Message: My Error

Category: Error

Machine: RJ-097

----------

输出的结果与我们设想的一致,使用编程的方法配置Logging Application Block简单的就介绍到这里,你也可以使用这种方法来配置其他的应用程序块。不过使用编程的方法来配置,失去了EL的灵活性,要知道EL的根本思想就是配置驱动,但是如果掌握了这些,也许你能够更好的使用EL,在CodeProject上有人写了一篇《Plug-in Manager for Logging - Configure MSEL2 On the fly》,有兴趣的朋友不妨参考一下。

 

参考:

http://davidhayden.com/blog/dave/archive/2006/02/18/2805.aspx

http://geekswithblogs.net/akraus1/archive/2006/02/16/69784.aspx

相关文章:

  • 2021-05-27
  • 2021-08-31
  • 2021-11-08
  • 2022-03-09
  • 2022-12-23
  • 2021-07-11
猜你喜欢
  • 2021-11-24
  • 2021-08-11
  • 2021-06-19
  • 2022-01-18
  • 2022-01-02
  • 2022-01-22
相关资源
相似解决方案