-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathTracing.cs
More file actions
143 lines (123 loc) · 4.5 KB
/
Tracing.cs
File metadata and controls
143 lines (123 loc) · 4.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using GitHub.DistributedTask.Logging;
using GitHub.Runner.Sdk;
namespace GitHub.Runner.Common
{
public sealed class Tracing : ITraceWriter, IDisposable
{
private ISecretMasker _secretMasker;
private TraceSource _traceSource;
/// <summary>
/// The underlying <see cref="System.Diagnostics.TraceSource"/> for this instance.
/// Useful when third-party libraries require a <see cref="System.Diagnostics.TraceSource"/>
/// to route their diagnostics into the runner's log infrastructure.
/// </summary>
public TraceSource Source => _traceSource;
public Tracing(string name, ISecretMasker secretMasker, SourceSwitch sourceSwitch, HostTraceListener traceListener, StdoutTraceListener stdoutTraceListener = null)
{
ArgUtil.NotNull(secretMasker, nameof(secretMasker));
_secretMasker = secretMasker;
_traceSource = new TraceSource(name);
_traceSource.Switch = sourceSwitch;
// Remove the default trace listener.
if (_traceSource.Listeners.Count > 0 &&
_traceSource.Listeners[0] is DefaultTraceListener)
{
_traceSource.Listeners.RemoveAt(0);
}
_traceSource.Listeners.Add(traceListener);
if (stdoutTraceListener != null)
{
_traceSource.Listeners.Add(stdoutTraceListener);
}
}
public void Info(string message)
{
Trace(TraceEventType.Information, message);
}
public void Info(string format, params object[] args)
{
Trace(TraceEventType.Information, StringUtil.Format(format, args));
}
public void Info(object item)
{
string json = JsonConvert.SerializeObject(item, Formatting.Indented);
Trace(TraceEventType.Information, json);
}
public void Error(Exception exception)
{
Trace(TraceEventType.Error, exception.ToString());
var innerEx = exception.InnerException;
while (innerEx != null)
{
Trace(TraceEventType.Error, "#####################################################");
Trace(TraceEventType.Error, innerEx.ToString());
innerEx = innerEx.InnerException;
}
}
// Do not remove the non-format overload.
public void Error(string message)
{
Trace(TraceEventType.Error, message);
}
public void Error(string format, params object[] args)
{
Trace(TraceEventType.Error, StringUtil.Format(format, args));
}
// Do not remove the non-format overload.
public void Warning(string message)
{
Trace(TraceEventType.Warning, message);
}
public void Warning(string format, params object[] args)
{
Trace(TraceEventType.Warning, StringUtil.Format(format, args));
}
// Do not remove the non-format overload.
public void Verbose(string message)
{
Trace(TraceEventType.Verbose, message);
}
public void Verbose(string format, params object[] args)
{
Trace(TraceEventType.Verbose, StringUtil.Format(format, args));
}
public void Verbose(object item)
{
string json = JsonConvert.SerializeObject(item, Formatting.Indented);
Trace(TraceEventType.Verbose, json);
}
public void Entering([CallerMemberName] string name = "")
{
Trace(TraceEventType.Verbose, $"Entering {name}");
}
public void Leaving([CallerMemberName] string name = "")
{
Trace(TraceEventType.Verbose, $"Leaving {name}");
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Trace(TraceEventType eventType, string message)
{
ArgUtil.NotNull(_traceSource, nameof(_traceSource));
_traceSource.TraceEvent(
eventType: eventType,
id: 0,
message: _secretMasker.MaskSecrets(message));
}
private void Dispose(bool disposing)
{
if (disposing)
{
_traceSource.Flush();
_traceSource.Close();
}
}
}
}