Share via


Walkthrough: Tracing Activities and Propagating 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.

This walkthrough demonstrates the steps to trace activities and propagate context information from code. The sequence of method calls with tracing information is shown in Figure 1.

Ff650601.1882638f-8560-4160-873b-e96bdb08fb22(en-us,PandP.10).png

Figure 1
Tracing scenario method calls

The traceButton_Click method does not pass an activity identifier when constructing the initial Tracer object. This means that the Logging Application Block generates a unique activity identifier for the tracing activities. The application block assigns this identifier to the Trace.CorrelationManager.ActivityId property. You can use this activity identifier to correlate all information logged for a particular activity.

To reproduce the demonstration

  1. Create an instance of the Tracer class in your code, passing the string "Trace" as the value for the category. Do not supply a value for the activityId parameter. The constructor of the Tracer object logs the start time of the activity using the Trace category. The listener that will receive the log entry is determined by the configuration settings for the Trace category.
  2. In the scope of the Tracer object, call the method DoDataAccess. Upon return from the call to DoDataAccess, the Tracer object is disposed. The Dispose method of the Tracer object logs the activity end time.
  3. Create a method named DoDataAccess. In this routine, create a Tracer object and pass the string "Data Access Events" as the value for the category. The configuration settings for this category specify the listener that will receive log entries for this Tracer object. The Tracer object is created as a nested Tracer object. It is instantiated within the scope of the Tracer object created in the traceButton_Click method. This means that the log entries created by this Tracer object will inherit the category Trace. In the body of this method, call the method DoTroubleshooting.
  4. Create a method named DoTroubleShooting. In this method, create a new LogEntry object. Create a message for the log entry that includes the current activity identifier. Set the category of the log entry to Troubleshooting. Use the Logger class to write the log entry.