這里只是說明在項目中如何配置使用微軟企業庫的日志組件,對數據庫方面的配置請參考其他資料。
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="<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>
需要注意的是:
在工具中只能選一個監聽器,但實際可以有多個監聽器同時監聽。
<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