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)}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"

           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 &amp; 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

你可能感兴趣的:(library)