Table of Contents

Class MessageExtensions

Namespace
Microsoft.Azure.ServiceBus.Diagnostics
Assembly
Microsoft.Azure.ServiceBus.dll
public static class MessageExtensions
Inheritance
MessageExtensions
Inherited Members

Methods

ExtractActivity(Message, string)

Creates Activity based on the tracing context stored in the MessageOptional Activity nameNew Activity with tracing context

public static Activity ExtractActivity(this Message message, string activityName = null)

Parameters

message Message
activityName string

Returns

Activity

Examples

async Task ProcessAsync()
{
   var message = await messageReceiver.ReceiveAsync();
   var activity = message.ExtractActivity();
   activity.Start();
   Logger.LogInformation($"Message received, Id = {Activity.Current.Id}")
   try 
   {
      // process message
   }
   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, let's user activity now
        Logger.LogInformation($"Message processed, Id = {activity.Id}, Duration = {activity.Duration}")
   }
}
    Note that every log is stamped with <xref href="System.Diagnostics.Activity.Current" data-throw-if-not-resolved="false"></xref>.Id, that could be used within 
    any nested method call (sync or async) - <xref href="System.Diagnostics.Activity.Current" data-throw-if-not-resolved="false"></xref> 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 UserProperties.

.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 message

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

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

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