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. |
By using the Logging Application Block, you can query the filter status to determine whether 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 of the Logger class, passing to it 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.
}
'Usage
Dim logEntry As New LogEntry()
logEntry.Priority = 2
logEntry.Categories.Add("Trace")
logEntry.Categories.Add("UI Events")
If Logger.ShouldLog(logEntry) Then
' 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.
End If
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 query a specific filter type to determine whether 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.
}
'Usage
Dim categories As ICollection(Of String) = New List(Of String)(0)
categories.Add("Trace")
categories.Add("UI Events")
If Logger.GetFilter(Of CategoryFilter)().ShouldLog(categories) Then
' Events with these categories should be logged.
End If