Skip to content

Commit c45e0d7

Browse files
Sync shared code from runtime (#23843)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 5d170de commit c45e0d7

File tree

4 files changed

+80
-83
lines changed

4 files changed

+80
-83
lines changed

src/Shared/runtime/NetEventSource.Common.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ namespace System.Net
2929
// Usage:
3030
// - Operations that may allocate (e.g. boxing a value type, using string interpolation, etc.) or that may have computations
3131
// 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
3434
// - Operations that have zero allocations / measurable computations at call sites can use a simpler pattern, calling methods like:
3535
// NetEventSource.Enter(this); // entering an instance method
3636
// NetEventSource.Info(this, "literal string"); // arbitrary message with a literal string
@@ -103,7 +103,7 @@ public static void Enter(object? thisOrContextObject, FormattableString? formatt
103103
{
104104
DebugValidateArg(thisOrContextObject);
105105
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);
107107
}
108108

109109
/// <summary>Logs entrance to a method.</summary>
@@ -115,7 +115,7 @@ public static void Enter(object? thisOrContextObject, object arg0, [CallerMember
115115
{
116116
DebugValidateArg(thisOrContextObject);
117117
DebugValidateArg(arg0);
118-
if (IsEnabled) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)})");
118+
if (Log.IsEnabled()) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)})");
119119
}
120120

121121
/// <summary>Logs entrance to a method.</summary>
@@ -129,7 +129,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
129129
DebugValidateArg(thisOrContextObject);
130130
DebugValidateArg(arg0);
131131
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)})");
133133
}
134134

135135
/// <summary>Logs entrance to a method.</summary>
@@ -145,7 +145,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
145145
DebugValidateArg(arg0);
146146
DebugValidateArg(arg1);
147147
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)})");
149149
}
150150

151151
[Event(EnterEventId, Level = EventLevel.Informational, Keywords = Keywords.EnterExit)]
@@ -163,7 +163,7 @@ public static void Exit(object? thisOrContextObject, FormattableString? formatta
163163
{
164164
DebugValidateArg(thisOrContextObject);
165165
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);
167167
}
168168

169169
/// <summary>Logs exit from a method.</summary>
@@ -175,7 +175,7 @@ public static void Exit(object? thisOrContextObject, object arg0, [CallerMemberN
175175
{
176176
DebugValidateArg(thisOrContextObject);
177177
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());
179179
}
180180

181181
/// <summary>Logs exit from a method.</summary>
@@ -189,7 +189,7 @@ public static void Exit(object? thisOrContextObject, object arg0, object arg1, [
189189
DebugValidateArg(thisOrContextObject);
190190
DebugValidateArg(arg0);
191191
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)}");
193193
}
194194

195195
[Event(ExitEventId, Level = EventLevel.Informational, Keywords = Keywords.EnterExit)]
@@ -207,7 +207,7 @@ public static void Info(object? thisOrContextObject, FormattableString? formatta
207207
{
208208
DebugValidateArg(thisOrContextObject);
209209
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);
211211
}
212212

213213
/// <summary>Logs an information message.</summary>
@@ -219,7 +219,7 @@ public static void Info(object? thisOrContextObject, object? message, [CallerMem
219219
{
220220
DebugValidateArg(thisOrContextObject);
221221
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());
223223
}
224224

225225
[Event(InfoEventId, Level = EventLevel.Informational, Keywords = Keywords.Default)]
@@ -237,7 +237,7 @@ public static void Error(object? thisOrContextObject, FormattableString formatta
237237
{
238238
DebugValidateArg(thisOrContextObject);
239239
DebugValidateArg(formattableString);
240-
if (IsEnabled) Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(formattableString));
240+
if (Log.IsEnabled()) Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(formattableString));
241241
}
242242

