@@ -20,7 +20,8 @@ public enum Trace2Event
20
20
Start = 1 ,
21
21
Exit = 2 ,
22
22
ChildStart = 3 ,
23
- ChildExit = 4
23
+ ChildExit = 4 ,
24
+ Error = 5 ,
24
25
}
25
26
26
27
/// <summary>
@@ -120,6 +121,21 @@ void WriteChildExit(
120
121
string filePath = "" ,
121
122
[ System . Runtime . CompilerServices . CallerLineNumber ]
122
123
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 ) ;
123
139
}
124
140
125
141
public class Trace2 : DisposableObject , ITrace2
@@ -255,6 +271,34 @@ public void WriteChildExit(
255
271
} ) ;
256
272
}
257
273
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
+
258
302
protected override void ReleaseManagedResources ( )
259
303
{
260
304
lock ( _writersLock )
@@ -796,3 +840,42 @@ protected override string GetEventMessage(Trace2FormatTarget formatTarget)
796
840
return sb . ToString ( ) ;
797
841
}
798
842
}
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
+ }
0 commit comments