Add a diagnostic when an Activity method returns void. #34
Closed
kieronlanning
started this conversation in
Ideas
Replies: 2 comments
-
Confirmed, I'll raise a Diagnostic by default during the following:
System.Diagnostics.ActivitySource outer = new("outer");
System.Diagnostics.ActivitySource inner = new("inner");
ActivitySource.AddActivityListener(new()
{
ShouldListenTo = act => act.Name == "outer",
Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) => ActivitySamplingResult.AllData
});
var outerAct = outer.StartActivity("an-outer-activity", ActivityKind.Consumer);
if (outerAct == null)
{
throw new Exception("`outer` should not be null.");
}
if (Activity.Current?.DisplayName != "an-outer-activity")
{
throw new Exception("`outer` should be the current activity.");
}
var innerAct = inner.StartActivity("an-innter-activity", ActivityKind.Producer, parentId: outerAct?.Id);
if (innerAct != null)
{
throw new Exception("`inner` should be null.");
}
if (Activity.Current?.DisplayName != "an-outer-activity")
{
throw new Exception ("`outer` should be the current activity.");
}
Console.WriteLine("the wrong activity was found."); |
Beta Was this translation helpful? Give feedback.
0 replies
-
Completed in v1.0.8. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Should I add a diagnostic if an
Activity
method returns void?The only way to access the Activity in the future is to use
Activity.Current
. If the previous call to create/ start an activity from anActivitySource
has no listeners, then the resulting activity will be null.This also means that any Event or Context method that doesn't take an
Activity
as a parameter will use theActivity.Current
too potentially associating information with the wrongActivity
. That too should be a diagnostic.Additionally, I could control the diagnostic behaviour through an attribute property, remembering that by default the attributes don't end up in the compiled output.
Beta Was this translation helpful? Give feedback.
All reactions