大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
Microsoft Enterprise Library 中的Logging模块主要用来记录日志,它可以将日志存储在不同的介质中:文本文件,Windows Event,邮件,MSMQ,DataBase,Xml等等。当然它还提供了扩展功能,通过扩展Logging模块的Listener类,我们就能将日志记录在我们需要的地方了。
虽然Microsoft Enterprise Library很庞大,但是却非常容易使用。接下来我们就来看个简单的例子吧。首先来看看Microsoft Enterprise Library 中Logging 模块的配置信息吧。
配置信息
图1
图2
图3
SimpleDemo
Microsoft Enterprise Library 提供的Listener, Filter, Formatter能满足我们大部分的需求,当然也会有特殊的情况。还好类库为我们提供了扩展接口,我们只有实现这些接口就可以制作属于自己的Listener, Filter, Formatter了。我们来看个简单的DEMO吧:
图4
图5
Custom Listener (自定义监听)
图6
图7
图8
图9
图10
图11
Custom Formatter(自定义格式器)
图12
图13
图14
图15
Custom Filter(自定义过滤器)
图16
图17
图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
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/179915.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...