243243
/// <summary>Logs an error message.</summary>
@@ -249,7 +249,7 @@ public static void Error(object? thisOrContextObject, object message, [CallerMem
249249
{
250250
DebugValidateArg(thisOrContextObject);
251251
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());
253253
}
254254

255255
[Event(ErrorEventId, Level = EventLevel.Error, Keywords = Keywords.Default)]
@@ -268,7 +268,7 @@ public static void Fail(object? thisOrContextObject, FormattableString formattab
268268
// Don't call DebugValidateArg on args, as we expect Fail to be used in assert/failure situations
269269
// that should never happen in production, and thus we don't care about extra costs.
270270

271-
if (IsEnabled) Log.CriticalFailure(IdOf(thisOrContextObject), memberName, Format(formattableString));
271+
if (Log.IsEnabled()) Log.CriticalFailure(IdOf(thisOrContextObject), memberName, Format(formattableString));
272272
Debug.Fail(Format(formattableString), $"{IdOf(thisOrContextObject)}.{memberName}");
273273
}
274274

@@ -282,7 +282,7 @@ public static void Fail(object? thisOrContextObject, object message, [CallerMemb
282282
// Don't call DebugValidateArg on args, as we expect Fail to be used in assert/failure situations
283283
// that should never happen in production, and thus we don't care about extra costs.
284284

285-
if (IsEnabled) Log.CriticalFailure(IdOf(thisOrContextObject), memberName, Format(message).ToString());
285+
if (Log.IsEnabled()) Log.CriticalFailure(IdOf(thisOrContextObject), memberName, Format(message).ToString());
286286
Debug.Fail(Format(message).ToString(), $"{IdOf(thisOrContextObject)}.{memberName}");
287287
}
288288

@@ -311,7 +311,7 @@ public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, [Calle
311311
[NonEvent]
312312
public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, int offset, int count, [CallerMemberName] string? memberName = null)
313313
{
314-
if (IsEnabled)
314+
if (Log.IsEnabled())
315315
{
316316
if (offset < 0 || offset > buffer.Length - count)
317317
{
@@ -343,7 +343,7 @@ public static unsafe void DumpBuffer(object? thisOrContextObject, IntPtr bufferP
343343
Debug.Assert(bufferPtr != IntPtr.Zero);
344344
Debug.Assert(count >= 0);
345345

346-
if (IsEnabled)
346+
if (Log.IsEnabled())
347347
{
348348
var buffer = new byte[Math.Min(count, MaxDumpSize)];
349349
fixed (byte* targetPtr = buffer)
@@ -369,7 +369,7 @@ public static void Associate(object first, object second, [CallerMemberName] str
369369
{
370370
DebugValidateArg(first);
371371
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));
373373
}
374374

375375
/// <summary>Logs a relationship between two objects.</summary>
@@ -383,7 +383,7 @@ public static void Associate(object? thisOrContextObject, object first, object s
383383
DebugValidateArg(thisOrContextObject);
384384
DebugValidateArg(first);
385385
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));
387387
}
388388

389389
[Event(AssociateEventId, Level = EventLevel.Informational, Keywords = Keywords.Default, Message = "[{2}]<-->[{3}]")]
@@ -396,7 +396,7 @@ private void Associate(string thisOrContextObject, string? memberName, string fi
396396
[Conditional("DEBUG_NETEVENTSOURCE_MISUSE")]
397397
private static void DebugValidateArg(object? arg)
398398
{
399-
if (!IsEnabled)
399+
if (!Log.IsEnabled())
400400
{
401401
Debug.Assert(!(arg is ValueType), $"Should not be passing value type {arg?.GetType()} to logging without IsEnabled check");
402402
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)
406406
[Conditional("DEBUG_NETEVENTSOURCE_MISUSE")]
407407
private static void DebugValidateArg(FormattableString? arg)
408408
{
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");
410410
}
411411

412-
public static new bool IsEnabled =>
413-
Log.IsEnabled();
414-
415412
[NonEvent]
416413
public static string IdOf(object? value) => value != null ? value.GetType().Name + "#" + GetHashCode(value) : NullInstance;
417414

src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicConnection.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,20 @@ internal sealed class MsQuicConnection : QuicConnectionProvider
5151
// constructor for inbound connections
5252
public MsQuicConnection(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, IntPtr nativeObjPtr)
5353
{
54-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
54+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
5555
_localEndPoint = localEndPoint;
5656
_remoteEndPoint = remoteEndPoint;
5757
_ptr = nativeObjPtr;
5858

5959
SetCallbackHandler();
6060
SetIdleTimeout(TimeSpan.FromSeconds(120));
61-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
61+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
6262
}
6363

6464
// constructor for outbound connections
6565
public MsQuicConnection(QuicClientConnectionOptions options)
6666
{
67-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
67+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
6868

6969
// TODO need to figure out if/how we want to expose sessions
7070
// Creating a session per connection isn't ideal.
@@ -75,7 +75,7 @@ public MsQuicConnection(QuicClientConnectionOptions options)
7575
SetCallbackHandler();
7676
SetIdleTimeout(options.IdleTimeout);
7777

78-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
78+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
7979
}
8080

8181
internal override IPEndPoint LocalEndPoint
@@ -168,7 +168,7 @@ internal uint HandleEvent(ref ConnectionEvent connectionEvent)
168168

169169
private uint HandleEventConnected(ConnectionEvent connectionEvent)
170170
{
171-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
171+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
172172

173173
SOCKADDR_INET inetAddress = MsQuicParameterHelpers.GetINetParam(MsQuicApi.Api, _ptr, (uint)QUIC_PARAM_LEVEL.CONNECTION, (uint)QUIC_PARAM_CONN.LOCAL_ADDRESS);
174174
_localEndPoint = MsQuicAddressHelpers.INetToIPEndPoint(inetAddress);
@@ -179,13 +179,13 @@ private uint HandleEventConnected(ConnectionEvent connectionEvent)
179179
// handle event shutdown initiated by transport
180180
_connectTcs.Complete(MsQuicStatusCodes.Success);
181181

182-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
182+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
183183
return MsQuicStatusCodes.Success;
184184
}
185185

186186
private uint HandleEventShutdownInitiatedByTransport(ConnectionEvent connectionEvent)
187187
{
188-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
188+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
189189

190190
if (!_connected)
191191
{
@@ -194,7 +194,7 @@ private uint HandleEventShutdownInitiatedByTransport(ConnectionEvent connectionE
194194

195195
_acceptQueue.Writer.Complete();
196196

197-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
197+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
198198

199199
return MsQuicStatusCodes.Success;
200200
}
@@ -208,22 +208,22 @@ private uint HandleEventShutdownInitiatedByPeer(ConnectionEvent connectionEvent)
208208

209209
private uint HandleEventShutdownComplete(ConnectionEvent connectionEvent)
210210
{
211-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
211+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
212212

213213
_shutdownTcs.Complete(MsQuicStatusCodes.Success);
214214

215-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
215+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
216216
return MsQuicStatusCodes.Success;
217217
}
218218

219219
private uint HandleEventNewStream(ConnectionEvent connectionEvent)
220220
{
221-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
221+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
222222

223223
MsQuicStream msQuicStream = new MsQuicStream(this, connectionEvent.StreamFlags, connectionEvent.Data.NewStream.Stream, inbound: true);
224224

225225
_acceptQueue.Writer.TryWrite(msQuicStream);
226-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
226+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
227227

228228
return MsQuicStatusCodes.Success;
229229
}
@@ -235,7 +235,7 @@ private uint HandleEventStreamsAvailable(ConnectionEvent connectionEvent)
235235

236236
internal override async ValueTask<QuicStreamProvider> AcceptStreamAsync(CancellationToken cancellationToken = default)
237237
{
238-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
238+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
239239

240240
ThrowIfDisposed();
241241

@@ -254,7 +254,7 @@ internal override async ValueTask<QuicStreamProvider> AcceptStreamAsync(Cancella
254254
};
255255
}
256256

257-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
257+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
258258
return stream;
259259
}
260260

@@ -305,7 +305,7 @@ internal override ValueTask ConnectAsync(CancellationToken cancellationToken = d
305305
private MsQuicStream StreamOpen(
306306
QUIC_STREAM_OPEN_FLAG flags)
307307
{
308-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
308+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
309309

310310
IntPtr streamPtr = IntPtr.Zero;
311311
QuicExceptionHelpers.ThrowIfFailed(
@@ -319,7 +319,7 @@ private MsQuicStream StreamOpen(
319319

320320
MsQuicStream stream = new MsQuicStream(this, flags, streamPtr, inbound: false);
321321

322-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
322+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
323323
return stream;
324324
}
325325

@@ -337,15 +337,15 @@ private ValueTask ShutdownAsync(
337337
QUIC_CONNECTION_SHUTDOWN_FLAG Flags,
338338
long ErrorCode)
339339
{
340-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
340+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
341341

342342
uint status = MsQuicApi.Api.ConnectionShutdownDelegate(
343343
_ptr,
344344
(uint)Flags,
345345
ErrorCode);
346346
QuicExceptionHelpers.ThrowIfFailed(status, "Failed to shutdown connection.");
347347

348-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
348+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
349349
return _shutdownTcs.GetTypelessValueTask();
350350
}
351351

@@ -377,7 +377,7 @@ private void Dispose(bool disposing)
377377
return;
378378
}
379379

380-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
380+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
381381

382382
if (_ptr != IntPtr.Zero)
383383
{
@@ -395,7 +395,7 @@ private void Dispose(bool disposing)
395395

396396
_disposed = true;
397397

398-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
398+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
399399
}
400400

401401
internal override ValueTask CloseAsync(long errorCode, CancellationToken cancellationToken = default)

src/Shared/runtime/Quic/Implementations/MsQuic/MsQuicListener.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal override IPEndPoint ListenEndPoint
6262

6363
internal override async ValueTask<QuicConnectionProvider> AcceptConnectionAsync(CancellationToken cancellationToken = default)
6464
{
65-
if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
65+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
6666

6767
ThrowIfDisposed();
6868

@@ -81,7 +81,7 @@ await connection.SetSecurityConfigForConnection(_sslOptions.ServerCertificate!,
8181
_options.CertificateFilePath,
8282
_options.PrivateKeyFilePath).ConfigureAwait(false);
8383

84-
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
84+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
8585
return connection;
8686
}
8787

0 commit comments

Comments
 (0)