企业库 - 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的主要对象:

1) log entity对象, log entity可以理解为一条log记录;
2) log writer为全局的log 管理对象,包含了所有log相关的操作;

3) log filter可以看成是log writer的属性,用来控制log writer的行为,例如控制log writer是否起作用,是否只对某些优先级的起作用;

4) trace source/catagory source ,用来管理多个和组织多种log记录方式(trace listener),例如一个trace source中可以包含多个记录方式,例如记录到db,且同时给用户发email等。一个log writer可以包含多个trace source。log writer自带有3个特殊的trace listeners:all event log source, unprocessed log source,warning and error log source ;

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如下:

< configuration >
    
< 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 &amp; Warnings"   />
        
</ specialSources >
    
</ loggingConfiguration >
</ configuration >


四 代码示例

using  System;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using  Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

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);
        }


    }
}

 

 

完!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值