ActivityContext
A representation that conforms to the W3C TraceContext specification. It contains two identifiers: a TraceId and a SpanId, along with a set of common TraceFlags and system-specific TraceState values.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System.Diagnostics
{
[NullableContext(2)]
[Nullable(0)]
public readonly struct ActivityContext : IEquatable<ActivityContext>
{
public ActivityTraceId TraceId { get; }
public ActivitySpanId SpanId { get; }
public ActivityTraceFlags TraceFlags { get; }
public string TraceState { get; }
public bool IsRemote { get; }
public ActivityContext(ActivityTraceId traceId, ActivitySpanId spanId, ActivityTraceFlags traceFlags, string traceState = null, bool isRemote = false)
{
TraceId = traceId;
SpanId = spanId;
TraceFlags = traceFlags;
TraceState = traceState;
IsRemote = isRemote;
}
public static bool TryParse(string traceParent, string traceState, bool isRemote, out ActivityContext context)
{
if (traceParent == null) {
context = default(ActivityContext);
return false;
}
return Activity.TryConvertIdToContext(traceParent, traceState, isRemote, out context);
}
public static bool TryParse(string traceParent, string traceState, out ActivityContext context)
{
return TryParse(traceParent, traceState, false, out context);
}
[NullableContext(1)]
public static ActivityContext Parse(string traceParent, [Nullable(2)] string traceState)
{
if (traceParent == null)
throw new ArgumentNullException("traceParent");
if (!Activity.TryConvertIdToContext(traceParent, traceState, false, out ActivityContext context))
throw new ArgumentException(System.SR.InvalidTraceParent);
return context;
}
public bool Equals(ActivityContext value)
{
if (SpanId.Equals(value.SpanId) && TraceId.Equals(value.TraceId) && TraceFlags == value.TraceFlags && TraceState == value.TraceState)
return IsRemote == value.IsRemote;
return false;
}
public override bool Equals([NotNullWhen(true)] object obj)
{
if (!(obj is ActivityContext))
return false;
ActivityContext value = (ActivityContext)obj;
return Equals(value);
}
public static bool operator ==(ActivityContext left, ActivityContext right)
{
return left.Equals(right);
}
public static bool operator !=(ActivityContext left, ActivityContext right)
{
return !(left == right);
}
public override int GetHashCode()
{
return HashCode.Combine(TraceId, SpanId, TraceFlags, TraceState);
}
}
}