From 870f47608474884041659705cfde1a2f15b79149 Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Tue, 30 Jun 2020 16:36:45 +0200 Subject: [PATCH 01/11] SocketsTelemetry logs Connect operations --- .../src/System.Net.Sockets.csproj | 319 ++++++------------ .../src/System/Net/Sockets/Socket.cs | 12 + .../Net/Sockets/SocketAsyncEventArgs.cs | 17 + .../System/Net/Sockets/SocketsTelemetry.cs | 83 +++++ 4 files changed, 219 insertions(+), 212 deletions(-) create mode 100644 src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs diff --git a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj index 951011f5416dff..3367c6767b28e2 100644 --- a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj +++ b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj @@ -14,6 +14,7 @@ + @@ -48,48 +49,29 @@ - + - - + + - + - - - - - - - - - + + + + + + + + + - - - - - - + + + + + + @@ -106,87 +88,47 @@ - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -202,100 +144,53 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index 6fe0de419f7717..de1e6a5a9ed09f 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -2158,6 +2158,8 @@ private bool CanUseConnectEx(EndPoint remoteEP) internal IAsyncResult UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback? callback, object? state, bool flowContext = false) { + SocketsTelemetry.Log.ConnectStart(remoteEP); + if (CanUseConnectEx(remoteEP)) { return BeginConnectEx(remoteEP, flowContext, callback, state); @@ -2424,6 +2426,9 @@ asyncResult as MultipleAddressConnectAsyncResult ?? Exception? ex = castedAsyncResult.Result as Exception; if (ex != null || (SocketError)castedAsyncResult.ErrorCode != SocketError.Success) { + SocketsTelemetry.Log.ConnectFailed(); + SocketsTelemetry.Log.ConnectStop(); + if (ex == null) { SocketError errorCode = (SocketError)castedAsyncResult.ErrorCode; @@ -2438,6 +2443,8 @@ asyncResult as MultipleAddressConnectAsyncResult ?? ExceptionDispatchInfo.Throw(ex); } + SocketsTelemetry.Log.ConnectStop(); + if (NetEventSource.IsEnabled) { NetEventSource.Connected(this, LocalEndPoint, RemoteEndPoint); @@ -4338,6 +4345,9 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket // Throw an appropriate SocketException if the native call fails. if (errorCode != SocketError.Success) { + SocketsTelemetry.Log.ConnectFailed(); + SocketsTelemetry.Log.ConnectStop(); + UpdateConnectSocketErrorForDisposed(ref errorCode); // Update the internal state of this socket according to the error before throwing. SocketException socketException = SocketExceptionFactory.CreateSocketException((int)errorCode, endPointSnapshot); @@ -4346,6 +4356,8 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket throw socketException; } + SocketsTelemetry.Log.ConnectStop(); + if (_rightEndPoint == null) { // Save a copy of the EndPoint so we can use it for Create(). diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs index e8857e5bfcf229..6ec6d92e9a7755 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs @@ -554,6 +554,9 @@ internal void StartOperationConnect(MultipleConnectAsync? multipleConnect, bool _multipleConnect = multipleConnect; _connectSocket = null; _userSocket = userSocket; + + // Log only the actual connect operation to a remote endpoint. + if (multipleConnect == null) SocketsTelemetry.Log.ConnectStart(_socketAddress!); } internal void CancelConnectAsync() @@ -567,6 +570,9 @@ internal void CancelConnectAsync() } else { + SocketsTelemetry.Log.ConnectCancelled(); + SocketsTelemetry.Log.ConnectStop(); + // Otherwise we're doing a normal ConnectAsync - cancel it by closing the socket. // _currentSocket will only be null if _multipleConnect was set, so we don't have to check. if (_currentSocket == null) @@ -582,6 +588,12 @@ internal void FinishOperationSyncFailure(SocketError socketError, int bytesTrans { SetResults(socketError, bytesTransferred, flags); + if (_multipleConnect == null && _completedOperation == SocketAsyncOperation.Connect) + { + SocketsTelemetry.Log.ConnectFailed(); + SocketsTelemetry.Log.ConnectStop(); + } + // This will be null if we're doing a static ConnectAsync to a DnsEndPoint with AddressFamily.Unspecified; // the attempt socket will be closed anyways, so not updating the state is OK. // If we're doing a static ConnectAsync to an IPEndPoint, we need to dispose @@ -720,12 +732,17 @@ internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags catch (ObjectDisposedException) { } } + SocketsTelemetry.Log.ConnectStop(); + // Mark socket connected. _currentSocket!.SetToConnected(); _connectSocket = _currentSocket; } else { + SocketsTelemetry.Log.ConnectFailed(); + SocketsTelemetry.Log.ConnectStop(); + SetResults(socketError, bytesTransferred, flags); _currentSocket!.UpdateStatusAfterSocketError(socketError); } diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs new file mode 100644 index 00000000000000..c522c2556f5142 --- /dev/null +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -0,0 +1,83 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics.Tracing; + +namespace System.Net.Sockets +{ + [EventSource(Name = "System.Net.Sockets")] + internal sealed class SocketsTelemetry : EventSource + { + public static readonly SocketsTelemetry Log = new SocketsTelemetry(); + + [NonEvent] + public void ConnectStart(Internals.SocketAddress address) + { + if (IsEnabled()) + { + ConnectStart(address.ToString()); + } + } + + [NonEvent] + public void ConnectStart(EndPoint address) + { + if (IsEnabled()) + { + var addressString = address.ToString(); + if (addressString != null) + { + ConnectStart(addressString); + } + } + } + + [Event(1, Level = EventLevel.Informational)] + public void ConnectStart(string address) + { + WriteSocketEvent(eventId: 1, address); + } + + [Event(2, Level = EventLevel.Informational)] + public void ConnectStop() + { + WriteEvent(eventId: 2); + } + + [Event(3, Level = EventLevel.Error)] + public void ConnectFailed() + { + WriteEvent(eventId: 3); + } + + [Event(4, Level = EventLevel.Warning)] + public void ConnectCancelled() + { + WriteEvent(eventId: 4); + } + + [NonEvent] + private unsafe void WriteSocketEvent(int eventId, string address) + { + if (IsEnabled()) + { + if (address == null) address = ""; + + fixed (char* addressPtr = address) + { + const int NumEventDatas = 1; + var descrs = stackalloc EventData[NumEventDatas]; + + descrs[0] = new EventData + { + DataPointer = (IntPtr)addressPtr, + Size = (address.Length + 1) * sizeof(char) + }; + + WriteEventCore(eventId, NumEventDatas, descrs); + } + } + } + } +} From aec1bc4d26efc1b72e6d2b7f3e420a3b8a8635bb Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Tue, 30 Jun 2020 18:03:51 +0200 Subject: [PATCH 02/11] IsEnabled is called for the specific EventLevel --- .../src/System/Net/Sockets/SocketsTelemetry.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs index c522c2556f5142..2eadbe48d937ef 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -14,7 +14,7 @@ internal sealed class SocketsTelemetry : EventSource [NonEvent] public void ConnectStart(Internals.SocketAddress address) { - if (IsEnabled()) + if (IsEnabled(EventLevel.Informational, EventKeywords.All)) { ConnectStart(address.ToString()); } @@ -23,7 +23,7 @@ public void ConnectStart(Internals.SocketAddress address) [NonEvent] public void ConnectStart(EndPoint address) { - if (IsEnabled()) + if (IsEnabled(EventLevel.Informational, EventKeywords.All)) { var addressString = address.ToString(); if (addressString != null) @@ -36,7 +36,7 @@ public void ConnectStart(EndPoint address) [Event(1, Level = EventLevel.Informational)] public void ConnectStart(string address) { - WriteSocketEvent(eventId: 1, address); + WriteSocketEvent(EventLevel.Informational, eventId: 1, address); } [Event(2, Level = EventLevel.Informational)] @@ -58,9 +58,9 @@ public void ConnectCancelled() } [NonEvent] - private unsafe void WriteSocketEvent(int eventId, string address) + private unsafe void WriteSocketEvent(EventLevel level, int eventId, string address) { - if (IsEnabled()) + if (IsEnabled(level, EventKeywords.All)) { if (address == null) address = ""; From b4b42fe8eba8ea7bb4bf0586aa6941b4f43a244b Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Tue, 30 Jun 2020 18:09:44 +0200 Subject: [PATCH 03/11] .csproj formatting reverted --- .../src/System.Net.Sockets.csproj | 318 ++++++++++++------ 1 file changed, 212 insertions(+), 106 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj index 3367c6767b28e2..192472bb8cc963 100644 --- a/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj +++ b/src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj @@ -49,29 +49,48 @@ - + - - + + - + - - - - - - - - - + + + + + + + + + - - - - - - + + + + + + @@ -88,47 +107,87 @@ - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -144,53 +203,100 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 7002376edd07718ddc9d8d0884dbb74158432f82 Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Wed, 1 Jul 2020 14:05:25 +0200 Subject: [PATCH 04/11] - IsEnabled is checked before invoking a telemetry method - ConnectFailed always calls ConnectStop - Redundant Write method removed --- .../src/System/Net/Sockets/Socket.cs | 13 +++-- .../Net/Sockets/SocketAsyncEventArgs.cs | 18 +++---- .../System/Net/Sockets/SocketsTelemetry.cs | 48 +++++-------------- 3 files changed, 27 insertions(+), 52 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index de1e6a5a9ed09f..346985958c46d2 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Tracing; using System.Globalization; using System.IO; using System.Net.Internals; @@ -2158,7 +2159,7 @@ private bool CanUseConnectEx(EndPoint remoteEP) internal IAsyncResult UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback? callback, object? state, bool flowContext = false) { - SocketsTelemetry.Log.ConnectStart(remoteEP); + if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStart(remoteEP); if (CanUseConnectEx(remoteEP)) { @@ -2426,8 +2427,7 @@ asyncResult as MultipleAddressConnectAsyncResult ?? Exception? ex = castedAsyncResult.Result as Exception; if (ex != null || (SocketError)castedAsyncResult.ErrorCode != SocketError.Success) { - SocketsTelemetry.Log.ConnectFailed(); - SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.IsEnabled(EventLevel.Error)) SocketsTelemetry.Log.ConnectFailed(); if (ex == null) { @@ -2443,7 +2443,7 @@ asyncResult as MultipleAddressConnectAsyncResult ?? ExceptionDispatchInfo.Throw(ex); } - SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStop(); if (NetEventSource.IsEnabled) { @@ -4345,8 +4345,7 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket // Throw an appropriate SocketException if the native call fails. if (errorCode != SocketError.Success) { - SocketsTelemetry.Log.ConnectFailed(); - SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.IsEnabled(EventLevel.Error)) SocketsTelemetry.Log.ConnectFailed(); UpdateConnectSocketErrorForDisposed(ref errorCode); // Update the internal state of this socket according to the error before throwing. @@ -4356,7 +4355,7 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket throw socketException; } - SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStop(); if (_rightEndPoint == null) { diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs index 6ec6d92e9a7755..b9f3d956aa8b4e 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.Tracing; using System.Runtime.InteropServices; using System.Threading; @@ -556,7 +557,7 @@ internal void StartOperationConnect(MultipleConnectAsync? multipleConnect, bool _userSocket = userSocket; // Log only the actual connect operation to a remote endpoint. - if (multipleConnect == null) SocketsTelemetry.Log.ConnectStart(_socketAddress!); + if (SocketsTelemetry.IsEnabled(EventLevel.Informational) && multipleConnect == null) SocketsTelemetry.Log.ConnectStart(_socketAddress!); } internal void CancelConnectAsync() @@ -570,8 +571,11 @@ internal void CancelConnectAsync() } else { - SocketsTelemetry.Log.ConnectCancelled(); - SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.IsEnabled(EventLevel.Warning)) + { + SocketsTelemetry.Log.ConnectCancelled(); + SocketsTelemetry.Log.ConnectStop(); + } // Otherwise we're doing a normal ConnectAsync - cancel it by closing the socket. // _currentSocket will only be null if _multipleConnect was set, so we don't have to check. @@ -588,10 +592,9 @@ internal void FinishOperationSyncFailure(SocketError socketError, int bytesTrans { SetResults(socketError, bytesTransferred, flags); - if (_multipleConnect == null && _completedOperation == SocketAsyncOperation.Connect) + if (SocketsTelemetry.IsEnabled(EventLevel.Error) && _multipleConnect == null && _completedOperation == SocketAsyncOperation.Connect) { SocketsTelemetry.Log.ConnectFailed(); - SocketsTelemetry.Log.ConnectStop(); } // This will be null if we're doing a static ConnectAsync to a DnsEndPoint with AddressFamily.Unspecified; @@ -732,7 +735,7 @@ internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags catch (ObjectDisposedException) { } } - SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStop(); // Mark socket connected. _currentSocket!.SetToConnected(); @@ -740,8 +743,7 @@ internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags } else { - SocketsTelemetry.Log.ConnectFailed(); - SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.IsEnabled(EventLevel.Error)) SocketsTelemetry.Log.ConnectFailed(); SetResults(socketError, bytesTransferred, flags); _currentSocket!.UpdateStatusAfterSocketError(socketError); diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs index 2eadbe48d937ef..6cf5f8db4c9be7 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -11,32 +11,28 @@ internal sealed class SocketsTelemetry : EventSource { public static readonly SocketsTelemetry Log = new SocketsTelemetry(); + [NonEvent] + public static bool IsEnabled(EventLevel level) + { + return Log.IsEnabled(level, EventKeywords.All); + } + [NonEvent] public void ConnectStart(Internals.SocketAddress address) { - if (IsEnabled(EventLevel.Informational, EventKeywords.All)) - { - ConnectStart(address.ToString()); - } + ConnectStart(address.ToString()); } [NonEvent] public void ConnectStart(EndPoint address) { - if (IsEnabled(EventLevel.Informational, EventKeywords.All)) - { - var addressString = address.ToString(); - if (addressString != null) - { - ConnectStart(addressString); - } - } + ConnectStart(address.ToString()); } [Event(1, Level = EventLevel.Informational)] - public void ConnectStart(string address) + public void ConnectStart(string? address) { - WriteSocketEvent(EventLevel.Informational, eventId: 1, address); + WriteEvent(eventId: 1, address ?? ""); } [Event(2, Level = EventLevel.Informational)] @@ -49,6 +45,7 @@ public void ConnectStop() public void ConnectFailed() { WriteEvent(eventId: 3); + ConnectStop(); } [Event(4, Level = EventLevel.Warning)] @@ -56,28 +53,5 @@ public void ConnectCancelled() { WriteEvent(eventId: 4); } - - [NonEvent] - private unsafe void WriteSocketEvent(EventLevel level, int eventId, string address) - { - if (IsEnabled(level, EventKeywords.All)) - { - if (address == null) address = ""; - - fixed (char* addressPtr = address) - { - const int NumEventDatas = 1; - var descrs = stackalloc EventData[NumEventDatas]; - - descrs[0] = new EventData - { - DataPointer = (IntPtr)addressPtr, - Size = (address.Length + 1) * sizeof(char) - }; - - WriteEventCore(eventId, NumEventDatas, descrs); - } - } - } } } From c693fe77e2780299b48acdb9e0057269b0791699 Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Fri, 3 Jul 2020 13:03:50 +0200 Subject: [PATCH 05/11] ConnectStartInternal --- .../src/System/Net/Sockets/SocketsTelemetry.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs index 6cf5f8db4c9be7..06e7a228e9439f 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -38,14 +38,14 @@ public void ConnectStart(string? address) [Event(2, Level = EventLevel.Informational)] public void ConnectStop() { - WriteEvent(eventId: 2); + ConnectStopInternal(); } [Event(3, Level = EventLevel.Error)] public void ConnectFailed() { WriteEvent(eventId: 3); - ConnectStop(); + ConnectStopInternal(); } [Event(4, Level = EventLevel.Warning)] @@ -53,5 +53,11 @@ public void ConnectCancelled() { WriteEvent(eventId: 4); } + + [NonEvent] + private void ConnectStopInternal() + { + WriteEvent(eventId: 2); + } } } From a6d7e3dee8c043a5dae4c02bfb9816054c2380a9 Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Fri, 3 Jul 2020 14:49:57 +0200 Subject: [PATCH 06/11] - Cancelled to Canceled - ConnectCanceled logs Stop --- .../src/System/Net/Sockets/SocketAsyncEventArgs.cs | 3 +-- .../src/System/Net/Sockets/SocketsTelemetry.cs | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs index b9f3d956aa8b4e..c6918a5461efeb 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs @@ -573,8 +573,7 @@ internal void CancelConnectAsync() { if (SocketsTelemetry.IsEnabled(EventLevel.Warning)) { - SocketsTelemetry.Log.ConnectCancelled(); - SocketsTelemetry.Log.ConnectStop(); + SocketsTelemetry.Log.ConnectCanceled(); } // Otherwise we're doing a normal ConnectAsync - cancel it by closing the socket. diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs index 06e7a228e9439f..cae93de1d1963f 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -49,9 +49,10 @@ public void ConnectFailed() } [Event(4, Level = EventLevel.Warning)] - public void ConnectCancelled() + public void ConnectCanceled() { WriteEvent(eventId: 4); + ConnectStopInternal(); } [NonEvent] From 245458e2fba8495b6f8b0d406dc511d4f4fdb8b7 Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Mon, 13 Jul 2020 13:48:25 +0200 Subject: [PATCH 07/11] Log.IsEnabled check made more limker trimming-friendly --- .../src/System/Net/Sockets/Socket.cs | 10 +++---- .../System/Net/Sockets/SocketsTelemetry.cs | 30 +++++++++++-------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index 346985958c46d2..6584f29895e042 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -2159,7 +2159,7 @@ private bool CanUseConnectEx(EndPoint remoteEP) internal IAsyncResult UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback? callback, object? state, bool flowContext = false) { - if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStart(remoteEP); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectStart(remoteEP); if (CanUseConnectEx(remoteEP)) { @@ -2427,7 +2427,7 @@ asyncResult as MultipleAddressConnectAsyncResult ?? Exception? ex = castedAsyncResult.Result as Exception; if (ex != null || (SocketError)castedAsyncResult.ErrorCode != SocketError.Success) { - if (SocketsTelemetry.IsEnabled(EventLevel.Error)) SocketsTelemetry.Log.ConnectFailed(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(); if (ex == null) { @@ -2443,7 +2443,7 @@ asyncResult as MultipleAddressConnectAsyncResult ?? ExceptionDispatchInfo.Throw(ex); } - if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectStop(); if (NetEventSource.IsEnabled) { @@ -4345,7 +4345,7 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket // Throw an appropriate SocketException if the native call fails. if (errorCode != SocketError.Success) { - if (SocketsTelemetry.IsEnabled(EventLevel.Error)) SocketsTelemetry.Log.ConnectFailed(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(); UpdateConnectSocketErrorForDisposed(ref errorCode); // Update the internal state of this socket according to the error before throwing. @@ -4355,7 +4355,7 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket throw socketException; } - if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectStop(); if (_rightEndPoint == null) { diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs index cae93de1d1963f..35b2c59b3fb5ee 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -11,12 +11,6 @@ internal sealed class SocketsTelemetry : EventSource { public static readonly SocketsTelemetry Log = new SocketsTelemetry(); - [NonEvent] - public static bool IsEnabled(EventLevel level) - { - return Log.IsEnabled(level, EventKeywords.All); - } - [NonEvent] public void ConnectStart(Internals.SocketAddress address) { @@ -32,7 +26,10 @@ public void ConnectStart(EndPoint address) [Event(1, Level = EventLevel.Informational)] public void ConnectStart(string? address) { - WriteEvent(eventId: 1, address ?? ""); + if (IsEnabled(EventLevel.Informational, EventKeywords.All)) + { + WriteEvent(eventId: 1, address ?? ""); + } } [Event(2, Level = EventLevel.Informational)] @@ -44,21 +41,30 @@ public void ConnectStop() [Event(3, Level = EventLevel.Error)] public void ConnectFailed() { - WriteEvent(eventId: 3); - ConnectStopInternal(); + if (IsEnabled(EventLevel.Error, EventKeywords.All)) + { + WriteEvent(eventId: 3); + ConnectStopInternal(); + } } [Event(4, Level = EventLevel.Warning)] public void ConnectCanceled() { - WriteEvent(eventId: 4); - ConnectStopInternal(); + if (IsEnabled(EventLevel.Warning, EventKeywords.All)) + { + WriteEvent(eventId: 4); + ConnectStopInternal(); + } } [NonEvent] private void ConnectStopInternal() { - WriteEvent(eventId: 2); + if (IsEnabled(EventLevel.Informational, EventKeywords.All)) + { + WriteEvent(eventId: 2); + } } } } From abd9c9d468a32945582f2e61cf93d5d363d4efca Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Mon, 13 Jul 2020 14:00:44 +0200 Subject: [PATCH 08/11] Remaining SocketsTelemetry.IsEnabled calls fixed --- .../src/System/Net/Sockets/SocketAsyncEventArgs.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs index 359e4577a4b566..92c1c279be00a6 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs @@ -556,7 +556,7 @@ internal void StartOperationConnect(MultipleConnectAsync? multipleConnect, bool _userSocket = userSocket; // Log only the actual connect operation to a remote endpoint. - if (SocketsTelemetry.IsEnabled(EventLevel.Informational) && multipleConnect == null) SocketsTelemetry.Log.ConnectStart(_socketAddress!); + if (SocketsTelemetry.Log.IsEnabled() && multipleConnect == null) SocketsTelemetry.Log.ConnectStart(_socketAddress!); } internal void CancelConnectAsync() @@ -570,10 +570,7 @@ internal void CancelConnectAsync() } else { - if (SocketsTelemetry.IsEnabled(EventLevel.Warning)) - { - SocketsTelemetry.Log.ConnectCanceled(); - } + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectCanceled(); // Otherwise we're doing a normal ConnectAsync - cancel it by closing the socket. // _currentSocket will only be null if _multipleConnect was set, so we don't have to check. @@ -590,7 +587,7 @@ internal void FinishOperationSyncFailure(SocketError socketError, int bytesTrans { SetResults(socketError, bytesTransferred, flags); - if (SocketsTelemetry.IsEnabled(EventLevel.Error) && _multipleConnect == null && _completedOperation == SocketAsyncOperation.Connect) + if (SocketsTelemetry.Log.IsEnabled() && _multipleConnect == null && _completedOperation == SocketAsyncOperation.Connect) { SocketsTelemetry.Log.ConnectFailed(); } @@ -733,7 +730,7 @@ internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags catch (ObjectDisposedException) { } } - if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStop(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectStop(); // Mark socket connected. _currentSocket!.SetToConnected(); @@ -741,7 +738,7 @@ internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags } else { - if (SocketsTelemetry.IsEnabled(EventLevel.Error)) SocketsTelemetry.Log.ConnectFailed(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(); SetResults(socketError, bytesTransferred, flags); _currentSocket!.UpdateStatusAfterSocketError(socketError); From ecce3af5a9c48a95d9331d24daeae00c97713e33 Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Mon, 13 Jul 2020 15:17:21 +0200 Subject: [PATCH 09/11] ConnectFailure's details are logged --- .../System.Net.Sockets/src/System/Net/Sockets/Socket.cs | 4 ++-- .../src/System/Net/Sockets/SocketAsyncEventArgs.cs | 4 ++-- .../src/System/Net/Sockets/SocketsTelemetry.cs | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index d6ef175c3cec20..c5ccdce568568a 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -2323,7 +2323,7 @@ asyncResult as MultipleAddressConnectAsyncResult ?? Exception? ex = castedAsyncResult.Result as Exception; if (ex != null || (SocketError)castedAsyncResult.ErrorCode != SocketError.Success) { - if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed((SocketError)castedAsyncResult.ErrorCode, ex); if (ex == null) { @@ -4133,7 +4133,7 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket // Throw an appropriate SocketException if the native call fails. if (errorCode != SocketError.Success) { - if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(errorCode, null); UpdateConnectSocketErrorForDisposed(ref errorCode); // Update the internal state of this socket according to the error before throwing. diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs index 92c1c279be00a6..03d1770883147e 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs @@ -589,7 +589,7 @@ internal void FinishOperationSyncFailure(SocketError socketError, int bytesTrans if (SocketsTelemetry.Log.IsEnabled() && _multipleConnect == null && _completedOperation == SocketAsyncOperation.Connect) { - SocketsTelemetry.Log.ConnectFailed(); + SocketsTelemetry.Log.ConnectFailed(socketError, null); } // This will be null if we're doing a static ConnectAsync to a DnsEndPoint with AddressFamily.Unspecified; @@ -738,7 +738,7 @@ internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags } else { - if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(socketError, null); SetResults(socketError, bytesTransferred, flags); _currentSocket!.UpdateStatusAfterSocketError(socketError); diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs index 35b2c59b3fb5ee..085dc947f4fe7f 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -39,11 +39,12 @@ public void ConnectStop() } [Event(3, Level = EventLevel.Error)] - public void ConnectFailed() + public void ConnectFailed(SocketError error, Exception? exception) { if (IsEnabled(EventLevel.Error, EventKeywords.All)) { - WriteEvent(eventId: 3); + string message = exception?.Message ?? string.Empty; + WriteEvent(eventId: 3, (int)error, message); ConnectStopInternal(); } } From 6c305f2df8e808517c82cb61f0e227e3fcf8841e Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Tue, 14 Jul 2020 13:14:41 +0200 Subject: [PATCH 10/11] - ConnectStart for sync flow is added - Functional tests --- .../src/System/Net/Sockets/Socket.cs | 4 +- .../System/Net/Sockets/SocketsTelemetry.cs | 6 +- .../System.Net.Sockets.Tests.csproj | 1 + .../tests/FunctionalTests/TelemetryTest.cs | 67 +++++++++++++++++++ 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index c5ccdce568568a..3aae0e067df122 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -2323,7 +2323,7 @@ asyncResult as MultipleAddressConnectAsyncResult ?? Exception? ex = castedAsyncResult.Result as Exception; if (ex != null || (SocketError)castedAsyncResult.ErrorCode != SocketError.Success) { - if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed((SocketError)castedAsyncResult.ErrorCode, ex); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed((SocketError)castedAsyncResult.ErrorCode, ex?.Message); if (ex == null) { @@ -4128,6 +4128,8 @@ static void InitializeSocketsCore() private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socketAddress) { + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectStart(socketAddress); + SocketError errorCode = SocketPal.Connect(_handle, socketAddress.Buffer, socketAddress.Size); // Throw an appropriate SocketException if the native call fails. diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs index 085dc947f4fe7f..c94ffd9712a4ae 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -1,6 +1,5 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. using System.Diagnostics.Tracing; @@ -39,12 +38,11 @@ public void ConnectStop() } [Event(3, Level = EventLevel.Error)] - public void ConnectFailed(SocketError error, Exception? exception) + public void ConnectFailed(SocketError error, string? exceptionMessage) { if (IsEnabled(EventLevel.Error, EventKeywords.All)) { - string message = exception?.Message ?? string.Empty; - WriteEvent(eventId: 3, (int)error, message); + WriteEvent(eventId: 3, (int)error, exceptionMessage ?? string.Empty); ConnectStopInternal(); } } diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/System.Net.Sockets.Tests.csproj b/src/libraries/System.Net.Sockets/tests/FunctionalTests/System.Net.Sockets.Tests.csproj index 6a6d63b4c6a4f6..73e4a078e1b7c0 100644 --- a/src/libraries/System.Net.Sockets/tests/FunctionalTests/System.Net.Sockets.Tests.csproj +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/System.Net.Sockets.Tests.csproj @@ -39,6 +39,7 @@ + diff --git a/src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs b/src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs new file mode 100644 index 00000000000000..33529f980d225f --- /dev/null +++ b/src/libraries/System.Net.Sockets/tests/FunctionalTests/TelemetryTest.cs @@ -0,0 +1,67 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Concurrent; +using System.Diagnostics.Tracing; +using Microsoft.DotNet.RemoteExecutor; +using Xunit; +using Xunit.Abstractions; + +namespace System.Net.Sockets.Tests +{ + public class TelemetryTest + { + public readonly ITestOutputHelper _output; + + public TelemetryTest(ITestOutputHelper output) + { + _output = output; + } + + [Fact] + public static void EventSource_ExistsWithCorrectId() + { + Type esType = typeof(Socket).Assembly.GetType("System.Net.Sockets.SocketsTelemetry", throwOnError: true, ignoreCase: false); + Assert.NotNull(esType); + + Assert.Equal("System.Net.Sockets", EventSource.GetName(esType)); + Assert.Equal(Guid.Parse("d5b2e7d4-b6ec-50ae-7cde-af89427ad21f"), EventSource.GetGuid(esType)); + + Assert.NotEmpty(EventSource.GenerateManifest(esType, esType.Assembly.Location)); + } + + [OuterLoop] + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] + public void EventSource_EventsRaisedAsExpected() + { + RemoteExecutor.Invoke(() => + { + using (var listener = new TestEventListener("System.Net.Sockets", EventLevel.Verbose)) + { + var events = new ConcurrentQueue(); + listener.RunWithCallback(events.Enqueue, () => + { + // Invoke several tests to execute code paths while tracing is enabled + + new SendReceiveSync(null).SendRecv_Stream_TCP(IPAddress.Loopback, false).GetAwaiter(); + new SendReceiveSync(null).SendRecv_Stream_TCP(IPAddress.Loopback, true).GetAwaiter(); + + new SendReceiveTask(null).SendRecv_Stream_TCP(IPAddress.Loopback, false).GetAwaiter(); + new SendReceiveTask(null).SendRecv_Stream_TCP(IPAddress.Loopback, true).GetAwaiter(); + + new SendReceiveEap(null).SendRecv_Stream_TCP(IPAddress.Loopback, false).GetAwaiter(); + new SendReceiveEap(null).SendRecv_Stream_TCP(IPAddress.Loopback, true).GetAwaiter(); + + new SendReceiveApm(null).SendRecv_Stream_TCP(IPAddress.Loopback, false).GetAwaiter(); + new SendReceiveApm(null).SendRecv_Stream_TCP(IPAddress.Loopback, true).GetAwaiter(); + + new NetworkStreamTest().CopyToAsync_AllDataCopied(4096, true).GetAwaiter().GetResult(); + new NetworkStreamTest().Timeout_ValidData_Roundtrips().GetAwaiter().GetResult(); + }); + Assert.DoesNotContain(events, ev => ev.EventId == 0); // errors from the EventSource itself + Assert.InRange(events.Count, 1, int.MaxValue); + } + }).Dispose(); + } + } +} From 33707e280dc76f8acd5d08a530a1348376ce075e Mon Sep 17 00:00:00 2001 From: Alexander Nikolaev Date: Tue, 14 Jul 2020 13:56:27 +0200 Subject: [PATCH 11/11] ConnectStop and ConnectFailed as well as ConnectStop and ConnectCanceled are logged via public non-event methods --- .../src/System/Net/Sockets/Socket.cs | 4 +- .../Net/Sockets/SocketAsyncEventArgs.cs | 6 +-- .../System/Net/Sockets/SocketsTelemetry.cs | 46 +++++++++++-------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index 3aae0e067df122..e75f084e1c9a61 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -2323,7 +2323,7 @@ asyncResult as MultipleAddressConnectAsyncResult ?? Exception? ex = castedAsyncResult.Result as Exception; if (ex != null || (SocketError)castedAsyncResult.ErrorCode != SocketError.Success) { - if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed((SocketError)castedAsyncResult.ErrorCode, ex?.Message); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailedAndStop((SocketError)castedAsyncResult.ErrorCode, ex?.Message); if (ex == null) { @@ -4135,7 +4135,7 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket // Throw an appropriate SocketException if the native call fails. if (errorCode != SocketError.Success) { - if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(errorCode, null); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailedAndStop(errorCode, null); UpdateConnectSocketErrorForDisposed(ref errorCode); // Update the internal state of this socket according to the error before throwing. diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs index 03d1770883147e..213cb30909b99a 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.cs @@ -570,7 +570,7 @@ internal void CancelConnectAsync() } else { - if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectCanceled(); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectCanceledAndStop(); // Otherwise we're doing a normal ConnectAsync - cancel it by closing the socket. // _currentSocket will only be null if _multipleConnect was set, so we don't have to check. @@ -589,7 +589,7 @@ internal void FinishOperationSyncFailure(SocketError socketError, int bytesTrans if (SocketsTelemetry.Log.IsEnabled() && _multipleConnect == null && _completedOperation == SocketAsyncOperation.Connect) { - SocketsTelemetry.Log.ConnectFailed(socketError, null); + SocketsTelemetry.Log.ConnectFailedAndStop(socketError, null); } // This will be null if we're doing a static ConnectAsync to a DnsEndPoint with AddressFamily.Unspecified; @@ -738,7 +738,7 @@ internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags } else { - if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed(socketError, null); + if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailedAndStop(socketError, null); SetResults(socketError, bytesTransferred, flags); _currentSocket!.UpdateStatusAfterSocketError(socketError); diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs index c94ffd9712a4ae..7f0e2a78eb749a 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketsTelemetry.cs @@ -10,18 +10,6 @@ internal sealed class SocketsTelemetry : EventSource { public static readonly SocketsTelemetry Log = new SocketsTelemetry(); - [NonEvent] - public void ConnectStart(Internals.SocketAddress address) - { - ConnectStart(address.ToString()); - } - - [NonEvent] - public void ConnectStart(EndPoint address) - { - ConnectStart(address.ToString()); - } - [Event(1, Level = EventLevel.Informational)] public void ConnectStart(string? address) { @@ -34,7 +22,10 @@ public void ConnectStart(string? address) [Event(2, Level = EventLevel.Informational)] public void ConnectStop() { - ConnectStopInternal(); + if (IsEnabled(EventLevel.Informational, EventKeywords.All)) + { + WriteEvent(eventId: 2); + } } [Event(3, Level = EventLevel.Error)] @@ -43,7 +34,6 @@ public void ConnectFailed(SocketError error, string? exceptionMessage) if (IsEnabled(EventLevel.Error, EventKeywords.All)) { WriteEvent(eventId: 3, (int)error, exceptionMessage ?? string.Empty); - ConnectStopInternal(); } } @@ -53,17 +43,33 @@ public void ConnectCanceled() if (IsEnabled(EventLevel.Warning, EventKeywords.All)) { WriteEvent(eventId: 4); - ConnectStopInternal(); } } [NonEvent] - private void ConnectStopInternal() + public void ConnectStart(Internals.SocketAddress address) { - if (IsEnabled(EventLevel.Informational, EventKeywords.All)) - { - WriteEvent(eventId: 2); - } + ConnectStart(address.ToString()); + } + + [NonEvent] + public void ConnectStart(EndPoint address) + { + ConnectStart(address.ToString()); + } + + [NonEvent] + public void ConnectCanceledAndStop() + { + ConnectCanceled(); + ConnectStop(); + } + + [NonEvent] + public void ConnectFailedAndStop(SocketError error, string? exceptionMessage) + { + ConnectFailed(error, exceptionMessage); + ConnectStop(); } } }