• 使用Microsoft EnterpriseLibrary(微软企业库)日志组件把系统日志写入数据库和xml文件


    这里只是说明在项目中如何配置使用微软企业库的日志组件,对数据库方面的配置请参考其他资料。

    1、在项目中添加Microsoft.Practices.EnterpriseLibrary.Data.dll、Microsoft.Practices.EnterpriseLibrary.Logging.dll、Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll这三个引用。

    2、打开EnterpriseLibrary的配置工具EntLibConfig.exe

    1)选择菜单“Block->Add Logging Setting"

    2)点击“+”号添加Logging Target Listeners,选择Add Database Trace Listener

    3、设置Database Trace Listener中的参数,比如数据库连接、插入日志存储过程、插入分类存储过程、选择文本格式等

    4、设置Database Setting中的“Connection String”中的数据库连接
    5、最后保存配置文件到项目路径中。

    设置后的配置文件:

    <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" />
            <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, 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="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    databaseInstanceName="Connection String" writeLogStoredProcName="EL_WRITELOG"
                    addCategoryStoredProcName="EL_ADDCATEGORY" formatter="Text Formatter" />
                <add name="XML Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    fileName="test.xml" />
            </listeners>
            <formatters>
                <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    template="&lt;TIMESTAMP&gt; {timestamp}&lt;/TIMESTAMP&gt; {newline}&#xA;&lt;MESSAGE&gt; {message}&lt;/MESSAGE&gt;{newline}&#xA;&lt;CATEGORY&gt;{category}&lt;/CATEGORY&gt;{newline}&#xA;&lt;PRIORITY&gt;{priority}&lt;/PRIORITY&gt;{newline}&#xA;&lt;EVENTID&gt;{eventid}&lt;/EVENTID&gt;{newline}&#xA;&lt;SEVERITY&gt;{severity}&lt;/SEVERITY&gt;{newline}&#xA;&lt;TITLE&gt;{title}&lt;/TITLE&gt;{newline}&#xA;&lt;MACHINE&gt;{localMachine}&lt;/MACHINE&gt;{newline}&#xA;&lt;APP DOMAIN&gt; {localAppDomain}&lt;/APP DOMAIN&gt;{newline}&#xA;&lt;PROCESSID&gt;{localProcessId}&lt;/PROCESSID&gt;{newline}&#xA;&lt;PROCESS NAME&gt; {localProcessName}&lt;/PROCESS NAME&gt; {newline}&#xA;&lt;THREAD NAME&gt; {threadName}&lt;/THREAD NAME&gt;{newline}&#xA;&lt;WIN32 THREADID&gt;{win32ThreadId}&lt;/WIN32 THREADID&gt;{newline}&#xA;&lt;EXTENDED PROPERTIES&gt; {dictionary(&lt;KEY&gt;{key}&lt;/KEY&gt; - &lt;VALUE&gt;{value}&lt;/VALUE&gt;{newline})}&lt;/EXTENDED PROPERTIES&gt;"
                    name="Text Formatter" />
            </formatters>
            <categorySources>
                <add switchValue="All" name="General">
                    <listeners>
                        <add name="Database Trace Listener" />
                        <add name="XML 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="Database Trace Listener" />
                        <add name="XML Trace Listener" />
                    </listeners>
                </errors>
            </specialSources>
        </loggingConfiguration>
        <dataConfiguration defaultDatabase="Connection String" />
        <connectionStrings>
            <add name="Connection String" connectionString="DATA SOURCE=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = eifoclog)));PERSIST SECURITY INFO=True;USER ID=FOC;Password=foc"
                providerName="System.Data.OracleClient" />
        </connectionStrings>
    </configuration>

    需要注意的是:

    在工具中只能选一个监听器,但实际可以有多个监听器同时监听。

            <categorySources>
                <add switchValue="All" name="General">
                    <listeners>
                        <add name="Database Trace Listener" />
                        <add name="XML Trace Listener" />

                    </listeners>
                </add>
            </categorySources>


    在项目中使用:

        class Program
        {
            static void Main(string[] args)
            {
                LogEntry logEntry = new LogEntry();
                logEntry.EventId = 1;
                logEntry.Priority = 1;
                logEntry.Severity = System.Diagnostics.TraceEventType.Error;
                logEntry.Title = "标题";
                logEntry.Message = "test";
                logEntry.Categories.Add("C#学习");
                logEntry.Categories.Add("Microsoft Enterprise Library学习");
    
                Logger.Writer.Write(logEntry, "General");
                Console.WriteLine("日志写入完成!");
            }
        }


    本文参考博客地址:http://www.cnblogs.com/huangcong/archive/2010/06/04/1751087.html

     示例代码地址:http://files.cnblogs.com/qiu2013/ConsoleApplication1.zip

  • 相关阅读:
    路飞学城系列:第2章 数据类型&文件操作-学习笔记【2.1-2.34-列表细讲、元组、字符串细讲、字典、集合、二进制与字符编码、16进制、HASH、为何dict字典查询速度快、用py操作文件、file类的其它必用功能等】
    路飞学城系列:第1章 Python基础语法入门-学习笔记(4)-补充【变量的创建、身份运算、空值None、三元运算】
    路飞学城系列:第2章 数据类型&文件操作-练习(3)【模拟登陆-等待优化ing-读写不能同时进行ing】
    路飞学城系列:第2章 数据类型&文件操作-练习(2)【文件操作:全局替换程序】
    路飞学城系列:第2章 数据类型&文件操作-练习(1)【列表index()方法的操作练习】
    路飞学城系列:第1章 Python基础语法入门-作业(1)【双色球选购-中文-自由发挥】
    Java泛型的理解
    LeetCode 力扣 99. 恢复二叉搜索树
    深入理解Java虚拟机---栈,堆,方法区
    java中API补充总结
  • 原文地址:https://www.cnblogs.com/qiu2013/p/3948131.html
Copyright © 2020-2023  润新知