Urgent Help Please: ExceptionHandling & LoggingHandling

Topics: Exception Handling Application Block, Logging Application Block
Sep 15, 2008 at 12:31 AM
Hi

I'm using the latest enterprise library and I have the configuration file shown below...

 <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="LMS MVP" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="C:\Projectl\_errorsLog\CustomWebPartError.log"
        header="----------------------------------------" footer="----------------------------------------"
        formatter="CustomWebPartLog" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Log Custom WebPart" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="CustomWebPartLog" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="LMS MVP">
        <listeners>
          <add name="Log Custom WebPart" />
        </listeners>
      </add>    
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Log Custom WebPart" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>



 <exceptionHandling>
    <exceptionPolicies>
<add name="Repository Policy">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow" name="Exception">
            <exceptionHandlers>
              <add logCategory="LMS MVP" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Logging Handler" />
              <add exceptionMessage="There is an error occured upon your request. Please contact helpdesk for assistance."
                exceptionMessageResourceType="" wrapExceptionType="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Wrap Handler" />
              <add exceptionMessage="There is an error occured." exceptionMessageResourceType=""
                replaceExceptionType="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Replace Handler" />
            </exceptionHandlers>
          </add>
 </exceptionTypes>
      </add>
 </exceptionPolicies>
  </exceptionHandling>

I tried to run this in a very simple page (on my local machine) with the following code...
protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
             
                throw new Exception("Generated Exception");

            }
            catch (Exception ex)
            {
                ExceptionPolicyImpl exPolicy = ExceptionSetting.ExceptionPolicy("Repository Policy");
                bool rethrow = exPolicy.HandleException(ex);
                if (rethrow)
                    throw;
            }


        }

I set the property UseDefaultLogger to false, it creates multiple flat file...but i just want to log it in one file...so I tried to set it to true but I got the following error...

<title>Unable to handle exception: 'LoggingExceptionHandler'.</title> <style> body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } </style> Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: Unable to handle exception: 'LoggingExceptionHandler'.

Source Error:

Line 35:             {
Line 36:                 ExceptionPolicyImpl exPolicy = ExceptionSetting.ExceptionPolicy("Repository Policy");
Line 37:                 bool rethrow = exPolicy.HandleException(ex);
Line 38:                 if (rethrow)
Line 39:                     throw;


Can someone please help me? What have I done wrong here...?

Thanks in advance

L


Sep 15, 2008 at 2:57 PM
Hi,

What is ExceptionSetting in your code? Can you get more information about the ExceptionHandlingException, such as stack trace or inner exception? You mention setting the use default logger flag; do you get the error only when setting it?

Fernando
Sep 15, 2008 at 11:26 PM
Hi Fsimonazzi

I got this ExceptionSettings to read an external config file, rather than putting together in web config...
Here is the class
using System;
using System.Data;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace TestProject
{
    public class ExceptionSettings
    {
        private static FileConfigurationSource mSource;

        public static FileConfigurationSource ExceptionSource
        {
            get
            {
                mSource = new FileConfigurationSource("_config/logExceptions.config");
                return mSource;
            }
        }

        public static ExceptionPolicyImpl ExceptionPolicy(string policy)
        {
            ExceptionPolicyFactory exceptionFactory = null;
            if (ExceptionSource != null)
                exceptionFactory = new ExceptionPolicyFactory(ExceptionSource);

            ExceptionPolicyImpl eimp = exceptionFactory.Create(policy);


            return eimp;
        }
    }
}

I replicate my problem in a new project so that I can make thing simpler...here is my external config (logException.config) file...
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="C:\Projects\TestProject\TestProject\_errorLog\trace.log"
        header="----------------------------------------" footer="----------------------------------------"
        formatter="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="FlatFile TraceListener" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="FlatFile TraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="FlatFile TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="Exception Policy">
        <exceptionTypes>
          <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow" name="Exception">
            <exceptionHandlers>
              <add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="true" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Logging Handler" />
              <add exceptionMessage="There is an error occurred." exceptionMessageResourceType=""
                wrapExceptionType="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Wrap Handler" />
              <add exceptionMessage="There is an error occured." exceptionMessageResourceType=""
                replaceExceptionType="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Replace Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
</configuration>

and here is my default.aspx.cs
   protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                TestError();
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        private void TestError()
        {
            try
            {
                //int i = 1;
                //int j = 0;
                //int k = i / j;
                throw new Exception("Quick Start Generated Exception");

            }
            catch (Exception ex)
            {
                 ExceptionPolicyImpl exPolicy = ExceptionSettings.ExceptionPolicy("Exception Policy");
                bool rethrow = exPolicy.HandleException(ex);
                if (rethrow)
                    throw;
            }

        }

Yes error occured only when I set UseDefaultLogger to True....if i leave it as default to False, I got duplicate trace.log file which start with the GUID number....

Thank you

Regards
Laytrix