@@ -29,8 +29,8 @@ 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.IsEnabled) NetEventSource.Enter(this, refArg1, valueTypeArg2); // entering an instance method with a value type arg
33
- // if (NetEventSource.IsEnabled) NetEventSource.Info(null, $"Found certificate: {cert}"); // info logging with a formattable string
32
+ // if (NetEventSource.Log. IsEnabled() ) NetEventSource.Enter(this, refArg1, valueTypeArg2); // entering an instance method with a value type arg
33
+ // if (NetEventSource.Log. IsEnabled() ) NetEventSource.Info(null, $"Found certificate: {cert}"); // info logging with a formattable string
34
34
// - Operations that have zero allocations / measurable computations at call sites can use a simpler pattern, calling methods like:
35
35
// NetEventSource.Enter(this); // entering an instance method
36
36
// NetEventSource.Info(this, "literal string"); // arbitrary message with a literal string
@@ -103,7 +103,7 @@ public static void Enter(object? thisOrContextObject, FormattableString? formatt
103
103
{
104
104
DebugValidateArg ( thisOrContextObject ) ;
105
105
DebugValidateArg ( formattableString ) ;
106
- if ( IsEnabled ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
106
+ if ( Log . IsEnabled ( ) ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
107
107
}
108
108
109
109
/// <summary>Logs entrance to a method.</summary>
@@ -115,7 +115,7 @@ public static void Enter(object? thisOrContextObject, object arg0, [CallerMember
115
115
{
116
116
DebugValidateArg ( thisOrContextObject ) ;
117
117
DebugValidateArg ( arg0 ) ;
118
- if ( IsEnabled ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } )") ;
118
+ if ( Log . IsEnabled ( ) ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } )") ;
119
119
}
120
120
121
121
/// <summary>Logs entrance to a method.</summary>
@@ -129,7 +129,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
129
129
DebugValidateArg ( thisOrContextObject ) ;
130
130
DebugValidateArg ( arg0 ) ;
131
131
DebugValidateArg ( arg1 ) ;
132
- if ( IsEnabled ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } , { Format ( arg1 ) } )") ;
132
+ if ( Log . IsEnabled ( ) ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } , { Format ( arg1 ) } )") ;
133
133
}
134
134
135
135
/// <summary>Logs entrance to a method.</summary>
@@ -145,7 +145,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
145
145
DebugValidateArg ( arg0 ) ;
146
146
DebugValidateArg ( arg1 ) ;
147
147
DebugValidateArg ( arg2 ) ;
148
- if ( IsEnabled ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } , { Format ( arg1 ) } , { Format ( arg2 ) } )") ;
148
+ if ( Log . IsEnabled ( ) ) Log . Enter ( IdOf ( thisOrContextObject ) , memberName , $ "({ Format ( arg0 ) } , { Format ( arg1 ) } , { Format ( arg2 ) } )") ;
149
149
}
150
150
151
151
[ Event ( EnterEventId , Level = EventLevel . Informational , Keywords = Keywords . EnterExit ) ]
@@ -163,7 +163,7 @@ public static void Exit(object? thisOrContextObject, FormattableString? formatta
163
163
{
164
164
DebugValidateArg ( thisOrContextObject ) ;
165
165
DebugValidateArg ( formattableString ) ;
166
- if ( IsEnabled ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
166
+ if ( Log . IsEnabled ( ) ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
167
167
}
168
168
169
169
/// <summary>Logs exit from a method.</summary>
@@ -175,7 +175,7 @@ public static void Exit(object? thisOrContextObject, object arg0, [CallerMemberN
175
175
{
176
176
DebugValidateArg ( thisOrContextObject ) ;
177
177
DebugValidateArg ( arg0 ) ;
178
- if ( IsEnabled ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , Format ( arg0 ) . ToString ( ) ) ;
178
+ if ( Log . IsEnabled ( ) ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , Format ( arg0 ) . ToString ( ) ) ;
179
179
}
180
180
181
181
/// <summary>Logs exit from a method.</summary>
@@ -189,7 +189,7 @@ public static void Exit(object? thisOrContextObject, object arg0, object arg1, [
189
189
DebugValidateArg ( thisOrContextObject ) ;
190
190
DebugValidateArg ( arg0 ) ;
191
191
DebugValidateArg ( arg1 ) ;
192
- if ( IsEnabled ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , $ "{ Format ( arg0 ) } , { Format ( arg1 ) } ") ;
192
+ if ( Log . IsEnabled ( ) ) Log . Exit ( IdOf ( thisOrContextObject ) , memberName , $ "{ Format ( arg0 ) } , { Format ( arg1 ) } ") ;
193
193
}
194
194
195
195
[ Event ( ExitEventId , Level = EventLevel . Informational , Keywords = Keywords . EnterExit ) ]
@@ -207,7 +207,7 @@ public static void Info(object? thisOrContextObject, FormattableString? formatta
207
207
{
208
208
DebugValidateArg ( thisOrContextObject ) ;
209
209
DebugValidateArg ( formattableString ) ;
210
- if ( IsEnabled ) Log . Info ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
210
+ if ( Log . IsEnabled ( ) ) Log . Info ( IdOf ( thisOrContextObject ) , memberName , formattableString != null ? Format ( formattableString ) : NoParameters ) ;
211
211
}
212
212
213
213
/// <summary>Logs an information message.</summary>
@@ -219,7 +219,7 @@ public static void Info(object? thisOrContextObject, object? message, [CallerMem
219
219
{
220
220
DebugValidateArg ( thisOrContextObject ) ;
221
221
DebugValidateArg ( message ) ;
222
- if ( IsEnabled ) Log . Info ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
222
+ if ( Log . IsEnabled ( ) ) Log . Info ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
223
223
}
224
224
225
225
[ Event ( InfoEventId , Level = EventLevel . Informational , Keywords = Keywords . Default ) ]
@@ -237,7 +237,7 @@ public static void Error(object? thisOrContextObject, FormattableString formatta
237
237
{
238
238
DebugValidateArg ( thisOrContextObject ) ;
239
239
DebugValidateArg ( formattableString ) ;
240
- if ( IsEnabled ) Log . ErrorMessage ( IdOf ( thisOrContextObject ) , memberName , Format ( formattableString ) ) ;
240
+ if ( Log . IsEnabled ( ) ) Log . ErrorMessage ( IdOf ( thisOrContextObject ) , memberName , Format ( formattableString ) ) ;
241
241
}
242
242
243
243
/// <summary>Logs an error message.</summary>
@@ -249,7 +249,7 @@ public static void Error(object? thisOrContextObject, object message, [CallerMem
249
249
{
250
250
DebugValidateArg ( thisOrContextObject ) ;
251
251
DebugValidateArg ( message ) ;
252
- if ( IsEnabled ) Log . ErrorMessage ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
252
+ if ( Log . IsEnabled ( ) ) Log . ErrorMessage ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
253
253
}
254
254
255
255
[ Event ( ErrorEventId , Level = EventLevel . Error , Keywords = Keywords . Default ) ]
@@ -268,7 +268,7 @@ public static void Fail(object? thisOrContextObject, FormattableString formattab
268
268
// Don't call DebugValidateArg on args, as we expect Fail to be used in assert/failure situations
269
269
// that should never happen in production, and thus we don't care about extra costs.
270
270
271
- if ( IsEnabled ) Log . CriticalFailure ( IdOf ( thisOrContextObject ) , memberName , Format ( formattableString ) ) ;
271
+ if ( Log . IsEnabled ( ) ) Log . CriticalFailure ( IdOf ( thisOrContextObject ) , memberName , Format ( formattableString ) ) ;
272
272
Debug . Fail ( Format ( formattableString ) , $ "{ IdOf ( thisOrContextObject ) } .{ memberName } ") ;
273
273
}
274
274
@@ -282,7 +282,7 @@ public static void Fail(object? thisOrContextObject, object message, [CallerMemb
282
282
// Don't call DebugValidateArg on args, as we expect Fail to be used in assert/failure situations
283
283
// that should never happen in production, and thus we don't care about extra costs.
284
284
285
- if ( IsEnabled ) Log . CriticalFailure ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
285
+ if ( Log . IsEnabled ( ) ) Log . CriticalFailure ( IdOf ( thisOrContextObject ) , memberName , Format ( message ) . ToString ( ) ) ;
286
286
Debug . Fail ( Format ( message ) . ToString ( ) , $ "{ IdOf ( thisOrContextObject ) } .{ memberName } ") ;
287
287
}
288
288
@@ -311,7 +311,7 @@ public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, [Calle
311
311
[ NonEvent ]
312
312
public static void DumpBuffer ( object ? thisOrContextObject , byte [ ] buffer , int offset , int count , [ CallerMemberName ] string ? memberName = null )
313
313
{
314
- if ( IsEnabled )
314
+ if ( Log . IsEnabled ( ) )
315
315
{
316
316
if ( offset < 0 || offset > buffer . Length - count )
317
317
{
@@ -343,7 +343,7 @@ public static unsafe void DumpBuffer(object? thisOrContextObject, IntPtr bufferP
343
343
Debug . Assert ( bufferPtr != IntPtr . Zero ) ;
344
344
Debug . Assert ( count >= 0 ) ;
345
345
346
- if ( IsEnabled )
346
+ if ( Log . IsEnabled ( ) )
347
347
{
348
348
var buffer = new byte [ Math . Min ( count , MaxDumpSize ) ] ;
349
349
fixed ( byte * targetPtr = buffer )
@@ -369,7 +369,7 @@ public static void Associate(object first, object second, [CallerMemberName] str
369
369
{
370
370
DebugValidateArg ( first ) ;
371
371
DebugValidateArg ( second ) ;
372
- if ( IsEnabled ) Log . Associate ( IdOf ( first ) , memberName , IdOf ( first ) , IdOf ( second ) ) ;
372
+ if ( Log . IsEnabled ( ) ) Log . Associate ( IdOf ( first ) , memberName , IdOf ( first ) , IdOf ( second ) ) ;
373
373
}
374
374
375
375
/// <summary>Logs a relationship between two objects.</summary>
@@ -383,7 +383,7 @@ public static void Associate(object? thisOrContextObject, object first, object s
383
383
DebugValidateArg ( thisOrContextObject ) ;
384
384
DebugValidateArg ( first ) ;
385
385
DebugValidateArg ( second ) ;
386
- if ( IsEnabled ) Log . Associate ( IdOf ( thisOrContextObject ) , memberName , IdOf ( first ) , IdOf ( second ) ) ;
386
+ if ( Log . IsEnabled ( ) ) Log . Associate ( IdOf ( thisOrContextObject ) , memberName , IdOf ( first ) , IdOf ( second ) ) ;
387
387
}
388
388
389
389
[ Event ( AssociateEventId , Level = EventLevel . Informational , Keywords = Keywords . Default , Message = "[{2}]<-->[{3}]" ) ]
@@ -396,7 +396,7 @@ private void Associate(string thisOrContextObject, string? memberName, string fi
396
396
[ Conditional ( "DEBUG_NETEVENTSOURCE_MISUSE" ) ]
397
397
private static void DebugValidateArg ( object ? arg )
398
398
{
399
- if ( ! IsEnabled )
399
+ if ( ! Log . IsEnabled ( ) )
400
400
{
401
401
Debug . Assert ( ! ( arg is ValueType ) , $ "Should not be passing value type { arg ? . GetType ( ) } to logging without IsEnabled check") ;
402
402
Debug . Assert ( ! ( arg is FormattableString ) , $ "Should not be formatting FormattableString \" { arg } \" if tracing isn't enabled") ;
@@ -406,12 +406,9 @@ private static void DebugValidateArg(object? arg)
406
406
[ Conditional ( "DEBUG_NETEVENTSOURCE_MISUSE" ) ]
407
407
private static void DebugValidateArg ( FormattableString ? arg )
408
408
{
409
- Debug . Assert ( IsEnabled || arg == null , $ "Should not be formatting FormattableString \" { arg } \" if tracing isn't enabled") ;
409
+ Debug . Assert ( Log . IsEnabled ( ) || arg == null , $ "Should not be formatting FormattableString \" { arg } \" if tracing isn't enabled") ;
410
410
}
411
411
412
- public static new bool IsEnabled =>
413
- Log . IsEnabled ( ) ;
414
-
415
412
[ NonEvent ]
416
413
public static string IdOf ( object ? value ) => value != null ? value . GetType ( ) . Name + "#" + GetHashCode ( value ) : NullInstance ;
417
414
0 commit comments