forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEtwEventCorrelator.cs
More file actions
119 lines (104 loc) · 4.62 KB
/
Copy pathEtwEventCorrelator.cs
File metadata and controls
119 lines (104 loc) · 4.62 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#if !UNIX
//-----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace System.Management.Automation.Tracing
{
using System;
using System.Diagnostics.Eventing;
/// <summary>
/// An object that can be used to manage the ETW activity ID of the current thread.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Etw")]
public interface IEtwEventCorrelator
{
/// <summary>
/// Gets or sets the ETW activity ID of the current thread.
/// </summary>
/// <remarks>
/// <para>This method should only be used for advanced scenarios
/// or diagnostics. Prefer using <see cref="StartActivity()" />
/// or <see cref="StartActivity(Guid)" /> instead.</para>
/// </remarks>
Guid CurrentActivityId { get; set; }
/// <summary>
/// Creates and sets a new activity ID for the current thread, optionally correlating
/// the new activity with another activity.
/// </summary>
/// <param name="relatedActivityId">The ID of an existing activity to be correlated with the
/// new activity or <see cref="Guid.Empty" /> if correlation is not desired.</param>
/// <returns>An object which can be used to revert the activity ID of the current thread once
/// the new activity yields control of the current thread.</returns>
IEtwActivityReverter StartActivity(Guid relatedActivityId);
/// <summary>
/// Creates and sets a new activity ID for the current thread. If the current thread
/// has an existing activity ID, it will be correlated with the new activity ID.
/// </summary>
/// <returns>An object which can be used to revert the activity ID of the current thread once
/// the new activity yields control of the current thread.</returns>
IEtwActivityReverter StartActivity();
}
/// <summary>
/// A simple implementation of <see cref="IEtwEventCorrelator" />.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Etw")]
public class EtwEventCorrelator :
IEtwEventCorrelator
{
private readonly EventProvider _transferProvider;
private readonly EventDescriptor _transferEvent;
/// <summary>
/// Creates an <see cref="EtwEventCorrelator" />.
/// </summary>
/// <param name="transferProvider">The <see cref="EventProvider" /> to use when logging transfer events
/// during activity correlation.</param>
/// <param name="transferEvent">The <see cref="EventDescriptor" /> to use when logging transfer events
/// during activity correlation.</param>
public EtwEventCorrelator(EventProvider transferProvider, EventDescriptor transferEvent)
{
if (transferProvider == null)
{
throw new ArgumentNullException("transferProvider");
}
_transferProvider = transferProvider;
_transferEvent = transferEvent;
}
/// <summary>
/// Implements <see cref="IEtwEventCorrelator.CurrentActivityId" />.
/// </summary>
public Guid CurrentActivityId
{
get
{
return EtwActivity.GetActivityId();
}
set
{
EventProvider.SetActivityId(ref value);
}
}
/// <summary>
/// Implements <see cref="IEtwEventCorrelator.StartActivity(Guid)" />.
/// </summary>
public IEtwActivityReverter StartActivity(Guid relatedActivityId)
{
var retActivity = new EtwActivityReverter(this, CurrentActivityId);
CurrentActivityId = EventProvider.CreateActivityId();
if (relatedActivityId != Guid.Empty)
{
var tempTransferEvent = _transferEvent;
_transferProvider.WriteTransferEvent(ref tempTransferEvent, relatedActivityId);
}
return retActivity;
}
/// <summary>
/// Implements <see cref="IEtwEventCorrelator.StartActivity()" />.
/// </summary>
public IEtwActivityReverter StartActivity()
{
return StartActivity(CurrentActivityId);
}
}
}
#endif