Use the Microsoft Enterpriselibrary (Microsoft Enterprise Library) logging component to write system logs to the database and XML files

Source: Internet
Author: User
Tags connectionstrings

This is just a description of how to configure the log components using Microsoft Enterprise Library in the project, please refer to other data for database configuration.

1, add Microsoft.Practices.EnterpriseLibrary.Data.dll, Microsoft.Practices.EnterpriseLibrary.Logging.dll, in the project Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll of these three references.

2. Open the Enterpriselibrary Configuration tool EntLibConfig.exe

1) Select Menu "Block->add Logging Setting"

2) Click "+" to add logging Target Listeners, select Add Database Trace Listener

3. Set parameters in database Trace listener, such as connection of databases, inserting log stored procedure, inserting classification stored procedure, selecting text format, etc.

4. Set the database connection in "Connection String" in database setting
5. Finally save the configuration file to the project path.

After setting up the configuration file:

<Configuration>    <configsections>        < Sectionname= "Loggingconfiguration"type= "Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, version=5.0.414.0, Culture=neutral, publickeytoken= 31bf3856ad364e35 "requirepermission= "true" />        < Sectionname= "Dataconfiguration"type= "Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, version=5.0.414.0, culture=neutral, publickeytoken=31bf3856ad364e35 " requirepermission= "true" />    </configsections>    <loggingconfigurationname=""tracingenabled= "true"defaultcategory= "General">        <Listeners>            <Addname= "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" />            <Addname= "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" />            <Addname= "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>            <Addtype= "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>            <AddSwitchvalue= "All"name= "General">                <Listeners>                    <Addname= "Database Trace Listener" />                    <Addname= "XML Trace Listener" />                </Listeners>            </Add>        </categorysources>        <specialsources>            <alleventsSwitchvalue= "All"name= "All Events" />            <notprocessedSwitchvalue= "All"name= "Unprocessed Category" />            <ErrorsSwitchvalue= "All"name= "Logging Errors &amp; Warnings ">                <Listeners>                    <Addname= "Database Trace Listener" />                    <Addname= "XML Trace Listener" />                </Listeners>            </Errors>        </specialsources>    </loggingconfiguration>    <dataconfigurationDefaultDatabase= "Connection String" />    <connectionStrings>        <Addname= "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>

It is important to note that:

Only one listener can be selected in the tool, but it is actually possible to have multiple listeners listening at the same time.

<Categorysources>
<AddSwitchvalue= "All"Name= "General">
                 <listeners>
                     <add name= "Database Trace Listener" />
                     < add name= "XML Trace Listener "/> < Span style= "color: #0000ff;" >
                 </listeners>
            </ add>
         </categorysources >


To use in a project:

    classProgram {Static voidMain (string[] args) {LogEntry LogEntry=NewLogEntry (); Logentry.eventid=1; Logentry.priority=1; Logentry.severity=System.Diagnostics.TraceEventType.Error; Logentry.title="title"; Logentry.message="Test"; LOGENTRY.CATEGORIES.ADD ("C # Learning"); LOGENTRY.CATEGORIES.ADD ("Microsoft Enterprise Library Learning"); Logger.Writer.Write (LogEntry," General"); Console.WriteLine ("Log write complete!"); }    }


This article refer to the blog address: http://www.cnblogs.com/huangcong/archive/2010/06/04/1751087.html

Example code address: Http://files.cnblogs.com/qiu2013/ConsoleApplication1.zip

Use the Microsoft Enterpriselibrary (Microsoft Enterprise Library) logging component to write system logs to the database and XML files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.