2009-10-27 60 views
1

我正在使用Database TraceListener進行日誌記錄。我已經執行了CreateLoggingDb.cmd並且存在數據庫。我的web.config如下所示。帶企業庫2.0的數據庫跟蹤監聽器沒有登錄數據庫

當一個excpetion發生時,我嘗試記錄下面。

ExceptionPolicy.HandleException(ex, "PayrollException"); 

該代碼可以運行,但不會登錄數據庫表。請讓我知道,如果web.config中的設置不正確或記錄程序必須不同。

<configuration> 
<configSections> 
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
</configSections> 
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
    <add databaseInstanceName="LoggingConn" writeLogStoredProcName="WriteLog" 
     addCategoryStoredProcName="AddCategory" formatter="Text Formatter" 
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" 
     traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" 
     name="Database Trace Listener" /> 

    </listeners> 
    <formatters> 
    <add template="Timestamp: {timestamp} 
     Message: {message} 
     Category: {category} 
     Priority: {priority} 
     EventId: {eventid} 
     Severity: {severity} 
     Title:{title} 
     Machine: {machine} 
     Application Domain: {appDomain} 
     Process Id: {processId} 
     Process Name: {processName} 
     Win32 Thread Id: {win32ThreadId} 
     Thread Name: {threadName} 
     Extended Properties: {dictionary({key} - {value} 
     )}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="Text Formatter"/> 
    </formatters> 
    <categorySources> 
    <add switchValue="All" name="General"> 
    <listeners> 
    <add name="Database TraceListener"/> 
    </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 TraceListener"/> 
    </listeners> 
    </errors> 
    </specialSources> 
</loggingConfiguration> 
<exceptionHandling> 
    <exceptionPolicies> 
    <add name="PayrollException"> 
    <exceptionTypes> 
    <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="NotifyRethrow" name="Exception"> 
     <exceptionHandlers> 
     <add exceptionMessage="Payroll Application Exception" wrapExceptionType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="Wrap Handler"/> 
     </exceptionHandlers> 
    </add> 
    </exceptionTypes> 
    </add> 
    </exceptionPolicies> 
</exceptionHandling> 
<connectionStrings> 
    <add name="Conn" connectionString="Database=Payroll;Server=10.135.158.211;User Id=xanoneappusr;Password=vivekm123;" providerName="System.Data.SqlClient"/> 
    <add name="LoggingConn" connectionString="Database=Logging;Server=10.135.158.211;User Id=xanoneappusr;Password=vivekm123;" providerName="System.Data.SqlClient"/> 
</connectionStrings> 
<appSettings> 
    <add key="PayrollApplication" value="Payroll"/> 
</appSettings> 
<system.web> 
    <compilation debug="true" batch="false"> 
    <assemblies> 
    <add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
    <add assembly="System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation> 
    <roleManager enabled="true"/> 
    <authentication mode="Forms"/> 
    <membership> 
    <providers> 
    <clear/> 
    <add connectionStringName="Conn" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" applicationName="PayrollApplication" name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"/> 
    </providers> 
    </membership> 
</system.web> 
</configuration>#x# 

回答

2

少數地方開始尋找有:

  1. 你使用EntLibConfig.exe打造的web.config?這是Enterprise Library附帶的一個exe文件,您可以在Program Files的Enterprise Library安裝目錄下找到它 - 它爲您提供了一個合理直觀的圖形界面來爲應用程序設置企業庫:只需將它指向配置文件爲您的應用程序文件,並設置你想要的東西。

  2. 對於數據庫日誌記錄,您需要使用數據訪問應用程序塊配置連接字符串 - 您需要在配置文件中爲此添加另一部分。

  3. 對於您的數據庫連接字符串,您使用的是什麼驗證模式?運行應用程序的用戶身份是否具有對日誌記錄數據庫的寫入權限?

  4. 你還有其他的數據庫連接嗎?

另外 - 如果您正在運行.Net Framework 3.5,則應考慮使用企業庫的4.1版。配置文件後

編輯提供

看着你連接(你可能希望通過地清除密碼),我認爲你缺少一個記錄異常處理程序的配置文件 - 唯一的例外你似乎擁有的處理程序是一個包裝異常處理程序。

下面是我的一個異常處理策略之一(來自企業庫4.1,但您的應該類似)的示例。

<exceptionPolicies> 
    <add name="Data Layer Policy"> 
    <exceptionTypes> 
     <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
     postHandlingAction="NotifyRethrow" name="Exception"> 
     <exceptionHandlers> 
      <add logCategory="DataLayer" eventId="100" severity="Error" title="Data Layer Exception" 
      formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      name="Logging Handler" /> 
     </exceptionHandlers> 
     </add> 
    </exceptionTypes> 
    </add> 
</exceptionPolicies> 

包裹處理程序用於您想在另一個包圍另一個異常的位置。你可以同時擁有一個日誌處理程序和一個包裝處理程序(如果需要的話) - 日誌處理程序在包裝處理程序之前觸發。