@@ -29,14 +29,9 @@ namespace System.Net
29
29
// Usage:
30
30
// - Operations that may allocate (e.g. boxing a value type, using string interpolation, etc.) or that may have computations
31
31
// at call sites should guard access like:
32
- // if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this, refArg1, valueTypeArg2); // entering an instance method with a value type arg
33
32
// if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, $"Found certificate: {cert}"); // info logging with a formattable string
34
33
// - Operations that have zero allocations / measurable computations at call sites can use a simpler pattern, calling methods like:
35
- // NetEventSource.Enter(this); // entering an instance method
36
34
// NetEventSource.Info(this, "literal string"); // arbitrary message with a literal string
37
- // NetEventSource.Enter(this, refArg1, regArg2); // entering an instance method with two reference type arguments
38
- // NetEventSource.Enter(null); // entering a static method
39
- // NetEventSource.Enter(null, refArg1); // entering a static method with one reference type argument
40
35
// Debug.Asserts inside the logging methods will help to flag some misuse if the DEBUG_NETEVENTSOURCE_MISUSE compilation constant is defined.
41
36
// However, because it can be difficult by observation to understand all of the costs involved, guarding can be done everywhere.
42
37
// - NetEventSource.Fail calls typically do not need to be prefixed with an IsEnabled check, even if they allocate, as FailMessage
@@ -103,7 +98,7 @@ public static void Enter(object? thisOrContextObject, FormattableString? formatt
103
98
{
104
99
DebugValidateArg ( thisOrContextObject ) ;
105
100
DebugValidateArg ( formattableString ) ;
106
- if ( Log . IsEnabled ( ) ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
101
+ if ( IsEnabled ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
107
102
}
108
103
109
104
/// <summary>Logs entrance to a method.</summary>
@@ -115,7 +110,7 @@ public static void Enter(object? thisOrContextObject, object arg0, [CallerMember
115
110
{
116
111
DebugValidateArg ( thisOrContextObject ) ;
117
112
DebugValidateArg ( arg0 ) ;
118
- if ( Log . IsEnabled ( ) ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } )") ;
113
+ if ( IsEnabled ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } )") ;
119
114
}
120
115
121
116
/// <summary>Logs entrance to a method.</summary>
@@ -129,7 +124,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
129
124
DebugValidateArg ( thisOrContextObject ) ;
130
125
DebugValidateArg ( arg0 ) ;
131
126
DebugValidateArg ( arg1 ) ;
132
- if ( Log . IsEnabled ( ) ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } , { Format ( arg1 ) } )") ;
127
+ if ( IsEnabled ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } , { Format ( arg1 ) } )") ;
133
128
}
134
129
135
130
/// <summary>Logs entrance to a method.</summary>
@@ -145,7 +140,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
145
140
DebugValidateArg ( arg0 ) ;
146
141
DebugValidateArg ( arg1 ) ;
147
142
DebugValidateArg ( arg2 ) ;
148
- if ( Log . IsEnabled ( ) ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } , { Format ( arg1 ) } , { Format ( arg2 ) } )") ;
143
+ if ( IsEnabled ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } , { Format ( arg1 ) } , { Format ( arg2 ) } )") ;
149
144
}
150
145
151
146
[ Event ( EnterEventId , Level = EventLevel . Informational , Keywords = Keywords . EnterExit ) ]
@@ -163,7 +158,7 @@ public static void Exit(object? thisOrContextObject, FormattableString? formatta
163
158
{
164
159
DebugValidateArg ( thisOrContextObject ) ;
165
160
DebugValidateArg ( formattableString ) ;
166
- if ( Log . IsEnabled ( ) ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
161
+ if ( IsEnabled ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
167
162
}
168
163
169
164
/// <summary>Logs exit from a method.</summary>
@@ -175,7 +170,7 @@ public static void Exit(object? thisOrContextObject, object arg0, [CallerMemberN
175
170
{
176
171
DebugValidateArg ( thisOrContextObject ) ;
177
172
DebugValidateArg ( arg0 ) ;
178
- if ( Log . IsEnabled ( ) ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , Format ( arg0 ) . ToString ( ) ) ;
173
+ if ( IsEnabled ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , Format ( arg0 ) . ToString ( ) ) ;
179
174
}
180
175
181
176
/// <summary>Logs exit from a method.</summary>
@@ -189,7 +184,7 @@ public static void Exit(object? thisOrContextObject, object arg0, object arg1, [
189
184
DebugValidateArg ( thisOrContextObject ) ;
190
185
DebugValidateArg ( arg0 ) ;
191
186
DebugValidateArg ( arg1 ) ;
192
- if ( Log . IsEnabled ( ) ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , $ "{ Format ( arg0 ) } , { Format ( arg1 ) } ") ;
187
+ if ( IsEnabled ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , $ "{ Format ( arg0 ) } , { Format ( arg1 ) } ") ;
193
188
}
194
189
195
190
[ Event ( ExitEventId , Level = EventLevel . Informational , Keywords = Keywords . EnterExit ) ]
@@ -207,7 +202,7 @@ public static void Info(object? thisOrContextObject, FormattableString? formatta
207
202
{
208
203
DebugValidateArg ( thisOrContextObject ) ;
209
204
DebugValidateArg ( formattableString ) ;
210
- if ( Log . IsEnabled ( ) ) Log . Info ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
205
+ if ( IsEnabled ) Log . Info ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
211
206
}
212
207
213
208
/// <summary>Logs an information message.</summary>
@@ -219,7 +214,7 @@ public static void Info(object? thisOrContextObject, object? message, [CallerMem
219
214
{
220
215
DebugValidateArg ( thisOrContextObject ) ;
221
216
DebugValidateArg ( message ) ;
222
- if ( Log . IsEnabled ( ) ) Log . Info ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
217
+ if ( IsEnabled ) Log . Info ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
223
218
}
224
219
225
220
[ Event ( InfoEventId , Level = EventLevel . Informational , Keywords = Keywords . Default ) ]
@@ -237,7 +232,7 @@ public static void Error(object? thisOrContextObject, FormattableString formatta
237
232
{
238
233
DebugValidateArg ( thisOrContextObject ) ;
239
234
DebugValidateArg ( formattableString ) ;
240
- if ( Log . IsEnabled ( ) ) Log . ErrorMessage ( IdOf ( thisOrContextObject ) , memberName , Format ( formattableString ) ) ;
235
+ if ( IsEnabled ) Log . ErrorMessage ( IdOf ( thisOrContextObject ) , memberName , Format ( formattableString ) ) ;
241
236
}
242
237
243
238
/// <summary>Logs an error message.</summary>
@@ -249,7 +244,7 @@ public static void Error(object? thisOrContextObject, object message, [CallerMem
249
244
{
250
245
DebugValidateArg ( thisOrContextObject ) ;
251
246
DebugValidateArg ( message ) ;
252
- if ( Log . IsEnabled ( ) ) Log . ErrorMessage ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
247
+ if ( IsEnabled ) Log . ErrorMessage ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
253
248
}
254
249
255
250
[ Event ( ErrorEventId , Level = EventLevel . Error , Keywords = Keywords . Default ) ]
@@ -268,7 +263,7 @@ public static void Fail(object? thisOrContextObject, FormattableString formattab
268
263
// Don't call DebugValidateArg on args, as we expect Fail to be used in assert/failure situations
269
264
// that should never happen in production, and thus we don't care about extra costs.
270
265
271
- if ( Log . IsEnabled ( ) ) Log . CriticalFailure ( IdOf ( thisOrContextObject ) , memberName , Format ( formattableString ) ) ;
266
+ if ( IsEnabled ) Log . CriticalFailure ( IdOf ( thisOrContextObject ) , memberName , Format ( formattableString ) ) ;
272
267
Debug . Fail ( Format ( formattableString ) , $ "{ IdOf ( thisOrContextObject ) } .{ memberName } ") ;
273
268
}
274
269
@@ -282,7 +277,7 @@ public static void Fail(object? thisOrContextObject, object message, [CallerMemb
282
277
// Don't call DebugValidateArg on args, as we expect Fail to be used in assert/failure situations
283
278
// that should never happen in production, and thus we don't care about extra costs.
284
279
285
- if ( Log . IsEnabled ( ) ) Log . CriticalFailure ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
280
+ if ( IsEnabled ) Log . CriticalFailure ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
286
281
Debug . Fail ( Format ( message ) . ToString ( ) , $ "{ IdOf ( thisOrContextObject ) } .{ memberName } ") ;
287
282
}
288
283
@@ -311,7 +306,7 @@ public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, [Calle
311
306
[ NonEvent ]
312
307
public static void DumpBuffer ( object ? thisOrContextObject , byte [ ] buffer , int offset , int count , [ CallerMemberName ] string ? memberName = null )
313
308
{
314
- if ( Log . IsEnabled ( ) )
309
+ if ( IsEnabled )
315
310
{
316
311
if ( offset < 0 || offset > buffer . Length - count )
317
312
{
@@ -343,7 +338,7 @@ public static unsafe void DumpBuffer(object? thisOrContextObject, IntPtr bufferP
343
338
Debug . Assert ( bufferPtr != IntPtr . Zero ) ;
344
339
Debug . Assert ( count >= 0 ) ;
345
340
346
- if ( Log . IsEnabled ( ) )
341
+ if ( IsEnabled )
347
342
{
348
343
var buffer = new byte [ Math . Min ( count , MaxDumpSize ) ] ;
349
344
fixed ( byte * targetPtr = buffer )
@@ -369,7 +364,7 @@ public static void Associate(object first, object second, [CallerMemberName] str
369
364
{
370
365
DebugValidateArg ( first ) ;
371
366
DebugValidateArg ( second ) ;
372
- if ( Log . IsEnabled ( ) ) Log . Associate ( IdOf ( first ) , memberName , IdOf ( first ) , IdOf ( second ) ) ;
367
+ if ( IsEnabled ) Log . Associate ( IdOf ( first ) , memberName , IdOf ( first ) , IdOf ( second ) ) ;
373
368
}
374
369
375
370
/// <summary>Logs a relationship between two objects.</summary>
@@ -383,7 +378,7 @@ public static void Associate(object? thisOrContextObject, object first, object s
383
378
DebugValidateArg ( thisOrContextObject ) ;
384
379
DebugValidateArg ( first ) ;
385
380
DebugValidateArg ( second ) ;
386
- if ( Log . IsEnabled ( ) ) Log . Associate ( IdOf ( thisOrContextObject ) , memberName , IdOf ( first ) , IdOf ( second ) ) ;
381
+ if ( IsEnabled ) Log . Associate ( IdOf ( thisOrContextObject ) , memberName , IdOf ( first ) , IdOf ( second ) ) ;
387
382
}
388
383
389
384
[ Event ( AssociateEventId , Level = EventLevel . Informational , Keywords = Keywords . Default , Message = "[{2}]<-->[{3}]" ) ]
@@ -396,7 +391,7 @@ private void Associate(string thisOrContextObject, string? memberName, string fi
396
391
[ Conditional ( "DEBUG_NETEVENTSOURCE_MISUSE" ) ]
397
392
private static void DebugValidateArg ( object ? arg )
398
393
{
399
- if ( ! Log . IsEnabled ( ) )
394
+ if ( ! IsEnabled )
400
395
{
401
396
Debug . Assert ( ! ( arg is ValueType ) , $ "Should not be passing value type { arg ? . GetType ( ) } to logging without IsEnabled check") ;
402
397
Debug . Assert ( ! ( arg is FormattableString ) , $ "Should not be formatting FormattableString \" { arg } \" if tracing isn't enabled") ;
@@ -406,9 +401,12 @@ private static void DebugValidateArg(object? arg)
406
401
[ Conditional ( "DEBUG_NETEVENTSOURCE_MISUSE" ) ]
407
402
private static void DebugValidateArg ( FormattableString ? arg )
408
403
{
409
- Debug . Assert ( Log . IsEnabled ( ) || arg == null , $ "Should not be formatting FormattableString \" { arg } \" if tracing isn't enabled") ;
404
+ Debug . Assert ( IsEnabled || arg == null , $ "Should not be formatting FormattableString \" { arg } \" if tracing isn't enabled") ;
410
405
}
411
406
407
+ public static new bool IsEnabled =>
408
+ Log . IsEnabled ( ) ;
409
+
412
410
[ NonEvent ]
413
411
public static string IdOf ( object ? value ) => value != null ? value . GetType ( ) . Name + "#" + GetHashCode ( value ) : NullInstance ;
414
412
0 commit comments