1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
// <copyright>
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
namespace System.Runtime.Diagnostics
{
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.Interop;
using System.Security;
internal class EventTraceActivity
{
// This field is public because it needs to be passed by reference for P/Invoke
public Guid ActivityId;
static EventTraceActivity empty;
public EventTraceActivity(bool setOnThread = false)
: this(Guid.NewGuid(), setOnThread)
{
}
public EventTraceActivity(Guid guid, bool setOnThread = false)
{
this.ActivityId = guid;
if (setOnThread)
{
SetActivityIdOnThread();
}
}
public static EventTraceActivity Empty
{
get
{
if (empty == null)
{
empty = new EventTraceActivity(Guid.Empty);
}
return empty;
}
}
public static string Name
{
get { return "E2EActivity"; }
}
[Fx.Tag.SecurityNote(Critical = "Critical because the CorrelationManager property has a link demand on UnmanagedCode.",
Safe = "We do not leak security data.")]
[SecuritySafeCritical]
public static EventTraceActivity GetFromThreadOrCreate(bool clearIdOnThread = false)
{
Guid guid = Trace.CorrelationManager.ActivityId;
if (guid == Guid.Empty)
{
guid = Guid.NewGuid();
}
else if (clearIdOnThread)
{
// Reset the ActivityId on the thread to avoid using the same Id again
Trace.CorrelationManager.ActivityId = Guid.Empty;
}
return new EventTraceActivity(guid);
}
[Fx.Tag.SecurityNote(Critical = "Critical because the CorrelationManager property has a link demand on UnmanagedCode.",
Safe = "We do not leak security data.")]
[SecuritySafeCritical]
public static Guid GetActivityIdFromThread()
{
return Trace.CorrelationManager.ActivityId;
}
public void SetActivityId(Guid guid)
{
this.ActivityId = guid;
}
[Fx.Tag.SecurityNote(Critical = "Critical because the CorrelationManager property has a link demand on UnmanagedCode.",
Safe = "We do not leak security data.")]
[SecuritySafeCritical]
void SetActivityIdOnThread()
{
Trace.CorrelationManager.ActivityId = this.ActivityId;
}
}
}
|