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
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.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);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static ActivityContext Parse(string traceParent, [System.Runtime.CompilerServices.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([System.Diagnostics.CodeAnalysis.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()
{
if (this == default(ActivityContext))
return 0;
int num = 5381;
num = (num << 5) + num + TraceId.GetHashCode();
num = (num << 5) + num + SpanId.GetHashCode();
num = (int)((num << 5) + num + TraceFlags);
return (num << 5) + num + ((TraceState != null) ? TraceState.GetHashCode() : 0);
}
}
}