Microsoft Enterprise Library 中的Logging模块主要用来记录日志,它可以将日志存储在不同的介质中:文本文件,Windows Event,邮件,MSMQ,DataBase,Xml等等。当然它还提供了扩展功能,通过扩展Logging模块的Listener类,我们就能将日志记录在我们需要的地方了。
虽然Microsoft Enterprise Library很庞大,但是却非常容易使用。接下来我们就来看个简单的例子吧。首先来看看Microsoft Enterprise Library 中Logging 模块的配置信息吧。
图1
图2
图3
Microsoft Enterprise Library 提供的Listener, Filter, Formatter能满足我们大部分的需求,当然也会有特殊的情况。还好类库为我们提供了扩展接口,我们只有实现这些接口就可以制作属于自己的Listener, Filter, Formatter了。我们来看个简单的DEMO吧:
图4
图5
图6
图7
图8
图9
图10
图11
图12
图13
图14
图15
图16
图17
图18
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(); } } }
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()); } } } }
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 } }
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 } }