Export (0) Print
Expand All

Checking Filter Status Before Constructing Log Messages

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

With the Logging Application Block, you can query the filter status to determine if a log message should be logged according to the filter configuration.

Typical Goals

In this scenario, you want to avoid collecting context information for a log message if the current application block configuration indicates that the information will not be logged.

Solution

Create a LogEntry object that contains the information that will be submitted to the Logging Application Block. Call the ShouldLog method on the Logger class, passing the LogEntry object.

The following code shows how to create a LogEntry object and use the ShouldLog method.

LogEntry logEntry = new LogEntry();
logEntry.Priority = 2;
logEntry.Categories.Add("Trace");
logEntry.Categories.Add("UI Events");

if (Logger.ShouldLog(logEntry))
{
  // Event will be logged according to current configuration.
// Perform operations (possibly expensive) to gather additional information 
  // for the event to be logged. 
}
else
{
  // Event will not be logged. Your application can avoid the performance
  // penalty of collecting information for an event that will not be
  // logged.
}


QuickStart

For an extended example of how to use the Filter class, see Walkthrough: Checking Filter Status Before Constructing Log Messages

Usage Notes

You can check query a specific filter type to determine if a log message should be logged according to the configuration settings for that filter type.

The following code shows how to query the CategoryFilter.

ICollection<string> categories = new List<string>(0);
categories.Add("Trace");
categories.Add("UI Events");

if (Logger.GetFilter<CategoryFilter>().ShouldLog(categories))
{
  // Events with these categories should be logged. 
}

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.
Show:
© 2014 Microsoft