首页 > 代码库 > EnterpriseLibrary4 自己封装程序集实现log打印
EnterpriseLibrary4 自己封装程序集实现log打印
注意:1)要引用响应的程序集,必须是41的
2)配置文件
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;using Microsoft.Practices.EnterpriseLibrary.Logging;using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;using Microsoft.Practices.EnterpriseLibrary.Logging.Instrumentation;using System.Collections.Generic;using System.Configuration;using System.Diagnostics;
namespace Core{ public class MicroLogger { // Fields private const int DefaultEventId = 1; private const int DefaultPriority = -1; private const TraceEventType DefaultSeverity = TraceEventType.Information; private const string DefaultTitle = ""; private static readonly ICollection<string> emptyCategoriesList = new List<string>(0); private static LogWriterFactory factory = new LogWriterFactory(ConfigurationSourceFactory.Create()); private static object sync = new object(); private static volatile LogWriter writer; //第三步 public static void Write(LogEntry log) { Writer.Write(log); } //第一步 public static void Write(object message) { Write(message, emptyCategoriesList, -1, 1, TraceEventType.Information, "", null); } #region public static void Write(object message, ICollection<string> categories) { Write(message, categories, -1, 1, TraceEventType.Information, "", null); } //第二步 public static void Write(object message, ICollection<string> categories, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties) { LogEntry log = new LogEntry { Message = message.ToString(), Categories = categories, Priority = priority, EventId = eventId, Severity = severity, Title = title, ExtendedProperties = properties }; Write(log); } //第四步 // Properties public static LogWriter Writer { get { if (writer == null) { lock (sync) { if (writer == null) { try { writer = factory.Create(); } catch (ConfigurationErrorsException exception) { TryLogConfigurationFailure(exception); throw; } } } } return writer; } } }}
对应的配置文件,可以通用
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </configSections> <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> <listeners> <add fileName="log\JobExecute.log" footer="----------------------------------------" formatter="Text Formatter" header="----------------------------------------" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Rolling Flat File Trace Listener JobExecute" /> <add fileName="log\error.log" footer="----------------------------------------" formatter="Text Formatter" header="----------------------------------------" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Rolling Flat File Trace Listener Error" /> <add fileName="log\returnError.log" footer="----------------------------------------" formatter="Text Formatter" header="----------------------------------------" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Rolling Flat File Trace Listener ReturnError" /> <add fileName="log\other.log" footer="----------------------------------------" formatter="Text Formatter" header="----------------------------------------" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Rolling Flat File Trace Listener Other" /> <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="YTO.Framework.Logging.Database.DatabaseTraceListener, YTO.Framework.Logging.Database" initializeData="" formatter="Text Formatter" name="SqlServer Trace Listener"/> </listeners> <formatters> <add template="Timestamp: {timestamp(local)}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter" /> </formatters> <categorySources> <add switchValue="All" name="Exception"> <listeners> <add name="Rolling Flat File Trace Listener Error" /> </listeners> </add> <add switchValue="All" name="JobExecute"> <listeners> <add name="Rolling Flat File Trace Listener JobExecute" /> </listeners> </add> <add switchValue="All" name="ReturnError"> <listeners> <add name="Rolling Flat File Trace Listener ReturnError" /> </listeners> </add> </categorySources> <specialSources> <allEvents switchValue="All" name="All Events" /> <notProcessed switchValue="All" name="Unprocessed Category"> <listeners> <add name="Rolling Flat File Trace Listener Other" /> </listeners> </notProcessed> <errors switchValue="All" name="Logging Errors & Warnings"> <listeners> <add name="Rolling Flat File Trace Listener Error" /> </listeners> </errors> </specialSources> </loggingConfiguration></configuration>
参考链接:
http://blog.csdn.net/ghostbear/article/details/8194460
http://tech.ddvip.com/2008-10/122369719476816.html
http://www.cnblogs.com/huangcong/archive/2010/05/31/1748672.html
EnterpriseLibrary4 自己封装程序集实现log打印
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。