Skip to content

Commit 241e45d

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

File tree

4 files changed

+23
-130
lines changed

4 files changed

+23
-130
lines changed

src/Shared/runtime/NetEventSource.Common.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,9 @@ 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.Log.IsEnabled()) NetEventSource.Enter(this, refArg1, valueTypeArg2); // entering an instance method with a value type arg
3332
// if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, $"Found certificate: {cert}"); // info logging with a formattable string
3433
// - 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
3634
// 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
4035
// Debug.Asserts inside the logging methods will help to flag some misuse if the DEBUG_NETEVENTSOURCE_MISUSE compilation constant is defined.
4136
// However, because it can be difficult by observation to understand all of the costs involved, guarding can be done everywhere.
4237
// - 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
10398
{
10499
DebugValidateArg(thisOrContextObject);
105100
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);
107102
}
108103

109104
/// <summary>Logs entrance to a method.</summary>
@@ -115,7 +110,7 @@ public static void Enter(object? thisOrContextObject, object arg0, [CallerMember
115110
{
116111
DebugValidateArg(thisOrContextObject);
117112
DebugValidateArg(arg0);
118-
if (Log.IsEnabled()) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)})");
113+
if (IsEnabled) Log.Enter(IdOf(thisOrContextObject), memberName, $"({Format(arg0)})");
119114
}
120115

121116
/// <summary>Logs entrance to a method.</summary>
@@ -129,7 +124,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
129124
DebugValidateArg(thisOrContextObject);
130125
DebugValidateArg(arg0);
131126
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)})");
133128
}
134129

135130
/// <summary>Logs entrance to a method.</summary>
@@ -145,7 +140,7 @@ public static void Enter(object? thisOrContextObject, object arg0, object arg1,
145140
DebugValidateArg(arg0);
146141
DebugValidateArg(arg1);
147142
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)})");
149144
}
150145

151146
[Event(EnterEventId, Level = EventLevel.Informational, Keywords = Keywords.EnterExit)]
@@ -163,7 +158,7 @@ public static void Exit(object? thisOrContextObject, FormattableString? formatta
163158
{
164159
DebugValidateArg(thisOrContextObject);
165160
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);
167162
}
168163

169164
/// <summary>Logs exit from a method.</summary>
@@ -175,7 +170,7 @@ public static void Exit(object? thisOrContextObject, object arg0, [CallerMemberN
175170
{
176171
DebugValidateArg(thisOrContextObject);
177172
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());
179174
}
180175

181176
/// <summary>Logs exit from a method.</summary>
@@ -189,7 +184,7 @@ public static void Exit(object? thisOrContextObject, object arg0, object arg1, [
189184
DebugValidateArg(thisOrContextObject);
190185
DebugValidateArg(arg0);
191186
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)}");
193188
}
194189

195190
[Event(ExitEventId, Level = EventLevel.Informational, Keywords = Keywords.EnterExit)]
@@ -207,7 +202,7 @@ public static void Info(object? thisOrContextObject, FormattableString? formatta
207202
{
208203
DebugValidateArg(thisOrContextObject);
209204
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);
211206
}
212207

213208
/// <summary>Logs an information message.</summary>
@@ -219,7 +214,7 @@ public static void Info(object? thisOrContextObject, object? message, [CallerMem
219214
{
220215
DebugValidateArg(thisOrContextObject);
221216
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());
223218
}
224219

225220
[Event(InfoEventId, Level = EventLevel.Informational, Keywords = Keywords.Default)]
@@ -237,7 +232,7 @@ public static void Error(object? thisOrContextObject, FormattableString formatta
237232
{
238233
DebugValidateArg(thisOrContextObject);
239234
DebugValidateArg(formattableString);
240-
if (Log.IsEnabled()) Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(formattableString));
235+
if (IsEnabled) Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(formattableString));
241236
}
242237

243238
/// <summary>Logs an error message.</summary>
@@ -249,7 +244,7 @@ public static void Error(object? thisOrContextObject, object message, [CallerMem
249244
{
250245
DebugValidateArg(thisOrContextObject);
251246
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());
253248
}
254249

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

271-
if (Log.IsEnabled()) Log.CriticalFailure(IdOf(thisOrContextObject), memberName, Format(formattableString));
266+
if (IsEnabled) Log.CriticalFailure(IdOf(thisOrContextObject), memberName, Format(formattableString));
272267
Debug.Fail(Format(formattableString), $"{IdOf(thisOrContextObject)}.{memberName}");
273268
}
274269

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

