Sergey Shishkin

on agile software development

Enterprise Library: Tracing Categories Bug

EntLib Logging Application Block 2.0 disappoints me again and again. ūüė¶ This time I needed to use non-string object as a logical operation identifier at the CorrelationManager.StartLogicalOperation method.

The using scenario "by-design" of course manages the logical operation stack through the EntLibTracer class, which supports only strings as operations identifiers. In my case more flexibility is needed, and I use custom code to manipulate the logical operation stack.

While having tracingEnabled set to true in the configuration, the LogWriter class adds all the logical operation identifiers from the stack to the LogEntry.Categories collection. I find this feature quite controversial, moreover it has an ugly bug!

The private method LogWriter.AddTracingCategories uses foreach loop with string loop variable! Looks like the author of that method never ever read something like "Code Complete" or "Writing Solid Code". Where did he get the confidence that iterating the Stack of objects he will always get strings? :-S Needless to say that the result of this mistake is an InvalidCastException. Here is the fix:

Logging\LogWriter.cs, Line 293, Replace:

              
foreach (string tracingOperation in Trace.CorrelationManager.LogicalOperationStack)
{
    // must take care of logging categories..
    if (!log.Categories.Contains(tracingOperation))
    {
        if (!replacementDone)
        {
            log.Categories = new List<string>(log.Categories);
            replacementDone = true;
        }
        log.Categories.Add(tracingOperation);
    }
}

To:

              
foreach (object operation in Trace.CorrelationManager.LogicalOperationStack)
{
    string tracingOperation = operation.ToString();
    // must take care of logging categories..
    if (!log.Categories.Contains(tracingOperation))
    {
        if (!replacementDone)
        {
            log.Categories = new List<string>(log.Categories);
            replacementDone = true;
        }
        log.Categories.Add(tracingOperation);
    }
}

If you use EntLibTracer class, take care of it as well – it actively exploits the same mistaken assumption. If you do not use it and do not want the operations being populated as Categories for each LogEntry, just disable tracingEnabled in the configuration (and you can leave the LogWriter unfixed then). Happy enterprise development! ūüėČ

Advertisements

Written by Sergey Shishkin

02.10.2006 at 13:24

Posted in Uncategorized

%d bloggers like this: