How to Extend Logging Application Block 4.0

Topics: Exception Handling Application Block, Logging Application Block
Feb 16, 2009 at 5:17 PM
I am using Logging and Exception Handling Block of Enterprise Library 4.0 to log the exceptions of my application in database. So far i am good, but i want to customize the Text Formattor(Template), so that i can have my own things to log the exception. For ex: I want to add "Source" in Template(of Text Formattor) as one of the property to get the Application name at the time of logging exception explicitly in one of the column in Database.
Please suggest me and if possible provide me a code.
Feb 16, 2009 at 8:16 PM
I am working on something similar; essentially adding information to the LogEntry that gets created for an exception.

The issue I see is that you don't have an Exception object anymore once you get to the Logging Application Block (therefore, no Exception.Source property). My thought is to create an Exception Handler called something like "AddInformationHandler", where you could add this information to the Data property of the Exception. If you are using the LoggingExceptionHandler, it already takes everything in the Exception's Data dictionary and adds it to the LogEntry's ExtendedProperties. From there, the information can be read by the existing logging formatters.

I don't have any code examples, but hopefully that all makes sense.

An easier option, since you say you want to log the application name, could be to just set this in the formatter's template property of your config file (i.e. hard-code it). Depends on if you have multiple config files (one per application).
Feb 17, 2009 at 2:56 AM
You are correct JCapriotti.  The key is to create a custom exception handler and add the additional info in the exception's Data collection.  You will then see those additional info in the Extended properties in the log.  You can also explicitly create an entry for it in the formatter, see example here -

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.