Use the Microsoft elastic iselibrary (Microsoft enterprise database) log component to write system logs to databases and xml files, and database components.

Source: Internet
Author: User

Use the Microsoft elastic iselibrary (Microsoft enterprise database) log component to write system logs to databases and xml files, and database components.

This section describes how to configure and use the log component of the Microsoft enterprise database in the project. For database configuration, see other materials.

1. Add Microsoft. practices. enterpriseLibrary. data. dll, Microsoft. practices. enterpriseLibrary. logging. dll, Microsoft. practices. enterpriseLibrary. logging. database. dll.

2. Enable the configuration tool entlibconfig.exe of enterpriselibrary.

1) Select "Block-> Add Logging Setting"

2) Click "+" to Add the Logging Target Listeners and select Add Database Trace Listener.

3. Set parameters in the Database Trace Listener, such as Database connection, log storage insertion, classified storage, and text format.

4. Set the Database Connection in "Connection String" in Database Setting
5. Save the configuration file to the project path.

The configured configuration file:

<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>

Note that:

Only one listener can be selected in the tool, but multiple listeners can be monitored at the same time.

<CategorySources>
<Add switchValue = "All" name = "General">
<Listeners>
<Add name = "Database Trace Listener"/>
<Add name = "XML Trace Listener"/>
</Listeners>
</Add>
</CategorySources>


Use in the project:

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 = "Title"; logEntry. message = "test"; logEntry. categories. add ("C # Learning"); logEntry. categories. add ("Microsoft Enterprise Library learning"); Logger. writer. write (logEntry, "General"); Console. writeLine ("log writing completed ! ");}}


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

Sample Code address: http://files.cnblogs.com/qiu2013/ConsoleApplication1.zip


MicrosoftPracticesEnterpriseLibraryDatadll

The main difference is that the connectionStrings configuration section added in the config file of. net 2.0 is used for database link string configuration and does not use its own link string configuration section as before.

Example:
The following is a configuration information for the database application Block in the. net 2.0-Based Enterprise Library.

<Configuration>
<ConfigSections>
<Section name = "dataConfiguration" type = "Microsoft. practices. enterpriseLibrary. data. configuration. databaseSettings, Microsoft. practices. enterpriseLibrary. data, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = null "/>
</ConfigSections>
<DataConfiguration defaultDatabase = "Connection String"/>
<ConnectionStrings>
<Add name = "Connection String" connectionString = "Database = Database; Server = (local) \ SQLEXPRESS; Integrated Security = SSPI ;"
ProviderName = "System. Data. SqlClient"/>
</ConnectionStrings>
</Configuration>

Note:

We can see above
The connectionStrings configuration section is. net 2.0. We define the actual database link string here.

In configSections, we can see that we have created a new dataConfiguration configuration section. In this section, we will specify our default database.

In this way, we can create a default database in the Code as follows:

Database db = DatabaseFactory. CreateDatabase ();
You can also use
Database db = DatabaseFactory. CreateDatabase ("Connection String ");
The next input parameter defines the name of the database link string in the configuration file.

In addition, in the dataConfiguration configuration section, providerMappings can be defined. This part has a lot to do with GenericDatabase, which will be detailed later in the Blog.

When GenericDatabase is used in a database that is not specifically specified, GenericDatabase can be simply understood as a universal database object .... Remaining full text>

Microsoft enterprise database configuration file Error

Let's take a look at the database. config attributes. Have you copied them to the local machine? Go to the bin directory and check them. Are there any of them?

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.