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="<TIMESTAMP> {timestamp}</TIMESTAMP> {newline}
<MESSAGE> {message}</MESSAGE>{newline}
<CATEGORY>{category}</CATEGORY>{newline}
<PRIORITY>{priority}</PRIORITY>{newline}
<EVENTID>{eventid}</EVENTID>{newline}
<SEVERITY>{severity}</SEVERITY>{newline}
<TITLE>{title}</TITLE>{newline}
<MACHINE>{localMachine}</MACHINE>{newline}
<APP DOMAIN> {localAppDomain}</APP DOMAIN>{newline}
<PROCESSID>{localProcessId}</PROCESSID>{newline}
<PROCESS NAME> {localProcessName}</PROCESS NAME> {newline}
<THREAD NAME> {threadName}</THREAD NAME>{newline}
<WIN32 THREADID>{win32ThreadId}</WIN32 THREADID>{newline}
<EXTENDED PROPERTIES> {dictionary(<KEY>{key}</KEY> - <VALUE>{value}</VALUE>{newline})}</EXTENDED PROPERTIES>" 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 & 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?