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= "< Timestamp> {timestamp}</timestamp> {newline}& #xA;< Message> {message}</message> {newline}& #xA;< Category> {category}</category> {newline}& #xA;< Priority> {priority}</priority> {newline}& #xA;< Eventid> {eventid}</eventid> {newline}& #xA;< Severity> {severity}</severity> {newline}& #xA;< Title> {title}</title> {newline}& #xA;< Machine> {localmachine}</machine> {newline}& #xA;< APP domain> {Localappdomain}</app domain> {newline}& #xA;< Processid> {localprocessid}</processid> {newline}& #xA;< PROCESS name> {localprocessname}</process name> {newline}& #xA;< THREAD name> {Threadname}</thread name> {newline}& #xA;< WIN32 threadid> {win32threadid}</win32 threadid> {newline}& #xA;< EXTENDED properties> {Dictionary (< Key> {key}</key> -< Value> {value}</value> {newline})} </extended properties> "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 & 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