File size: 1,852 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable
#if !UNIX

namespace System.Management.Automation.Tracing
{
    using System;

    /// <summary>
    ///     An object that can be used to revert the ETW activity ID of the current thread
    ///     to its original value.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Etw")]
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Reverter")]
    public interface IEtwActivityReverter :
        IDisposable
    {
        /// <summary>
        ///     Reverts the ETW activity ID of the current thread to its original value.
        /// </summary>
        /// <remarks>
        ///     <para>Calling <see cref="IDisposable.Dispose"/> has the same effect as
        ///         calling this method and is useful in the C# "using" syntax.</para>
        /// </remarks>
        void RevertCurrentActivityId();
    }

    internal class EtwActivityReverter :
        IEtwActivityReverter
    {
        private readonly IEtwEventCorrelator _correlator;
        private readonly Guid _oldActivityId;

        private bool _isDisposed;

        public EtwActivityReverter(IEtwEventCorrelator correlator, Guid oldActivityId)
        {
            _correlator = correlator;
            _oldActivityId = oldActivityId;
        }

        public void RevertCurrentActivityId()
        {
            Dispose();
        }

        public void Dispose()
        {
            if (!_isDisposed)
            {
                _correlator.CurrentActivityId = _oldActivityId;
                _isDisposed = true;

                GC.SuppressFinalize(this);
            }
        }
    }
}

#endif