企业库 - Logging block
一 Logging Block
Logging是几乎所有的程序中必不可缺少的功能。Logging可以帮助我们调试程序,在后台程序或者执行很耗时的程序,Logging可以帮助我们来记录程序是否正确运行,是否有异常抛出等。
Logging通常包含的功能有记录Log到不同的地方,还能够很灵活的控制是否需要Log。企业库的Logging Block为我们提供了所有的这些功能,通过企业库我们可以很灵活的在app.config/web.app中切换不同的Log方式,Logging提供了以下的方式来Log记录:
1)The event log
2)An e-mail message
3)A database
4)A message queue
5)A text file
6)A Windows® Management Instrumentation (WMI) event
7)Custom locations using application block extension points
二 Logging Block的主要对象和执行过程
Logging Block的主要对象:
3) log filter可以看成是log writer的属性,用来控制log writer的行为,例如控制log writer是否起作用,是否只对某些优先级的起作用;
5) trace listener: 表示log的记录方式,自带的有db listener,email listener等;
6) log formatter: 用来定义log的记录格式和内容;
Logging Block的执行过程:
1) client构造log entity,然后传递给log writer;
2) log writer使用log filter来过滤log entity,只有没有被过滤的log entity才真正被记录;
3) log writer将log entity传递给所有的trace source;
4) trace source将log entity传递给管理的trace listener;
5) trace listener按照设置的log formatter来记录log;
logging block的执行过程如下图:
三 Logging Block的配置
可以使用企业库自带的配置工具来生产相应的配置文件,
例如以上的log writer的配置:
设置log是否可以使用的logging enable filter和只处理priority为0到5的logging priority filter;
包含了一个名字为General的trace source;
general的trace source包含了一个rolling flat file trace listener;
rolling flat file trace listener使用text formatter来记录log;
配置后的xml如下:
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
Microsoft.Practices.EnterpriseLibrary.Logging,
Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
formatter="Text Formatter" rollInterval="Day" rollSizeKB="1024"
maxArchivedFiles="10" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp}{newline} Message: {message}{newline} Category: {category}{newline} Priority: {priority}{newline} EventId: {eventid}{newline} Severity: {severity}{newline} Title:{title}{newline} Machine: {localMachine}{newline} App Domain: {localAppDomain}{newline} ProcessId: {localProcessId}{newline} Process Name: {localProcessName}{newline} Thread Name: {threadName}{newline} Win32 ThreadId:{win32ThreadId}{newline} Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
</formatters>
<logFilters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
enabled="true" name="Logging Enabled Filter" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
maximumPriority="5" name="Priority Filter" />
</logFilters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Rolling Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings" />
</specialSources>
</loggingConfiguration>
</configuration>
四 代码示例
using Microsoft.Practices.EnterpriseLibrary.Logging;
namespace MyCommon
{
public sealed class MyLogger
{
# region Static
static MyLogger instance = null;
static readonly object padlock = new object();
public static MyLogger Instance
{
get
{
if (instance == null)
{
lock (padlock)
{
if (instance == null)
{
instance = new MyLogger();
}
}
}
return instance;
}
}
#endregion
private LogWriter lw = null;
private MyLogger()
{
lw = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
}
private void Log(string msg, TraceEventType tet)
{
if (lw.IsLoggingEnabled())
{
LogEntry le = new LogEntry();
le.Message = msg;
le.Severity = tet;
lw.Write(le);
}
}
public void Critical(string msg)
{
this.Log(msg, TraceEventType.Critical);
}
public void Error(string msg)
{
this.Log(msg, TraceEventType.Error);
}
public void Warning(string msg)
{
this.Log(msg, TraceEventType.Warning);
}
public void Info(string msg)
{
this.Log(msg,TraceEventType.Information);
}
}
}
完!