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.

Ff648605.LOG-QS-01(en-us,PandP.10).gif

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.

    using (new Tracer("UI Events"))
    {
      DoDataAccess();  
    }
    
    'Usage
    Using (New Tracer("Trace"))
      DoDataAccess()
    End Using
    
  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 as a nested Tracer object. It is instantiated in 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.

    private void DoDataAccess()
    {
      using (new Tracer("Data Access Events"))
      {
        DoTroubleShooting();
      }
    }
    
    'Usage
    Private Sub DoDataAccess()
      Using (New Tracer("Data Access Events"))
        DoTroubleShooting()
      End Using
    End Sub
    
  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.

    private void DoTroubleShooting()
    {
      string logMessage = "Simulated troubleshooting message for Logging QuickStart. " +
      "Current activity=\"" + Trace.CorrelationManager.ActivityId + "\"";
    
      LogEntry logEntry = new LogEntry();
    
      logEntry.Categories.Clear();
      logEntry.Categories.Add("Troubleshooting");
      logEntry.Priority = 5;
      logEntry.Severity = TraceEventType.Error;
      logEntry.Message = logMessage;
    
      Logger.Write(logEntry);
    }
    
    'Usage
    Private Sub DoTroubleShooting()
    
      Dim logMessage As String = "Simulated troubleshooting message for Logging QuickStart. " & _
              "Current activity=""" & Trace.CorrelationManager.ActivityId.ToString() & """"""
    
      Dim logEntry As New LogEntry()
    
      logEntry.Categories.Clear()
      logEntry.Categories.Add("Troubleshooting")
      logEntry.Priority = 5
      logEntry.Severity = TraceEventType.Information
      logEntry.Severity = TraceEventType.Error
      logEntry.Message = logMessage
    
      Logger.Write(logEntry)
    End Sub           
    
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.