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

 

转载于:https://www.cnblogs.com/photoninfo/p/4156901.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值