Description
openedon Apr 22, 2023
Description
I suspect that there is an issue with the way equality is implemented in ActivityTraceId
. If the activity.TraceId has a default value and we use either the ==
operator or Equals
method to compare it with the default ActivityTraceId
, it always seems to returns false. This behavior is unexpected and can lead to bugs in applications that rely on this comparison.
Using F12 on Visual Studio to see the definition of ActivityTraceId
, I noticed that it internally uses _hexString to calculate HashCode and for Equals
and ==
implementation. When we use default
, the _hexString is uninitialized which causes this comparison to fail.
Please call out if I am doing or expecting something unreasonable. Thanks.
Reproduction Steps
- Create an
Activity
object with a defaultActivityTraceId
. - Compare the
activity.TraceId
with the defaultActivityTraceId
using the==
operator orEquals
method. - Observe that the comparison always returns
false
, even though theTraceId
property of the activity is set to the default value.
Here is a simple xUnit Test to demonstrate the same. (My library targets .NET Standard 2.1 and uses .NET 6.)
[Fact]
public void CompareTraceIdWithDefault()
{
Activity.TraceIdGenerator = () => default;
using var activity = new Activity("my-activity").Start();
// All of these fail.
Assert.Equal(default, activity.TraceId);
Assert.True(default(ActivityTraceId) == activity.TraceId);
Assert.True(default(ActivityTraceId).Equals(activity.TraceId));
// This works.
Assert.Equal(default(ActivityTraceId).ToString(), activity.TraceId.ToString());
}
Expected behavior
When comparing the activity.TraceId
with the default ActivityTraceId
using the ==
operator or Equals
method, the comparison should return true if the activity.TraceId
has the default value.`
Actual behavior
When comparing the activity.TraceId
with the default ActivityTraceId
using the ==
operator or Equals
method, the comparison always returns false
, even if the activity.TraceId
has the default value.
Regression?
No response
Known Workarounds
Use ToString()
and then compare.
Configuration
- Which version of .NET is the code running on?
.NET 6 (6.0.408) - What OS and version, and what distro if applicable?
Windows 10. - What is the architecture (x64, x86, ARM, ARM64)?
x64 - Do you know whether it is specific to that configuration?
Don't think so.
Other information
No response