zoukankan      html  css  js  c++  java
  • 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="logJobExecute.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="logerror.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
    eturnError.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="logother.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

  • 相关阅读:
    多线程博文地址 http://www.cnblogs.com/nokiaguy/archive/2008/07/13/1241817.html
    vs2010一运行就报错deven.exe assert failure 解决方法,卸载系统中.netFramework最新版本的(简体中文)
    Lambda语句中创建自定义类型时,也可指定某种特定类型,方法是在new与{}之间写上类型名称
    Win7开始菜单所在目录
    C#中Struct与Class的区别
    Linq语句:三表联查
    用exp、dmp导入导出用户到同一个实例下时,类型type会有问题
    列、约束重命名,原数据不丢失
    CDM中,创建一个或多个组合属性的唯一约束
    EF中新建表和关联表的方法
  • 原文地址:https://www.cnblogs.com/slu182/p/4256206.html
Copyright © 2011-2022 走看看