zoukankan      html  css  js  c++  java
  • 使用Microsoft Enterprise Library 5.0记录日志信息

    下载微软企业库Microsoft Enterprise Library 5.0,并进行安装。

    2.新建一个控制台或ASP.NET应用程序,在程序中添加要引用的dll文件,dll文件位于Microsoft Enterprise Library 5.0安装的目录。

    3.在调用相关的方法记录日志之前,需对配置文件进行配置,右键点击Web.config文件,选择Edit Enterprise Library V5 Configuration。

    4.选择Blocks菜单,点击Add Logging Settings,会出现如下界面。

    5.点击Logging Target Listeners旁边的加号,可以添加记录日志的方式,默认的方式为Event Log Listener,日志记录在Windows系统日志里面,我们可以根据需要添加记录到文件或数据库中等其他方式。

    6.接下来可以设置文件的路径和格式化的文本模板。

    7.修改Text Formatter的Template中的值,可以设置成自己想要的模板。

    8.在Categories目录中,可以设置记录日志的类型,默认为General,还可以添加或修改类型。选择Listeners对应的下拉列表框,可以设置记录的方式。

    9.在设置好所有的信息后,保存相关的信息,配置文件中自动生成相应的配置文件。

    <configuration>

        <configSections>
            <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        </configSections>
        <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
            <listeners>
                <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    source="Enterprise Library Logging" formatter="Text Formatter"
                    log="" machineName="." traceOutputOptions="None" />
                <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    fileName="trace.log" formatter="Text Formatter" traceOutputOptions="None" />
            </listeners>
            <formatters>
                <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Machine: {localMachine}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                    name="Text Formatter" />
            </formatters>
            <categorySources>
                <add switchValue="All" name="General">
                    <listeners>
                        <add name="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">
                    <listeners>
                        <add name="Event Log Listener" />
                    </listeners>
                </errors>
            </specialSources>
        </loggingConfiguration>
        <system.web>
            <compilation debug="true" targetFramework="4.0" />
        </system.web>

    </configuration>

    10.在配置好Web.config文件后,在页面引入命名空间Microsoft.Practices.EnterpriseLibrary.Logging,就可以在代码中调用了。

    IDictionary<string, object> dic = new Dictionary<string, object>();
    dic.Add("用户名:", "张三");
    dic.Add("IP地址:", "192.168.11.12");

    ICollection<string> coll = new List<string>();
    coll.Add("General");

    LogEntry log = new LogEntry();
    log.Message = "日志测试";
    log.TimeStamp = DateTime.Now;
    log.ExtendedProperties = dic;//记录额外的信息
    log.Categories = coll;//设置记录的日志类型

    Logger.Write(log);

    11.最后在trace.log文件中记录相关的日志信息。

  • 相关阅读:
    编译型与解释型、动态语言与静态语言、强类型语言与弱类型语言的区别
    【转】 Oracle 用户权限管理方法
    oracle实例内存(SGA和PGA)调整
    oracle修改内存使用和性能调节,SGA
    下星期计划——9.7~9.13
    医疗数据分析——过高费用的异常检测
    Groovy介绍
    Java 7代码层面上的更新
    Java陷阱之assert关键字
    支持向量机(SVM)、支持向量回归(SVR)
  • 原文地址:https://www.cnblogs.com/roak/p/14249255.html
Copyright © 2011-2022 走看看