Enterprise Libarry 5.0 - Exception block doesn't play nice with the logging block?

Topics: Exception Handling Application Block, Logging Application Block
May 2, 2010 at 5:45 AM

I can't get the exception block and the logging block to work together in enterprise library 5.0?

 

If I use the logging block on its own then it works fine.

 

Also If I use the exception block own its own and just have it wrap or replace an exception then this works fine too.

 

But when I then tell the exception block to use the "Logging Exception Handler" and point it to a category in my logging block, then I get the following error:

 

The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' cannot be resolved. Please verify the spelling is correct or that the full type name is provided.

 

 

Here is the code I'm using:

 

try
{

            // use exception block to log exception
            ExceptionPolicy.HandleException(ex, "MyPolicy");


            // use logging block
            var entry = new LogEntry();
            entry.Message = "my general message";
            entry.Severity = System.Diagnostics.TraceEventType.Information;
            entry.Priority = 1;
            entry.Categories = new List<string> {"General"};
            Logger.Write(entry);

}

catch (System.Exception ex)
{
     Console.WriteLine(ex.Message);
}

 

And below is my app.config with for the blocks...

 

<?xml version="1.0" encoding="utf-8" ?>
<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" />
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="MyPolicy">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="NotifyRethrow">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                priority="0" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="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="c:\rolling.log" formatter="Text Formatter" rollFileExistsBehavior="Increment"
                rollInterval="Day" rollSizeKB="1024" traceOutputOptions="Timestamp" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category">
                <listeners>
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </notProcessed>
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>

 

Any suggestions on how to fix this would be very appreciated. The strange thing is these 2 block work correctly apart, but don't work together? I can't seem to figure this one out?

 

 

May 2, 2010 at 6:26 PM

            // Added this line as a test to cause LoggingExceptionHandler concrete type to be resolved.
            // And it does resolve it successfully without throwing an exxception. The variable "x" is never used.
            var x = new LoggingExceptionHandler("xGeneral", 100, System.Diagnostics.TraceEventType.Information, "title", 1, null, null);
            
            // use exception block to log exception  (Only works if the above line is included.  Very strange indeed?)
            // Previously I could not get this line to execute successfully. I don't know why adding the above line fixed the call
            // to ExceptionPolicy.HandleException(ex, "MyPolicy");  below ?
            ExceptionPolicy.HandleException(ex, "MyPolicy");

            // use logging block (this code works fine)
            var entry = new LogEntry();
            entry.Message = "my general message";
            entry.Severity = System.Diagnostics.TraceEventType.Information;
            entry.Priority = 1;
            entry.Categories = new List<string> { "General" };
            Logger.Write(entry);

 

Why do I have to add...

var x = new LoggingExceptionHandler("xGeneral", 100, System.Diagnostics.TraceEventType.Information, "title", 1, null, null);

above my call to

ExceptionPolicy.HandleException(ex, "MyPolicy");

to get it to work? This is sooo strange? Please help to understand what is going on here.  Thanks!

 

 

 

May 4, 2010 at 1:26 AM

This happens because the ExceptionHandling.Logging assembly does't get copied in your output folder.  Referencing it doesn't automatically copy it to your output folder; it did when you used a class or anything from that assembly.  Make sure you deploy this assembly to your output folder.

Related threads:

http://entlib.codeplex.com/Thread/View.aspx?ThreadId=18926

 http://entlib.codeplex.com/Thread/View.aspx?ThreadId=32287

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

Jun 10, 2010 at 3:25 PM

Adding the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll in the reference of the project will solve the issue.

Aug 6, 2010 at 9:55 PM
Referencing doesn't work. I have EL 5 and VS2010. I do post-build event and it works.