<data name="ActivityStartAlreadyStarted" xml:space="preserve">
<value>"Can not start an Activity that was already started"</value>
</data>
+ <data name="ActivitySetParentAlreadyStarted" xml:space="preserve">
+ <value>"Can not set parent on already started Activity"</value>
+ </data>
<data name="EndTimeNotUtc" xml:space="preserve">
<value>"EndTime is not UTC"</value>
</data>
/// <param name="parentId">The id of the parent operation.</param>
public Activity SetParentId(string parentId)
{
- if (Parent != null)
+ if (_id != null || _spanId != null)
+ {
+ // Cannot set the parent on already started Activity.
+ NotifyError(new InvalidOperationException(SR.ActivitySetParentAlreadyStarted));
+ }
+ else if (Parent != null)
{
NotifyError(new InvalidOperationException(SR.SetParentIdOnActivityWithParent));
}
/// </summary>
public Activity SetParentId(ActivityTraceId traceId, ActivitySpanId spanId, ActivityTraceFlags activityTraceFlags = ActivityTraceFlags.None)
{
- if (Parent != null)
+ if (_id != null || _spanId != null)
+ {
+ // Cannot set the parent on already started Activity.
+ NotifyError(new InvalidOperationException(SR.ActivitySetParentAlreadyStarted));
+ }
+ else if (Parent != null)
{
NotifyError(new InvalidOperationException(SR.SetParentIdOnActivityWithParent));
}
[Fact]
public void SetParentId()
{
- var parent = new Activity("parent");
+ using (var a = new Activity("foo"))
+ {
+ a.Start();
+ string parentId = a.ParentId;
+ a.SetParentId("00-6e76af18746bae4eadc3581338bbe8b1-2899ebfdbdce904b-00"); // Error does nothing
+ Assert.Equal(parentId, a.ParentId);
+ }
+
+ using (var a = new Activity("foo"))
+ {
+ a.Start();
+ string parentId = a.ParentId;
+ a.SetParentId(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()); // Nothing will happen
+ Assert.Equal(parentId, a.ParentId);
+ }
+
+ using var parent = new Activity("parent");
parent.SetParentId(null); // Error does nothing
Assert.Null(parent.ParentId);
Assert.Equal(parent.ParentId, parent.RootId);
parent.Start();
- var child = new Activity("child");
+ using var child = new Activity("child");
child.Start();
Assert.Equal(parent.Id, child.ParentId);
[Fact]
public void IdFormat_W3CWhenTraceIdAndSpanIdProvided()
{
- Activity activity = new Activity("activity3");
+ using Activity activity = new Activity("activity3");
ActivityTraceId activityTraceId = ActivityTraceId.CreateRandom();
activity.SetParentId(activityTraceId, ActivitySpanId.CreateRandom());
activity.Start();