Log exceptions with entlib 5

Topics: Exception Handling Application Block, Logging Application Block
Sep 30, 2010 at 6:46 AM

I,

With entlib 4.1 I knew how to log exceptions, but with entlib 5 it seems a lot of things have changed, including the configuration tool, so I'm a llitle bit lost now. Someone could tell me how to log exceptions with Entlib 5?

Thanks

Sep 30, 2010 at 8:39 AM

The major difference is as you've noticed is the look of the configuration tool.  Another one would be the suggestion of resolving an instance of ExceptionManager instead of using the static ExceptionPolicy. 

ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

bool rethrow = exManager.HandleException("Exception Policy", ex);

I suggest taking a look also at the Enterprise Library 5.0 Hands on Labs

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 29, 2011 at 11:51 PM

I am also having a problem exceptions in NetTiers 2.3.1 and Enterprise Library 5.0.

When an exception is thrown from inside the generated code, the DomainUtil.HandleException method generates the following exception:

Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ExceptionPolicyImpl, key "ServiceLayerExceptionPolicy" ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl", name = "ServiceLayerExceptionPolicy".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type ExceptionPolicyImpl has multiple constructors of length 2. Unable to disambiguate.

Other people have had this problem and it is covered on another thread in this forum (http://community.codesmithtools.com/nettiers/f/16/p/11381/43559.aspx#43559), but the thread leads to a link to this thread as the solution to the problem.

I don't understand what you mean by "... the suggestion of resolving an instance of ExceptionManager instead of using the static Exception Policy...". Who suggested this to whom? Is this a suggestion that the NetTiers support team should change it? Or that a user (like me) should change the templates to do this?

The generated code to handle exceptions looks like this:

/// <summary>
/// Wraps call to tohe <see cref="ExceptionPolicy"/> class which handles all exceptions based on the security policy.
/// </summary>
public static bool HandleException(Exception e, string policyName)
{
     try
     {
          return ExceptionPolicy.HandleException(e, policyName);
     }
     catch (System.Configuration.ConfigurationErrorsException)
     {
          return true;
     }
}
As a test, I have tried changing the generated code to read as follows:
/// <summary>
/// Wraps call to tohe <see cref="ExceptionPolicy"/> class which handles all exceptions based on the security policy.
/// </summary>

public static bool HandleException(Exception e, string policyName
{
     try
     {
          ExceptionManager exManager = Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();

          bool rethrow = exManager.HandleException(e, policyName);

          return rethrow;
     }
     catch (System.Configuration.ConfigurationErrorsException)
     {
          return true;
     }
}
Is this what you had in mind? When I do this, I just get the following exception instead:
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: The policy with name 'ServiceLayerExceptionPolicy' cannot be found. Exception handling aborted.
And yet I *do* have ServiceLayerExceptionPolicy defined in the Services project (as well as in my test project, because I wasn't sure where it should go).  My config file is as follows:
<?xml version="1.0"?>
<configuration>
<configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<exceptionHandling>
    <exceptionPolicies>
        <add name="ServiceLayerExceptionPolicy">
            <exceptionTypes>
                <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                    postHandlingAction="NotifyRethrow" />
            </exceptionTypes>
        </add>
    </exceptionPolicies>
</exceptionHandling>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

So I am trying to understand, given that I am using NetTiers 2.3.1 with Enterprise Library 5, how I am supposed to program and configure my project so that I don't get a
secondary exception whenever an exception is thrown inside the generated code.
Is this a bug in NetTiers, or am I just using it incorrectly? If it is a bug, will there be a fix for it, or should I try to change the template myself?
If I could get it to work correctly by changing the generated code, I could probably make the change to the template myself and I would be happy to submit the change to you, but I"m not there yet.
Rick.
Jun 30, 2011 at 3:35 AM

Hi Rick,

The suggestion by Sarah is the Enterprise Library 5.0's new way of handling exception by using ExceptionManager object. Honestly, its hard to tell if this will work with .NetTiers since the code is already generated. Modifying it can possibly introduce issues. It is still better to ask this to the .NetTiers forum.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 30, 2011 at 3:35 AM

Hi Rick,

The suggestion by Sarah is the Enterprise Library 5.0's new way of handling exception by using ExceptionManager object. Honestly, its hard to tell if this will work with .NetTiers since the code is already generated. Modifying it can possibly introduce issues. It is still better to ask this to the .NetTiers forum.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com