Populating a Log Message with Additional Context Information

Populating a Log Message with Additional Context Information

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.

The LogEntry class defines common properties to hold information common to typical logging scenarios. Developers often need to add context information to log entries. The same type of context information can be required for multiple log entries in the same application or in multiple applications. Because context information can be expensive to gather, certain types of information are not automatically collected. The ExtraInformation providers gather context information that is useful but not always necessary because it is expensive to collect. Examples are stack track information and COM+ information.

Typical Goals

In this scenario, you want to add a collection of related properties to a log entry. The values of the properties are determined by the specific context of the application at the time the log entry is created. For example, you want to add security-related properties such as the Windows identity and authentication status of the current user.

Solution

Use the application block classes that implement IExtraInformationProvider to populate a collection of properties. Use the ExtendedProperties property of the LogEntry class to pass the collection of properties to the Write method of the Logger class.

The Logging Application Block includes the following classes that derive from IExtraInformationProvider:

  • ComPlusInformationProvider. This class populates the collection with the following commonly needed COM+ diagnostic information:

    Activity ID

    Application ID

    Transaction ID

    Direct caller account name

    Original caller account name

  • DebugInformationProvider. This class populates the collection with a property containing the current stack trace information.
  • ManagedSecurityContextInformationProvider. This class populates the collection with the following commonly needed security-related information diagnostic information from the managed runtime:

    Authentication type

    Current identity name

    Authentication status

  • UnmanagedSecurityContextInformationProvider. This class populates the collection with the following commonly needed unmanaged security-related information:

    Current user

    Process account name

QuickStart

For an extended example of how to add additional context information to a log entry, see Walkthrough: Populating a Log Message with Additional Context Information.

Populating a Dictionary with Context Information

The following code shows how to use the ManagedSecurityContextInformationProvider class to populate a dictionary with security-related information.

Dictionary<string, object> dictionary = new Dictionary<string, object>();
ManagedSecurityContextInformationProvider informationHelper = 
  new ManagedSecurityContextInformationProvider();    

informationHelper.PopulateDictionary(dictionary);

You can also add custom properties to the dictionary. The following code shows how to add the current screen resolution to the collection of properties declared in the previous example.

int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
string resolution = String.Format("{0}x{1}", width, height);

dictionary.Add("Screen resolution", resolution);

The following code shows how to add the collection of properties to a LogEntry object.

logEntry.ExtendedProperties = dictionary;

logEntry.ExtendedProperties = dictionary

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:
© 2016 Microsoft