285-
if (Log.IsEnabled()) Log.CriticalFailure(IdOf(thisOrContextObject), memberName, Format(message).ToString());
280+
if (IsEnabled) Log.CriticalFailure(IdOf(thisOrContextObject), memberName, Format(message).ToString());
286281
Debug.Fail(Format(message).ToString(), $"{IdOf(thisOrContextObject)}.{memberName}");
287282
}
288283

@@ -311,7 +306,7 @@ public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, [Calle
311306
[NonEvent]
312307
public static void DumpBuffer(object? thisOrContextObject, byte[] buffer, int offset, int count, [CallerMemberName] string? memberName = null)
313308
{
314-
if (Log.IsEnabled())
309+
if (IsEnabled)
315310
{
316311
if (offset < 0 || offset > buffer.Length - count)
317312
{
@@ -343,7 +338,7 @@ public static unsafe void DumpBuffer(object? thisOrContextObject, IntPtr bufferP
343338
Debug.Assert(bufferPtr != IntPtr.Zero);
344339
Debug.Assert(count >= 0);
345340

346-
if (Log.IsEnabled())
341+
if (IsEnabled)
347342
{
348343
var buffer = new byte[Math.Min(count, MaxDumpSize)];
349344
fixed (byte* targetPtr = buffer)
@@ -369,7 +364,7 @@ public static void Associate(object first, object second, [CallerMemberName] str
369364
{
370365
DebugValidateArg(first);
371366
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));
373368
}
374369

375370
/// <summary>Logs a relationship between two objects.</summary>
@@ -383,7 +378,7 @@ public static void Associate(object? thisOrContextObject, object first, object s
383378
DebugValidateArg(thisOrContextObject);
384379
DebugValidateArg(first);
385380
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));
387382
}
388383

389384
[Event(AssociateEventId, Level = EventLevel.Informational, Keywords = Keywords.Default, Message = "[{2}]<-->[{3}]")]
@@ -396,7 +391,7 @@ private void Associate(string thisOrContextObject, string? memberName, string fi
396391
[Conditional("DEBUG_NETEVENTSOURCE_MISUSE")]
397392
private static void DebugValidateArg(object? arg)
398393
{
399-
if (!Log.IsEnabled())
394+
if (!IsEnabled)
400395
{
401396
Debug.Assert(!(arg is ValueType), $"Should not be passing value type {arg?.GetType()} to logging without IsEnabled check");
402397
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)
406401
[Conditional("DEBUG_NETEVENTSOURCE_MISUSE")]
407402
private static void DebugValidateArg(FormattableString? arg)
408403
{
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");
410405
}
411406

407+
public static new bool IsEnabled =>
408+
Log.IsEnabled();
409+
412410
[NonEvent]
413411
public static string IdOf(object? value) => value != null ? value.GetType().Name + "#" + GetHashCode(value) : NullInstance;
414412

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,17 @@ internal sealed class MsQuicConnection : QuicConnectionProvider
5151
// constructor for inbound connections
5252
public MsQuicConnection(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, IntPtr nativeObjPtr)
5353
{
54-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
5554
_localEndPoint = localEndPoint;
5655
_remoteEndPoint = remoteEndPoint;
5756
_ptr = nativeObjPtr;
5857

5958
SetCallbackHandler();
6059
SetIdleTimeout(TimeSpan.FromSeconds(120));
61-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
6260
}
6361

6462
// constructor for outbound connections
6563
public MsQuicConnection(QuicClientConnectionOptions options)
6664
{
67-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
68-
6965
// TODO need to figure out if/how we want to expose sessions
7066
// Creating a session per connection isn't ideal.
7167
_session = new MsQuicSession();
@@ -74,8 +70,6 @@ public MsQuicConnection(QuicClientConnectionOptions options)
7470

7571
SetCallbackHandler();
7672
SetIdleTimeout(options.IdleTimeout);
77-
78-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
7973
}
8074

8175
internal override IPEndPoint LocalEndPoint
@@ -168,8 +162,6 @@ internal uint HandleEvent(ref ConnectionEvent connectionEvent)
168162

169163
private uint HandleEventConnected(ConnectionEvent connectionEvent)
170164
{
171-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
172-
173165
SOCKADDR_INET inetAddress = MsQuicParameterHelpers.GetINetParam(MsQuicApi.Api, _ptr, (uint)QUIC_PARAM_LEVEL.CONNECTION, (uint)QUIC_PARAM_CONN.LOCAL_ADDRESS);
174166
_localEndPoint = MsQuicAddressHelpers.INetToIPEndPoint(inetAddress);
175167

@@ -179,22 +171,18 @@ private uint HandleEventConnected(ConnectionEvent connectionEvent)
179171
// handle event shutdown initiated by transport
180172
_connectTcs.Complete(MsQuicStatusCodes.Success);
181173

182-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
183174
return MsQuicStatusCodes.Success;
184175
}
185176

186177
private uint HandleEventShutdownInitiatedByTransport(ConnectionEvent connectionEvent)
187178
{
188-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
189-
190179
if (!_connected)
191180
{
192181
_connectTcs.CompleteException(new IOException("Connection has been shutdown."));
193182
}
194183

195184
_acceptQueue.Writer.Complete();
196185

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

199187
return MsQuicStatusCodes.Success;
200188
}
@@ -208,23 +196,14 @@ private uint HandleEventShutdownInitiatedByPeer(ConnectionEvent connectionEvent)
208196

209197
private uint HandleEventShutdownComplete(ConnectionEvent connectionEvent)
210198
{
211-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
212-
213199
_shutdownTcs.Complete(MsQuicStatusCodes.Success);
214-
215-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
216200
return MsQuicStatusCodes.Success;
217201
}
218202

219203
private uint HandleEventNewStream(ConnectionEvent connectionEvent)
220204
{
221-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
222-
223205
MsQuicStream msQuicStream = new MsQuicStream(this, connectionEvent.StreamFlags, connectionEvent.Data.NewStream.Stream, inbound: true);
224-
225206
_acceptQueue.Writer.TryWrite(msQuicStream);
226-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
227-
228207
return MsQuicStatusCodes.Success;
229208
}
230209

@@ -235,8 +214,6 @@ private uint HandleEventStreamsAvailable(ConnectionEvent connectionEvent)
235214

236215
internal override async ValueTask<QuicStreamProvider> AcceptStreamAsync(CancellationToken cancellationToken = default)
237216
{
238-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
239-
240217
ThrowIfDisposed();
241218

242219
MsQuicStream stream;
@@ -254,7 +231,6 @@ internal override async ValueTask<QuicStreamProvider> AcceptStreamAsync(Cancella
254231
};
255232
}
256233

257-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
258234
return stream;
259235
}
260236

@@ -305,8 +281,6 @@ internal override ValueTask ConnectAsync(CancellationToken cancellationToken = d
305281
private MsQuicStream StreamOpen(
306282
QUIC_STREAM_OPEN_FLAG flags)
307283
{
308-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
309-
310284
IntPtr streamPtr = IntPtr.Zero;
311285
QuicExceptionHelpers.ThrowIfFailed(
312286
MsQuicApi.Api.StreamOpenDelegate(
@@ -317,10 +291,7 @@ private MsQuicStream StreamOpen(
317291
out streamPtr),
318292
"Failed to open stream to peer.");
319293

320-
MsQuicStream stream = new MsQuicStream(this, flags, streamPtr, inbound: false);
321-
322-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
323-
return stream;
294+
return new MsQuicStream(this, flags, streamPtr, inbound: false);
324295
}
325296

326297
private void SetCallbackHandler()
@@ -337,15 +308,12 @@ private ValueTask ShutdownAsync(
337308
QUIC_CONNECTION_SHUTDOWN_FLAG Flags,
338309
long ErrorCode)
339310
{
340-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
341-
342311
uint status = MsQuicApi.Api.ConnectionShutdownDelegate(
343312
_ptr,
344313
(uint)Flags,
345314
ErrorCode);
346315
QuicExceptionHelpers.ThrowIfFailed(status, "Failed to shutdown connection.");
347316

348-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
349317
return _shutdownTcs.GetTypelessValueTask();
350318
}
351319

@@ -377,8 +345,6 @@ private void Dispose(bool disposing)
377345
return;
378346
}
379347

380-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Enter(this);
381-
382348
if (_ptr != IntPtr.Zero)
383349
{
384350
MsQuicApi.Api.ConnectionCloseDelegate?.Invoke(_ptr);
@@ -394,8 +360,6 @@ private void Dispose(bool disposing)
394360
}
395361

396362
_disposed = true;
397-
398-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
399363
}
400364

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

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ internal override IPEndPoint ListenEndPoint
6262

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

6967
MsQuicConnection connection;
@@ -81,7 +79,6 @@ await connection.SetSecurityConfigForConnection(_sslOptions.ServerCertificate!,
8179
_options.CertificateFilePath,
8280
_options.PrivateKeyFilePath).ConfigureAwait(false);
8381

84-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Exit(this);
8582
return connection;
8683
}
8784

0 commit comments

Comments
 (0)