if(Logger.GetFilter<LogEnabledFilter>("SuccessFilter").Enabled)

Topics: Logging Application Block
Jan 14, 2009 at 2:21 PM

Quick question.

You are able to define more than one 'Log Enabled Filter' using the configuration tool.

I have done this and have a successfilter and failurefilter. I need to be able to turn on and off independently the logging of successfull processing and failures.

I have found though that when I switch one of the 'Log Enabled Filters' enabled to false all logging is turned off. My code looks like the following.

if(success)
{
        if(Logger.GetFilter<LogEnabledFilter>("SuccessFilter").Enabled)
        {
            LogEntry logentry = new LogEntry("Successfully processed file", "FileProcessing",1,0,TraceEventType.Information,"Success",null );
            Logger.Write(logentry);
        }
}
else
{
        if(Logger.GetFilter<LogEnabledFilter>("FailureFilter").Enabled)
        {
            LogEntry logentry = new LogEntry("Failed to processed file", "FileProcessing",1,0,TraceEventType.Error,"Failure",null );
            Logger.Write(logentry);
        }
}
 
When I step though the code with the SuccessFilter set to enabled and the FailureFilter disabled the code gets into the branch that writes a Success log entry however nothing is logged. When I run the same code with both filters enabled everything works as expected.

I assume therefore that these switches are global. Can anyone confirm this is the case and if so how can I achieve my aims in another way (categories ?)

Regards

Ben Blackmore.

Jan 15, 2009 at 7:39 AM
Hi Ben,

Yes, LogEnabledFilter switch is global. The Config Tool UI allows us to create multiple LogEnabledFilter with different names but it should not be used that way.  It can be a future improvement to limit adding multiple LogEnabledFilter to avoid confusion. :)

For your need, i suggest you do the following:
(1) add 1 LogEnabledFilter to enable logging.
(2)  add a CategoryFilter and add 'FileProcessing' as one of the Categories. Use this CategoryFilter to filter your LogEntry objects.
(3) For your LogEntry objects, ensure that you specify its Category.

This way you dont have to programatically check if logging is enabled. Your new code can be:

if(success)
{
    LogEntry logentry = new LogEntry("Successfully processed file", "FileProcessing",1,0,TraceEventType.Information,"Success",null );
    Logger.Write(logentry);
}
else
{
    LogEntry logentry = new LogEntry("Failed to processed file", "FileProcessing",1,0,TraceEventType.Error,"Failure",null );
    Logger.Write(logentry);
}


Hope this helps.

Rhea Echevarria
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Jan 15, 2009 at 2:43 PM
Rhea

Thanks for your help. I thought this might be the case. Tried the way you suggested and everything is working perfectly.

Regards & Thanks


Ben.