Logging application block of Enterprise Library 2.0

Logging application block of Enterprise Library 2.0

 

Posted by: Rickie Lee

http://rickie.cnblogs.com

The logging application block includes three types of filters, Category Filter, Priority Filter and LogEnabledFilter respectively.

 

1. CategoryFilter

The Category Filter is used to filter LogEntry objects by their category collection. It will filter any LogEntry object which category collection contains the specified filtered category.

 

For example,

CategoryFilterExpression: [Allow all except: UI Events]

If a LogEntry object belongs to the [UI Events] category, this object will be filtered.

 

The relevant source code in the CategoryFilter.cs file lists below.

/// <summary>

/// Tests a set of categories against the category filters.

/// </summary>

/// <param name="categories">The set of categories.</param>

/// <returns><b>true</b> if the message passes through the filter and should be logged, <b>false</b> otherwise.</returns>

public bool ShouldLog(IEnumerable<string> categories)

{

       bool matchDetected = false;

       foreach (string category in categories)

       {

              if (this.CategoryFilters.Contains(category))

              {

                     matchDetected = true;

                     break;

              }

       }

 

       return ((CategoryFilterMode.AllowAllExceptDenied == this.CategoryFilterMode) && !matchDetected)

       || ((CategoryFilterMode.DenyAllExceptAllowed == this.CategoryFilterMode) && matchDetected);

}

 

2. PriorityFilter

The PriorityFilter filters LogEntry objects by their priority property. The PriorityFilter has three properties, MaximumPriority, MinimumPriority and Name.

 

But if the MinimumPriority of a LogEntry object has been assigned to a negective value, it will be reassigned to the default minimumPriority value [-1].

 

The relevant source code in the PriorityFilter.cs file is as follows.

/// <summary>

/// Tests a log entry to see if its priority is within the allowed limits.

/// </summary>

/// <param name="priority">Priority to test.</param>

/// <returns>Returns true if the priority passes through the category filter.</returns>

public bool ShouldLog(int priority)

{

       if (priority < 0)

       {

              priority = this.minimumPriority;

       }

       return (this.maximumPriority >= priority && priority >= this.minimumPriority);

}

 

3. LogEnabledFilter

The LogEnabledFilter is a global filter that applies to all LogEntry objects in an application. With it, you can turn on and turn off logging.

 

The following code snippet is retrieved from the LoggingQuickStart application, which is used to check the LogEnabledFilter status.

// ----------------------------------------------------------------------

// Query the logging enabled filter to determine if logging is enabled.

// ----------------------------------------------------------------------

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

{

     // Logging is enabled.

      this.resultsTextBox.Text += "Logging is enabled." + Environment.NewLine;

}

else

{

   // Logging is not enabled. Events will not be logged. Your application can avoid the performance

   // penalty of collecting information for an event that will not be loggeed.

   this.resultsTextBox.Text += "Logging is not enabled." + Environment.NewLine;

}

 

References:

1. Microsoft Enterprise Library 2.0, Logging Application Block, http://msdn.microsoft.com/library/?url=/library/en-us/dnpag2/html/EntLib2.asp

2. LoggingQuickStart sample application.

你可能感兴趣的:(application)