Enterprise Library 6.0 Semantic Logging Application Block Configuration

使用Enterprise Library 6.0的Logging application 模块,配置步骤如下:

1.Nuget 安装 Enterprise Library Logging模块

   命令行:Install-package EnterpriseLibrary.Logging.

2.配置文件:

  当前基本都是通过Enterprise Library 配置的,但是很不幸,我的总是安装失败,于是自己baidu了一把,然后进行配置,配置如下:

<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,Microsoft.Practices.EnterpriseLibrary.Logging"/>
  </configSections>
   <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
                formatter="Text Formatter"  rollInterval="Day" rollSizeKB="1024"  fileName="App_Data/trace.log"
                maxArchivedFiles="10" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
                template="Timestamp: {timestamp}{newline}&#xA;Title:{title}{newline}&#xA;Severity:{severity}{newline}&#xA;Message: {message}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">               
            </errors>
        </specialSources>
    </loggingConfiguration>

3.简单封装logger,如下:

 public sealed class LogHelper
    {

        private static LogHelper instance = null;
        static readonly object lockobj = new object();

        public static LogHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock(lockobj)
                    {
                        if(instance==null)
                        {
                            instance = new LogHelper();
                        }
                    }
                }
                return instance;
            }
        }

        private LogWriter _logWriter = null;

        private LogHelper()
        {
            LogWriterFactory factory = new LogWriterFactory();
            _logWriter = factory.Create();
        }


        private void LogContent(System.Diagnostics.TraceEventType traceType,string title,string message)
        {
            if(_logWriter.IsLoggingEnabled())
            {
                LogEntry entity = new LogEntry();
                entity.Severity = traceType;
                entity.TimeStamp = DateTime.Now;
                entity.Message = message;
                entity.Title = title;
                _logWriter.Write(entity);

            }
        }
               
        /// <summary>
        /// log Exception infomation
        /// </summary>
        /// <param name="title">Log title</param>
        /// <param name="ex">Exception</param>
        public  void LogException(Exception ex)
        {
            LogContent(System.Diagnostics.TraceEventType.Error, ex.Message, ex.ToString());
        }
        

4.经过测试,写入日志。

 

Enterprise Library 可参考官方文档:http://msdn.microsoft.com/zh-cn/library/dn169621.aspx

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。