Fluent interface Exception Handling configuration question...

Topics: Exception Handling Application Block, Logging Application Block
Sep 20, 2012 at 2:08 AM
Edited Sep 20, 2012 at 2:10 AM

Greetings,

I am successfully configuring exception handling using the fluent interface thusly:

builder.ConfigureExceptionHandling()
	.GivenPolicyWithName("LogPolicy")
	.ForExceptionType<Exception>()
		.LogToCategory("Log")
			.WithSeverity(System.Diagnostics.TraceEventType.Warning)
			.UsingEventId(9100);

However I would like to change the title to the exception message text. I understand it would be done with something like this:

.UsingEventId(9100).UsingTitle(ex.Message);
However I am configuring this at start up before any exceptions are thrown so ex is not available. How can I set this up so that it will use the exception's message as the title prior to the exception?
Sep 20, 2012 at 5:27 AM

The properties (fluent or XML) of the LoggingExceptionHandler are set at configuration time.  You can change them but you would have to reconfigure the container each time which is not what you want.  The way to get what you want is to create a new LoggingExceptionHandler that extracts the exception Message in the HandleException method and sets it to the LogEntry title.  Create a custom handler that reuses most of the LoggingExceptionHandler source code.

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

Sep 25, 2012 at 1:32 AM

Okay, I've reviewed the example at the link you posted. However I am at a loss as to how to access the LogEntry title from the custom exception handler.

Sep 25, 2012 at 2:00 AM

Take a look at the existing LoggingExceptionHandler.cs.

In there you will see a public method that handles the exception:

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            WriteToLog(CreateMessage(exception, handlingInstanceId), exception.Data);
            return exception;
        }

The WriteToLog method is where the LogEntry is created:

        protected virtual void WriteToLog(string logMessage, IDictionary exceptionData)
        {
            LogEntry entry = new LogEntry(
                logMessage,
                logCategory,
                minimumPriority,
                eventId,
                severity,
                defaultTitle,
                null);

            foreach (DictionaryEntry dataEntry in exceptionData)
            {
                if (dataEntry.Key is string)
                {
                    entry.ExtendedProperties.Add(dataEntry.Key as string, dataEntry.Value);
                }
            }

            this.logWriter.Write(entry);
        }

So you could change the code to set the title:

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            WriteToLog(exception.Message, CreateMessage(exception, handlingInstanceId), exception.Data);
            return exception;
        }

        protected virtual void WriteToLog(string title, string logMessage, IDictionary exceptionData)
        {
            LogEntry entry = new LogEntry(
                logMessage,
                logCategory,
                minimumPriority,
                eventId,
                severity,
                title ?? defaultTitle,
                null);

            foreach (DictionaryEntry dataEntry in exceptionData)
            {
                if (dataEntry.Key is string)
                {
                    entry.ExtendedProperties.Add(dataEntry.Key as string, dataEntry.Value);
                }
            }

            this.logWriter.Write(entry);
        }

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