共用方式為


EventDataDiagnosticExtensions.ExtractActivity(EventData, String) Method

Definition

Creates Activity based on the tracing context stored in the EventDataThe event received from EventHubOptional Activity nameNew Activity with tracing context

public static System.Diagnostics.Activity ExtractActivity (this Microsoft.Azure.EventHubs.EventData eventData, string activityName = default);
static member ExtractActivity : Microsoft.Azure.EventHubs.EventData * string -> System.Diagnostics.Activity
<Extension()>
Public Function ExtractActivity (eventData As EventData, Optional activityName As String = Nothing) As Activity

Parameters

eventData
EventData
activityName
String

Returns

Examples

async Task ProcessAsync(EventData eventData)
{
   var activity = eventData.ExtractActivity();
   activity.Start();
   Logger.LogInformation($"Event received, Id = {Activity.Current.Id}")
   try 
   {
      // process event
   }
   catch (Exception ex)
   {
        Logger.LogError($"Exception {ex}, Id = {Activity.Current.Id}")
   }
   finally 
   {
        activity.Stop();
        // Activity is stopped, we no longer have it in Activity.Current
        Logger.LogInformation($"Event processed, Id = {activity.Id}, Duration = {activity.Duration}")
   }
}

Note that every log is stamped with Current.Id, that could be used within any nested method call (sync or async) - Current is an ambient context that flows with async method calls.

Remarks

Tracing context is used to correlate telemetry between producer and consumer and represented by 'Diagnostic-Id' and 'Correlation-Context' properties in Properties.

.NET SDK automatically injects context when sending message to the ServiceBus (if diagnostics is enabled by tracing system).

'Diagnostic-Id' uniquely identifies operation that enqueued the event

'Correlation-Context' is comma separated list of string key value pairs representing optional context for the operation.

If there is no tracing context in the event, this method returns Activity without parent.

Returned Activity needs to be started before it can be used (see example below)

Applies to