using Microsoft.Extensions.DiagnosticAdapter; public class MyDiagnosticListener { [DiagnosticName("MyEvent")] public void OnMyEvent([ScopedDiagnosticLogicalCallContext] DiagnosticContext context, [MyAttribute("value")] string message) { context.Set("Message", message); } } public class MyAttribute : DiagnosticAttribute { public MyAttribute(string value) { this.Value = value; } public string Value { get; } }
using System.Diagnostics; public class MyDiagnosticSource { private DiagnosticListener listener = new DiagnosticListener("MyDiagnosticSource"); public void SendData(int data) { using (var scope = listener.StartActivity("SendData", data)) { scope.AddAttribute("MyAttribute", "value"); // send data } } }In this example, the `MyDiagnosticSource` class creates a new `DiagnosticListener` and starts an activity with the `StartActivity` method. Custom attributes are added to the scope using the `AddAttribute` method. The package library for the DiagnosticScope class is part of the Microsoft.Extensions.DiagnosticAdapter package.