2009-10-27 59 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> 

包裹处理程序用于您想在另一个包围另一个异常的位置。你可以同时拥有一个日志处理程序和一个包装处理程序(如果需要的话) - 日志处理程序在包装处理程序之前触发。