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:



            // 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"};


catch (System.Exception ex)


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


<?xml version="1.0" encoding="utf-8" ?>
        <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" />
            <add name="MyPolicy">
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                            <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" />
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
            <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" />
            <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" />
            <add switchValue="All" name="General">
                    <add name="Rolling Flat File Trace Listener" />
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category">
                    <add name="Rolling Flat File Trace Listener" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                    <add name="Rolling Flat File Trace Listener" />


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" };


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:



Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

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.