views:

1068

answers:

2

I am using Database TraceListener for logging. I've executed the CreateLoggingDb.cmd and the Logging Database is in place. My web.config looks like the below.

When an excpetion occurs I try to log it as below.

ExceptionPolicy.HandleException(ex, "PayrollException");

The code runs but it does not log in the database table. Please let me know if the settings in web.config are not correct or the procedure for logging has to be different.

+1  A: 

A few places to start looking are:

  1. Did you use EntLibConfig.exe to build the web.config? This is an exe that comes with Enterprise Library, you should find it in the Bin directory of the Enterprise Library install in Program Files - it gives you a reasonably intuitive graphical interface to set up enterprise library for an application: just point it at the config file for your application and set up the things you want.

  2. For database logging you need to configure your connection strings using the Data Access Application Block - you will need another section in your config file for this.

  3. For your database connection string, what authenticaion mode are you using? Does the user identity running the application have write access to the logging database?

  4. Do you have other database connectivity?

Also - if you are running the .Net framework 3.5 you should consider using version 4.1 of the Enterprise Library.

EDIT after config file supplied

Looking at the config file you've attached (you may want to remove the passwords by the way) I think that you are missing a logging exception handler - the only exception handler you appear to have is a wrap exception handler.

Below is an example of one of one of my exception handling policies (from 4.1 of the Enterprise Library but yours should be similar).

<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>

The wrap handler is used where you want to wrap one exception around another. You can have both a logging handler and a wrap handler if that is needed - the logging handlers fire before the wrapping ones.

David Hall
A: 

Please find below my 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" />

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

</configuration>

Sweta Jha
Sweta, it's common practice to edit your question to supply additional information instead of adding an answer. Also, watch the formatting - it's a bit hard to read.
Tuzo