Microsoft Enterprise Library: Logging 模块

    Microsoft Enterprise Library 中的Logging模块主要用来记录日志,它可以将日志存储在不同的介质中:文本文件,Windows Event,邮件,MSMQ,DataBase,Xml等等。当然它还提供了扩展功能,通过扩展Logging模块的Listener类,我们就能将日志记录在我们需要的地方了。

   虽然Microsoft Enterprise Library很庞大,但是却非常容易使用。接下来我们就来看个简单的例子吧。首先来看看Microsoft Enterprise Library 中Logging 模块的配置信息吧。

配置信息

Microsoft Enterprise Library: Logging 模块_第1张图片

图1

Microsoft Enterprise Library: Logging 模块_第2张图片

图2

Microsoft Enterprise Library: Logging 模块_第3张图片

图3

 SimpleDemo

          Microsoft Enterprise Library 提供的Listener, Filter, Formatter能满足我们大部分的需求,当然也会有特殊的情况。还好类库为我们提供了扩展接口,我们只有实现这些接口就可以制作属于自己的Listener, Filter, Formatter了。我们来看个简单的DEMO吧:

Microsoft Enterprise Library: Logging 模块_第4张图片

图4

Microsoft Enterprise Library: Logging 模块_第5张图片

图5

 

Custom Listener (自定义监听)

Microsoft Enterprise Library: Logging 模块_第6张图片

图6

Microsoft Enterprise Library: Logging 模块_第7张图片

图7

Microsoft Enterprise Library: Logging 模块_第8张图片

图8

Microsoft Enterprise Library: Logging 模块_第9张图片

图9

Microsoft Enterprise Library: Logging 模块_第10张图片

图10

Microsoft Enterprise Library: Logging 模块_第11张图片

图11

 

 

 

Custom Formatter(自定义格式器)

Microsoft Enterprise Library: Logging 模块_第12张图片

图12

Microsoft Enterprise Library: Logging 模块_第13张图片

图13

Microsoft Enterprise Library: Logging 模块_第14张图片

图14

Microsoft Enterprise Library: Logging 模块_第15张图片

图15

 

 

 

Custom Filter(自定义过滤器)

Microsoft Enterprise Library: Logging 模块_第16张图片

图16

Microsoft Enterprise Library: Logging 模块_第17张图片

图17

Microsoft Enterprise Library: Logging 模块_第18张图片

图18

 

使用到的代码

SimpleDemo

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

namespace SimpleLoggingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            LogWriter log = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            log.Write("Hello Microsoft Enterprise Library");
            log.Dispose();
        }
    }
}

CustomListener

using System;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.IO;
using Microsoft.Win32;
using System.Collections.Generic;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

namespace Logging
{
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class CustomListener:CustomTraceListener
    {

        public override void Write(string message)
        {
            WriteMsgInRegistryKey(message);
        }

        public override void WriteLine(string message)
        {
            WriteMsgInRegistryKey(message);
        }

        private void WriteMsgInRegistryKey(string msg)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey software = hklm.OpenSubKey("software", true);
            RegistryKey microsoft = software.OpenSubKey("microsoft", true);
            RegistryKey entryDir = microsoft.CreateSubKey("Entry Dir", RegistryKeyPermissionCheck.ReadWriteSubTree);

            if (entryDir != null)
            {
                int entryNum = entryDir.GetValueNames().Length;
                entryDir.SetValue(string.Format("No{0}", entryNum), msg, RegistryValueKind.String);
                entryDir.Close();
            }
            microsoft.Close();
            software.Close();
           

        }
        public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data)
        {
            if (this.Formatter != null)
            {
                this.Write(this.Formatter.Format(data as LogEntry));
            }
            else
            {
                this.Write(data.ToString());
            }
        }
    }
}


CustomFormatter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;

namespace Logging
{
    [ConfigurationElementType(typeof(CustomFormatterData))]
    public class CustomTextFormatter:ILogFormatter
    {
        private NameValueCollection _Dictionary = null;
        public CustomTextFormatter(NameValueCollection dictionary) 
        {
            this._Dictionary = dictionary;
        }
        
        #region ILogFormatter 成员

        public string Format(LogEntry log)
        {
            return log.ToString() + UtileLibrary.PrintNameValueCollection(_Dictionary) +"\n By Custom Formatter!\n";
        }

        #endregion
    }
}


CustomFilter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;

namespace Logging
{
    [ConfigurationElementType(typeof(CustomLogFilterData))]
    public class CustomLogFilter:ILogFilter
    {
        public CustomLogFilter(NameValueCollection dictionary)
        { 
        
        }

        #region ILogFilter 成员

        public bool Filter(LogEntry log)
        {
            if (log.Message.Contains("fuck"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public string Name
        {
            get { return "GhostHouse's Door"; }
        }

        #endregion
    }
}


你可能感兴趣的:(Microsoft Enterprise Library: Logging 模块)