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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Should I add a diagnostic if an
Activitymethod 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 anActivitySourcehas no listeners, then the resulting activity will be null.This also means that any Event or Context method that doesn't take an
Activityas a parameter will use theActivity.Currenttoo 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