Last active
November 6, 2020 09:47
ActivitySource sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
namespace activitysample | |
{ | |
class Program | |
{ | |
private static readonly ActivitySource _ActivitySource = new ActivitySource("actsrc1"); | |
static void Main(string[] args) | |
{ | |
var listener = new ActivityListener(); | |
listener.ActivityStarted += (Activity act) => | |
{ | |
// invoked when activity started | |
Console.WriteLine($"activity started: {act.OperationName}"); | |
}; | |
listener.ActivityStopped += (Activity act) => | |
{ | |
// invoked when activity stopped | |
Console.WriteLine($"activity stoppped: {act.OperationName}"); | |
}; | |
listener.ShouldListenTo += (ActivitySource actsrc) => | |
{ | |
// invoked when listening started and added new ActivitySource instance. | |
// if returned true, then begin to watch the ActivitySource events. | |
Console.WriteLine($"shouldlistento: {actsrc.Name}"); | |
return actsrc.Name == "actsrc1"; | |
}; | |
listener.Sample = (ref ActivityCreationOptions<ActivityContext> options) => | |
{ | |
// invoked when before activity starting. | |
// if ActivitySamplingResult.None returned, no activity enabled. | |
// if ActivitySamplingResult.PropagationData returned, activity will be enabled and both IsAllDataRequested and Recorded flags will be false. | |
// if ActivitySamplingResult.AllData returned, activity will be enabled and IsAllDataRequested will be true, Recorded will be false. | |
// if ActivitySamplingResult.AllDataAndRecorded returned, activity will be enabled and IsAllDataRequested will be true, Recorded will be true. | |
Console.WriteLine($"sampling: {options.Name}"); | |
return ActivitySamplingResult.AllDataAndRecorded; | |
}; | |
ActivitySource.AddActivityListener(listener); | |
using (var act1 = _ActivitySource.StartActivity("act1", ActivityKind.Internal)) | |
{ | |
} | |
using (var act2 = _ActivitySource.StartActivity("act2", ActivityKind.Internal)) | |
{ | |
// child Activity, set act2 as parent Activity | |
using (var act3 = _ActivitySource.StartActivity("act2.child", ActivityKind.Internal)) | |
{ | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment