Cannot log into database

Topics: Logging Application Block
Nov 14, 2012 at 4:22 AM

We are using Enterprise Lib 5.0 and .Net 4. For normal logic or system exception, we'll log into database. If it is SQL connection issue, we log into a flat file. But somehow the database logging is not working. The funny thing is if I grant IIS_IUSRS the permission to the folder of text log, the database logging is working again. I don't understand if there is any underlying connection between the flat file logging and database logging.

We have other applications also using the same logging strategy. Those apps never have issues of logging. We don't need to grant permission to IIS_IUSRS to text log folder. The only difference is other apps are using .NET 3.5.

I much appreciate your help.

Nov 15, 2012 at 3:50 AM

Based on your description, I would guess that you have a category that is configured to write to multiple trace listeners: first a file followed by a database.  When writing to multiple trace listeners if the one fails then all subsequent trace listeners will not fire. 

I have a blog post, Enterprise Library Logging Extensions – Part 3, which gives one approach to work around this behavior.

If this isn't your issue, can you provide more information (e.g. configuration) about the exact scenario?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Nov 16, 2012 at 12:09 AM
Edited Nov 16, 2012 at 12:10 AM

Thanks for your reply. Here is the logging and exception handling part in our config.

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="Default">
    <listeners>
      <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="Logging" writeLogStoredProcName="WriteLog"
        addCategoryStoredProcName="AddCategory" formatter="Default Text Formatter"
        traceOutputOptions="None" filter="All" />
      <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        StoredProc="USP_WriteLog" Database="Logging" type="XXX.Web.ExceptionHandling.XXXDatabaseTraceListener, XXX.Web.ExceptionHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="XXXDatabaseTraceListener" formatter="XXX Text Formatter" />
      <add name="XXXEmailTraceListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        toAddress="webteamsupport@XXX.com.au" fromAddress="XXX-web-dev@XXX.com.au"
        subjectLineStarter="Web application error attention required:"
        smtpServer="XXX" formatter="XXX Text Formatter" useSSL="false"
        traceOutputOptions="DateTime, Timestamp, Callstack" filter="Critical" />
      <add name="XXX Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="D:\Logs\rolling.log" footer="---------------End of  Log-----------------------"
        formatter="Default Text Formatter" header="---------------Start of Log----------------------"
        rollInterval="Month" rollSizeKB="1024" timeStampPattern="dd-MM-yyyy"
        maxArchivedFiles="5" filter="Error" />
    </listeners>
    <formatters>
      ......
    </formatters>
    <categorySources>
      <add switchValue="All" name="Default">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="XXXDBLog">
        <listeners>
          <add name="XXXDatabaseTraceListener" />
        </listeners>
      </add>
      <add switchValue="All" name="EmailAlert">
        <listeners>
          <add name="XXXEmailTraceListener" />
        </listeners>
      </add>
      <add switchValue="All" name="TextLog">
        <listeners>
          <add name="XXX Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="DataAccessPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="DataAccessLoggingHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="TextLog" eventId="100" severity="Error" title="XXX Web Exception Handling"
                formatterType="XXX.Web.ExceptionHandling.CustomTextExceptionFormatter, XXX.Web.ExceptionHandling"
                priority="0" />
              <add name="DataAccessReplaceHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="An unknown error has occurred in Data Access Layer while processing your request. Please call XXX on 13 13 34 with Error Token ID {handlingInstanceID}."
                replaceExceptionType="XXX.Web.ExceptionHandling.DataAccessException, XXX.Web.ExceptionHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="BusinessLogicPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="BusinessLogicLoggingHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="XXXDBLog" eventId="100" severity="Error" title="XXX Web Exception Handling"
                formatterType="XXX.Web.ExceptionHandling.CustomTextExceptionFormatter, XXX.Web.ExceptionHandling"
                priority="0" />
              <add name="BusinessLogicReplaceHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="An unknown error has occurred in Business Logic Layer while processing your request. Please call XXX on 13 13 34 with Error Token ID {handlingInstanceID}."
                replaceExceptionType="XXX.Web.ExceptionHandling.BusinessLogicException, XXX.Web.ExceptionHandling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

Nov 16, 2012 at 3:33 AM

I don't see anything in the configuration file that looks like it could cause the behavior you are seeing.  Each category only has one trace listener associated with it.  I do notice that there is no errors special source configured.  You should configure that to capture any errors that may occur.

Can you create a project that reproduces the issue?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Nov 18, 2012 at 10:43 PM

Can you explain further what it means by 'no errors special source configured'? Thank you.

Nov 18, 2012 at 10:53 PM

The special sources will appear in the config file as a child of logging <loggingConfiguration>:

<specialSources>
    <allEvents switchValue="All" name="All Events" />
    <notProcessed switchValue="All" name="Unprocessed Category" />
    <errors switchValue="All" name="Logging Errors & Warnings">
      <listeners>
        <add name="Flat File Trace Listener" />
      </listeners>
    </errors>
  </specialSources>

It is the destination to write messages to if there is an error in the block.  If it is not set errors will be swallowed so you won't know if you are having an issue logging (and what that issue is!).  If you use the configuration tool it will complain that the special sources is not found (Required attribute 'specialSources' not found.).

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com