Enterprise Library 2.0的数据库跟踪侦听器未登录数据库

时间:2009-10-27 04:39:36

标签: enterprise-library

我正在使用Database TraceListener进行日志记录。我已经执行了CreateLoggingDb.cmd并且记录了数据库。我的web.config如下所示。

当发生激活时,我尝试按以下方式记录。

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#

1 个答案:

答案 0 :(得分:2)

开始寻找的几个地方是:

  1. 您是否使用EntLibConfig.exe构建web.config?这是Enterprise Library附带的exe,您应该在Program Files中的Enterprise Library安装的Bin目录中找到它 - 它为您提供了一个相当直观的图形界面来为应用程序设置企业库:只需将其指向配置为您的应用程序提供文件并设置您想要的东西。

  2. 对于数据库日志记录,您需要使用数据访问应用程序块配置连接字符串 - 您的配置文件中需要另一部分。

  3. 对于您的数据库连接字符串,您使用的是哪种认证模式?运行应用程序的用户标识是否具有对日志记录数据库的写入权限?

  4. 您是否有其他数据库连接?

  5. 另外 - 如果您运行.Net framework 3.5,则应考虑使用企业库的4.1版。

    提供配置文件后编辑

    查看您附加的配置文件(您可能希望删除密码)我认为您缺少一个日志记录异常处理程序 - 您看起来唯一的异常处理程序是一个包装异常处理程序。 / p>

    下面是我的一个异常处理策略之一的示例(来自企业库的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>
    

    在您想要围绕另一个异常包装一个异常的地方使用包装处理程序。如果需要,您可以同时拥有日志处理程序和包装处理程序 - 日志处理程序在包装之前触发。