Exception to log multiple categories in DB?

Topics: Logging Application Block
Jan 23, 2007 at 3:41 PM
I know that the EntLib 2.0 Logging Application Block supports logging multiple categories for a log entry. I haven't had any problem doing this with the logging block explicitly in code. But I'm not sure this is possible using the logging block via the exception handling block.

Using the EntLib Configuration tool when adding a logging handler you can only select one LogCategory. Furthermore in my App.Config XML the category is implemented as an attribute rather than a nested element structure.

<add logCategory="General" eventId="100" severity="Error" title="BC Logging"

Am I missing something here? Did the EntLib development teams not coordinate on this one?

Do I have to go write a custom Hanlder to do this?
Jan 23, 2007 at 6:25 PM
Hi Bruce -

You are correct that the logging handler for the exception block always writes to a single category. We're all one team so it wasn't a matter of co-ordination; rather it was that there wasn't any obvious way of choosing different categories for different exceptions. Since the code calling the exception block shouldn't know if the exception is going to be logged or not, it isn't appropriate for this to be specified in code.

So in the short term you'll probably need to implement this yourself, but if you can describe the behavior you'd like to see in more detail we can look at it for the future.

Jan 23, 2007 at 8:03 PM
Thank you for getting back to me so quickly. I understood why it wasn't appropriate to specify the category in code, that made perfect sense.

My reasoning for wanting to log exceptions to multiple categories is for reporting and querying capabilities. One category logging capabilities are fine if all your categories are mutually exclusive, but suppose I had the following categories -

Suppose my data access component (DAC) uses WMI, file based IO and queries a SQL database to perform its tasks, not so uncommon. Any exception generated from my DAC would belong to the Data category but could belong to either WMI, Database or FileIO categories.

This is all based on reporting and querying/troubleshooting needs.

You could put this information in the error message (similar to your HandlingInstanceID) or query the error message or title for particular namespaces, but querying with LIKE and SUBSTRING functions can be very ugly and inefficient.

I like the idea of the following database query...

Log l,
CategoryLog cl
l.LogID = cl.LogID
AND cl.CategoryID = 2

Or something to this effect.

Or perhaps a more pertinent example would be having a list of applications that access a database as your categories so you could quickly filter/search for all exceptions/logging information pertaining to a particular application your debugging.

It's just the category field seems like such a prime canidate for my where clause being represented as an integer and all in the Log table.

Am I not using the exception handling block/logging block as they were intended to be used? If you know of a simpler alternative using EntLib having similar capabilities please let me know.