Skip to content

Commit cab3b7a

Browse files
committed
trace2: add error event
Add error event, which will capture exceptions, to Trace2 tracing system.
1 parent d48aaf3 commit cab3b7a

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/shared/Core/Trace2.cs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public enum Trace2Event
2020
Start = 1,
2121
Exit = 2,
2222
ChildStart = 3,
23-
ChildExit = 4
23+
ChildExit = 4,
24+
Error = 5,
2425
}
2526

2627
/// <summary>
@@ -120,6 +121,21 @@ void WriteChildExit(
120121
string filePath = "",
121122
[System.Runtime.CompilerServices.CallerLineNumber]
122123
int lineNumber = 0);
124+
125+
/// <summary>
126+
/// Writes an error as a message to the trace writer.
127+
/// </summary>
128+
/// <param name="errorMessage">The error message to write.</param>
129+
/// <param name="parameterizedMessage">The error format string.</param>
130+
/// <param name="filePath">Path of the file this method is called from.</param>
131+
/// <param name="lineNumber">Line number of file this method is called from.</param>
132+
void WriteError(
133+
string errorMessage,
134+
string parameterizedMessage = null,
135+
[System.Runtime.CompilerServices.CallerFilePath]
136+
string filePath = "",
137+
[System.Runtime.CompilerServices.CallerLineNumber]
138+
int lineNumber = 0);
123139
}
124140

125141
public class Trace2 : DisposableObject, ITrace2
@@ -255,6 +271,34 @@ public void WriteChildExit(
255271
});
256272
}
257273

274+
public void WriteError(
275+
string errorMessage,
276+
string parameterizedMessage = null,
277+
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
278+
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0)
279+
{
280+
// It is possible for an error to be thrown before TRACE2 can be initialized.
281+
// Since certain dependencies are not available until initialization,
282+
// we must immediately return if this method is invoked prior to
283+
// initialization.
284+
if (!_initialized)
285+
{
286+
return;
287+
}
288+
289+
WriteMessage(new ErrorMessage()
290+
{
291+
Event = Trace2Event.Error,
292+
Sid = _sid,
293+
Time = DateTimeOffset.UtcNow,
294+
File = Path.GetFileName(filePath),
295+
Line = lineNumber,
296+
Message = errorMessage,
297+
ParameterizedMessage = parameterizedMessage ?? errorMessage,
298+
Depth = ProcessManager.Depth
299+
});
300+
}
301+
258302
protected override void ReleaseManagedResources()
259303
{
260304
lock (_writersLock)
@@ -796,3 +840,42 @@ protected override string GetEventMessage(Trace2FormatTarget formatTarget)
796840
return sb.ToString();
797841
}
798842
}
843+
844+
public class ErrorMessage : Trace2Message
845+
{
846+
[JsonProperty("msg")]
847+
public string Message { get; set; }
848+
849+
[JsonProperty("format")]
850+
public string ParameterizedMessage { get; set; }
851+
852+
public override string ToJson()
853+
{
854+
return JsonConvert.SerializeObject(this,
855+
new StringEnumConverter(typeof(SnakeCaseNamingStrategy)),
856+
new IsoDateTimeConverter()
857+
{
858+
DateTimeFormat = TimeFormat
859+
});
860+
}
861+
862+
public override string ToNormalString()
863+
{
864+
return BuildNormalString();
865+
}
866+
867+
public override string ToPerformanceString()
868+
{
869+
return BuildPerformanceString();
870+
}
871+
872+
protected override string BuildPerformanceSpan()
873+
{
874+
return EmptyPerformanceSpan;
875+
}
876+
877+
protected override string GetEventMessage(Trace2FormatTarget formatTarget)
878+
{
879+
return Message;
880+
}
881+
}

src/shared/TestInfrastructure/Objects/NullTrace.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public void WriteChildExit(
7878
string filePath = "",
7979
int lineNumber = 0) { }
8080

81+
public void WriteError(string errorMessage,
82+
string parameterizedMessage = null,
83+
string filePath = "",
84+
int lineNumber = 0)
85+
{ }
86+
8187
public string SetSid() { return ""; }
8288

8389
#endregion

0 commit comments

Comments
 (0)