From e9d6a976226936ce107d618bd0f93e36d31a92ac Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Tue, 19 Jan 2021 14:17:37 -0800 Subject: [PATCH 01/11] Step 3 : More traces --- .../Data/SqlClient/SNI/LocalDB.Windows.cs | 4 +- .../Microsoft/Data/SqlClient/SNI/SNICommon.cs | 23 +- .../Data/SqlClient/SNI/SNILoadHandle.cs | 2 +- .../Data/SqlClient/SNI/SNIMarsConnection.cs | 66 ++-- .../Data/SqlClient/SNI/SNIMarsHandle.cs | 90 ++++-- .../Data/SqlClient/SNI/SNINpHandle.cs | 65 ++-- .../Microsoft/Data/SqlClient/SNI/SNIPacket.cs | 57 +++- .../Data/SqlClient/SNI/SNIPhysicalHandle.cs | 16 +- .../Microsoft/Data/SqlClient/SNI/SNIProxy.cs | 18 +- .../Data/SqlClient/SNI/SNITcpHandle.cs | 283 +++++++++++------- .../src/Microsoft/Data/SqlClient/SNI/SSRP.cs | 2 +- .../Data/SqlClient/SNI/SslOverTdsStream.cs | 8 +- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 35 +-- .../Data/SqlClient/TdsParserStateObject.cs | 58 ++-- .../SqlClient/TdsParserStateObjectManaged.cs | 45 ++- .../Data/SqlClient/TdsParserStateObject.cs | 10 +- .../Data/SqlClient/SqlClientEventSource.cs | 158 +++------- 17 files changed, 533 insertions(+), 407 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs index 3df18d1148..d449b32235 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs @@ -26,7 +26,7 @@ internal sealed class LocalDB private IntPtr _startInstanceHandle = IntPtr.Zero; // Local Db api doc https://msdn.microsoft.com/en-us/library/hh217143.aspx - // HRESULT LocalDBStartInstance( [Input ] PCWSTR pInstanceName, [Input ] DWORD dwFlags,[Output] LPWSTR wszSqlConnection,[Input/Output] LPDWORD lpcchSqlConnection); + // HRESULT LocalDBStartInstance( [Input ] PCWSTR pInstanceName, [Input ] DWORD dwFlags,[Output] LPWSTR wszSqlConnection,[Input/Output] LPDWORD lpcchSqlConnection); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int LocalDBStartInstance( [In] [MarshalAs(UnmanagedType.LPWStr)] string localDBInstanceName, @@ -112,7 +112,7 @@ internal static string MapLocalDBErrorStateToErrorMessage(LocalDBErrorState erro /// private bool LoadUserInstanceDll() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.LocalDB.Windows.LoadUserInstanceDll | SNI | INFO | SCOPE | Entering Scope {0}"); try { // Check in a non thread-safe way if the handle is already set for performance. diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs index 475660aa2b..f42ff57c7d 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs @@ -126,33 +126,33 @@ internal class SNICommon internal const int LocalDBBadRuntime = 57; /// - /// Validate server certificate callback for SSL + /// We only validate Server name in Certificate to match with "targetServerName". + /// Certificate validation and chain trust validations are done by SSLStream class [System.Net.Security.SecureChannel.VerifyRemoteCertificate method] + /// This method is called as a result of callback for SSL Stream Certificate validation. /// /// Server that client is expecting to connect to - /// Sender object /// X.509 certificate - /// X.509 chain /// Policy errors /// True if certificate is valid - internal static bool ValidateSslServerCertificate(string targetServerName, object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors) + internal static bool ValidateSslServerCertificate(string targetServerName, X509Certificate cert, SslPolicyErrors policyErrors) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNICommon.ValidateSslServerCertificate | SNI | SCOPE | INFO | Entering Scope {0} "); try { if (policyErrors == SslPolicyErrors.None) { - SqlClientEventSource.Log.TrySNITraceEvent(" SSL Server certificate validated."); + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | INFO | targetServerName {0}, SSL Server certificate not validated as PolicyErrors set to None.", targetServerName); return true; } if ((policyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) != 0) { - SqlClientEventSource.Log.TrySNITraceEvent(" SSL Remote certificate name mismatched."); string certServerName = cert.Subject.Substring(cert.Subject.IndexOf('=') + 1); // Verify that target server name matches subject in the certificate if (targetServerName.Length > certServerName.Length) { + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, Target Server name is of greater length than Subject in Certificate.", targetServerName); return false; } else if (targetServerName.Length == certServerName.Length) @@ -160,6 +160,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, objec // Both strings have the same length, so targetServerName must be a FQDN if (!targetServerName.Equals(certServerName, StringComparison.OrdinalIgnoreCase)) { + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, Target Server name does not match Subject in Certificate.", targetServerName); return false; } } @@ -167,6 +168,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, objec { if (string.Compare(targetServerName, 0, certServerName, 0, targetServerName.Length, StringComparison.OrdinalIgnoreCase) != 0) { + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, Target Server name does not match Subject in Certificate.", targetServerName); return false; } @@ -176,6 +178,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, objec // (Names have different lengths, so the target server can't be a FQDN.) if (certServerName[targetServerName.Length] != '.') { + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, Target Server name does not match Subject in Certificate.", targetServerName); return false; } } @@ -183,8 +186,10 @@ internal static bool ValidateSslServerCertificate(string targetServerName, objec else { // Fail all other SslPolicy cases besides RemoteCertificateNameMismatch + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, SslPolicyError {1}, SSL Policy invalidated certificate.", targetServerName, policyErrors); return false; } + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | INFO | targetServerName {0}, Client certificate validated successfully.", targetServerName); return true; } finally @@ -203,7 +208,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, objec /// internal static uint ReportSNIError(SNIProviders provider, uint nativeError, uint sniError, string errorMessage) { - SqlClientEventSource.Log.TrySNITraceEvent(" Provider ={0}, native Error ={1}, SNI Error ={2}, Error Message ={3}", provider, nativeError, sniError, errorMessage); + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ReportSNIError | SNI | ERR | Provider = {0}, native Error = {1}, SNI Error = {2}, Error Message = {3}", provider, nativeError, sniError, errorMessage); return ReportSNIError(new SNIError(provider, nativeError, sniError, errorMessage)); } @@ -216,7 +221,7 @@ internal static uint ReportSNIError(SNIProviders provider, uint nativeError, uin /// internal static uint ReportSNIError(SNIProviders provider, uint sniError, Exception sniException) { - SqlClientEventSource.Log.TrySNITraceEvent(" Provider ={0}, SNI Error ={1}, Exception ={2}", provider, sniError, sniException.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ReportSNIError | SNI | ERR | Provider = {0}, SNI Error = {1}, Exception = {2}", provider, sniError, sniException.Message); return ReportSNIError(new SNIError(provider, sniError, sniException)); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNILoadHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNILoadHandle.cs index 6e885462e5..bfada20446 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNILoadHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNILoadHandle.cs @@ -30,7 +30,7 @@ public SNIError LastError set { - SqlClientEventSource.Log.TrySNITraceEvent(" Last Error Value = {0}", value); + SqlClientEventSource.Log.TrySNITraceEvent("SNILoadHandle.LastError | SNI | INFO | SETTER | Last Error Value = {0}", value?.errorMessage); _lastError.Value = value; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs index 19a4f8ea2b..6d36c4282f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs @@ -44,6 +44,7 @@ public Guid ConnectionId public SNIMarsConnection(SNIHandle lowerHandle) { _lowerHandle = lowerHandle; + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.ctor | SNI | INFO | Trace | Created MARS Session Id {0}", ConnectionId); _lowerHandle.SetAsyncCallbacks(HandleReceiveComplete, HandleSendComplete); } @@ -54,6 +55,7 @@ public SNIMarsHandle CreateMarsSession(object callbackObject, bool async) ushort sessionId = _nextSessionId++; SNIMarsHandle handle = new SNIMarsHandle(this, sessionId, callbackObject, async); _sessions.Add(sessionId, handle); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.CreateMarsSession | SNI | INFO | Trace | MARS Session Id {0}, SNI MARS Handle Id {1}, created new MARS Session {2}", ConnectionId, handle?.ConnectionId, sessionId); return handle; } } @@ -64,17 +66,17 @@ public SNIMarsHandle CreateMarsSession(object callbackObject, bool async) /// public uint StartReceive() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(" StartReceive"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.StartReceive | SNI | INFO | SCOPE | Entering Scope {0}"); try { SNIPacket packet = null; if (ReceiveAsync(ref packet) == TdsEnums.SNI_SUCCESS_IO_PENDING) { - SqlClientEventSource.Log.TrySNITraceEvent(" Success IO pending."); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.StartReceive | SNI | INFO | Trace | MARS Session Id {0}, Success IO pending.", ConnectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } - SqlClientEventSource.Log.TrySNITraceEvent(" Connection not useable."); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.StartReceive | SNI | ERR | MARS Session Id {0}, Connection not usable.", ConnectionId); return SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnNotUsableError, Strings.SNI_ERROR_19); } finally @@ -90,7 +92,7 @@ public uint StartReceive() /// SNI error code public uint Send(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(" Send"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.Send | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (this) @@ -112,7 +114,7 @@ public uint Send(SNIPacket packet) /// SNI error code public uint SendAsync(SNIPacket packet, SNIAsyncCallback callback) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(" SendAsync"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (this) @@ -133,18 +135,25 @@ public uint SendAsync(SNIPacket packet, SNIAsyncCallback callback) /// SNI error code public uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(" SendAsync"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0} "); try { if (packet != null) { ReturnPacket(packet); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.ReceiveAsync | SNI | INFO | Trace | MARS Session Id {0}, Packet {1} returned", ConnectionId, packet?._id); +#endif packet = null; } lock (this) { - return _lowerHandle.ReceiveAsync(ref packet); + var response = _lowerHandle.ReceiveAsync(ref packet); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.ReceiveAsync | SNI | INFO | Trace | MARS Session Id {0}, Received new packet {1}", ConnectionId, packet?._id); +#endif + return response; } } finally @@ -159,7 +168,7 @@ public uint ReceiveAsync(ref SNIPacket packet) /// SNI error status public uint CheckConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0} "); try { lock (this) @@ -179,15 +188,18 @@ public uint CheckConnection() public void HandleReceiveError(SNIPacket packet) { Debug.Assert(Monitor.IsEntered(this), "HandleReceiveError was called without being locked."); - if (!Monitor.IsEntered(this)) - { - SqlClientEventSource.Log.TrySNITraceEvent(" HandleReceiveError was called without being locked."); - } foreach (SNIMarsHandle handle in _sessions.Values) { if (packet.HasCompletionCallback) { handle.HandleReceiveError(packet); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveError | SNI | ERR | Trace | MARS Session Id {0}, Packet {1} has Completion Callback ", ConnectionId, packet?._id); + } + else + { + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveError | SNI | ERR | Trace | MARS Session Id {0}, Packet {1} does not have Completion Callback, error not handled.", ConnectionId, packet?._id); +#endif } } Debug.Assert(!packet.IsInvalid, "packet was returned by MarsConnection child, child sessions should not release the packet"); @@ -211,7 +223,7 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) /// SNI error code public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | SCOPE | Entering Scope {0} "); try { SNISMUXHeader currentHeader = null; @@ -223,7 +235,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) lock (this) { HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent(" not successful."); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, Handled receive error code: {1}", _lowerHandle?.ConnectionId, sniErrorCode); return; } } @@ -246,22 +258,25 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (bytesTaken == 0) { sniErrorCode = ReceiveAsync(ref packet); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | Trace | MARS Session Id {0}, Non-SMUX Header SNI Packet received with code {1}", ConnectionId, sniErrorCode); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { - SqlClientEventSource.Log.TrySNITraceEvent(" not successful."); return; } HandleReceiveError(packet); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, Handled receive error code: {1}", _lowerHandle?.ConnectionId, sniErrorCode); return; } } _currentHeader.Read(_headerBytes); - _dataBytesLeft = (int)_currentHeader.length; _currentPacket = _lowerHandle.RentPacket(headerSize: 0, dataSize: (int)_currentHeader.length); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, _dataBytesLeft {1}, _currentPacket {2}, Reading data of length: _currentHeader.length {3}", _lowerHandle?.ConnectionId, _dataBytesLeft, currentPacket?._id, _currentHeader?.length); +#endif } currentHeader = _currentHeader; @@ -277,6 +292,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (_dataBytesLeft > 0) { sniErrorCode = ReceiveAsync(ref packet); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | Trace | MARS Session Id {0}, SMUX DATA Header SNI Packet received with code {1}, _dataBytesLeft {2}", ConnectionId, sniErrorCode, _dataBytesLeft); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { @@ -284,6 +300,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } HandleReceiveError(packet); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, Handled receive error code: {1}", _lowerHandle?.ConnectionId, sniErrorCode); return; } } @@ -295,6 +312,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.InvalidParameterError, Strings.SNI_ERROR_5); HandleReceiveError(packet); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | Current Header Session Id {0} not found, MARS Session Id {1} will be destroyed, New SNI error created: {2}", _currentHeader?.sessionId, _lowerHandle?.ConnectionId, sniErrorCode); _lowerHandle.Dispose(); _lowerHandle = null; return; @@ -303,16 +321,19 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_FIN) { _sessions.Remove(_currentHeader.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | FIN | MARS Session Id {0}, SMUX_FIN flag received, Current Header Session Id {1} removed", _lowerHandle?.ConnectionId, _currentHeader?.sessionId); } else { currentSession = _sessions[_currentHeader.sessionId]; + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, Current Session assigned to Session Id {1}", _lowerHandle?.ConnectionId, _currentHeader?.sessionId); } } if (currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { currentSession.HandleReceiveComplete(currentPacket, currentHeader); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | SMUX_DATA | MARS Session Id {0}, Current Session {1} completed receiving Data", _lowerHandle?.ConnectionId, _currentHeader?.sessionId); } if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_ACK) @@ -320,13 +341,17 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) try { currentSession.HandleAck(currentHeader.highwater); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | SMUX_ACK | MARS Session Id {0}, Current Session {1} handled ack", _lowerHandle?.ConnectionId, _currentHeader?.sessionId); } catch (Exception e) { + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, Exception occurred: {2}", _currentHeader?.sessionId, e?.Message); SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } - +#if DEBUG Debug.Assert(_currentPacket == currentPacket, "current and _current are not the same"); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | SMUX_ACK | MARS Session Id {0}, Current Packet {1} returned", _lowerHandle?.ConnectionId, currentPacket?._id); +#endif ReturnPacket(currentPacket); currentPacket = null; _currentPacket = null; @@ -344,6 +369,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } HandleReceiveError(packet); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, packet.DataLeft 0, SNI error {2}", _lowerHandle?.ConnectionId, sniErrorCode); return; } } @@ -360,7 +386,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) /// public uint EnableSsl(uint options) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.EnableSsl | SNI | INFO | SCOPE | Entering Scope {0}"); try { return _lowerHandle.EnableSsl(options); @@ -376,7 +402,7 @@ public uint EnableSsl(uint options) /// public void DisableSsl() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.DisableSsl | SNI | INFO | SCOPE | Entering Scope {0}"); try { _lowerHandle.DisableSsl(); @@ -403,7 +429,7 @@ public void ReturnPacket(SNIPacket packet) /// public void KillConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.KillConnection | SNI | INFO | SCOPE | Entering Scope {0}"); try { _lowerHandle.KillConnection(); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs index c6fd0a7620..ea47745fba 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs @@ -21,7 +21,7 @@ internal sealed class SNIMarsHandle : SNIHandle private readonly Queue _receivedPacketQueue = new Queue(); private readonly Queue _sendPacketQueue = new Queue(); private readonly object _callbackObject; - private readonly Guid _connectionId = Guid.NewGuid(); + private readonly Guid _connectionId; private readonly ushort _sessionId; private readonly ManualResetEventSlim _packetEvent = new ManualResetEventSlim(false); private readonly ManualResetEventSlim _ackEvent = new ManualResetEventSlim(false); @@ -54,14 +54,15 @@ internal sealed class SNIMarsHandle : SNIHandle /// public override void Dispose() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.Dispose | SNI | INFO | SCOPE | Entering Scope {0}"); try { SendControlPacket(SNISMUXFlags.SMUX_FIN); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Dispose | SNI | INFO | MARS Session Id {0}, Sent SMUX_FIN packet to terminate session.", ConnectionId); } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent(" internal exception error = {0}, Member Name={1}", e.Message, e.GetType().Name); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Dispose | SNI | ERR | MARS Session Id {0}, Internal exception error = {1}, Member Name={2}", ConnectionId, e?.Message, e?.GetType()?.Name); SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } finally @@ -81,9 +82,11 @@ public SNIMarsHandle(SNIMarsConnection connection, ushort sessionId, object call { _sessionId = sessionId; _connection = connection; + _connectionId = connection.ConnectionId; _callbackObject = callbackObject; _handleSendCompleteCallback = HandleSendComplete; SendControlPacket(SNISMUXFlags.SMUX_SYN); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.ctor | SNI | INFO | MARS Session Id {0}, Sent SMUX_SYN packet to start a new session, session Id {1}", ConnectionId, _sessionId); _status = TdsEnums.SNI_SUCCESS; } @@ -93,11 +96,13 @@ public SNIMarsHandle(SNIMarsConnection connection, ushort sessionId, object call /// SMUX header flags private void SendControlPacket(SNISMUXFlags flags) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.SendControlPacket | SNI | INFO | SCOPE | Entering Scope {0}"); try { SNIPacket packet = RentPacket(headerSize: SNISMUXHeader.HEADER_LENGTH, dataSize: 0); - +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendControlPacket | SNI | INFO | MARS Session Id {0}, Packet rented {1}, packet dataLeft {2}", ConnectionId, packet?._id, packet?.DataLeft); +#endif lock (this) { SetupSMUXHeader(0, flags); @@ -107,6 +112,9 @@ private void SendControlPacket(SNISMUXFlags flags) _connection.Send(packet); ReturnPacket(packet); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendControlPacket | SNI | INFO | MARS Session Id {0}, Packet returned {1}, packet dataLeft {2}", ConnectionId, packet?._id, packet?.DataLeft); +#endif } finally { @@ -116,7 +124,7 @@ private void SendControlPacket(SNISMUXFlags flags) private void SetupSMUXHeader(int length, SNISMUXFlags flags) { - Debug.Assert(Monitor.IsEntered(this), "must take lock on self before updating mux header"); + Debug.Assert(Monitor.IsEntered(this), "must take lock on self before updating smux header"); _currentHeader.SMID = 83; _currentHeader.flags = (byte)flags; @@ -134,12 +142,14 @@ private void SetupSMUXHeader(int length, SNISMUXFlags flags) /// The packet with the SMUx header set. private SNIPacket SetPacketSMUXHeader(SNIPacket packet) { - Debug.Assert(packet.ReservedHeaderSize == SNISMUXHeader.HEADER_LENGTH, "mars handle attempting to mux packet without mux reservation"); + Debug.Assert(packet.ReservedHeaderSize == SNISMUXHeader.HEADER_LENGTH, "mars handle attempting to smux packet without smux reservation"); SetupSMUXHeader(packet.Length, SNISMUXFlags.SMUX_DATA); _currentHeader.Write(packet.GetHeaderBuffer(SNISMUXHeader.HEADER_LENGTH)); packet.SetHeaderActive(); - +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SetPacketSMUXHeader | SNI | INFO | MARS Session Id {0}, Setting SMUX_DATA header in current header for packet {1}", ConnectionId, packet?._id); +#endif return packet; } @@ -150,8 +160,8 @@ private SNIPacket SetPacketSMUXHeader(SNIPacket packet) /// SNI error code public override uint Send(SNIPacket packet) { - Debug.Assert(packet.ReservedHeaderSize == SNISMUXHeader.HEADER_LENGTH, "mars handle attempting to send muxed packet without mux reservation in Send"); - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + Debug.Assert(packet.ReservedHeaderSize == SNISMUXHeader.HEADER_LENGTH, "mars handle attempting to send muxed packet without smux reservation in Send"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.Send | SNI | INFO | SCOPE | Entering Scope {0}"); try { while (true) @@ -164,10 +174,12 @@ public override uint Send(SNIPacket packet) } } + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Send | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for Acknowledgment event.", ConnectionId, _sequenceNumber, _sendHighwater); _ackEvent.Wait(); lock (this) { + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | MARS Session Id {0}, _sendPacketQueue count found {1}, Acknowledgment event Reset", ConnectionId, _sendPacketQueue?.Count); _ackEvent.Reset(); } } @@ -177,6 +189,7 @@ public override uint Send(SNIPacket packet) { muxedPacket = SetPacketSMUXHeader(packet); } + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Send | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SMUX Packet is going to be sent. ", ConnectionId, _sequenceNumber, _sendHighwater); return _connection.Send(muxedPacket); } finally @@ -193,20 +206,21 @@ public override uint Send(SNIPacket packet) /// SNI error code private uint InternalSendAsync(SNIPacket packet, SNIAsyncCallback callback) { - Debug.Assert(packet.ReservedHeaderSize == SNISMUXHeader.HEADER_LENGTH, "mars handle attempting to send muxed packet without mux reservation in InternalSendAsync"); - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + Debug.Assert(packet.ReservedHeaderSize == SNISMUXHeader.HEADER_LENGTH, "mars handle attempting to send muxed packet without smux reservation in InternalSendAsync"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.InternalSendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (this) { if (_sequenceNumber >= _sendHighwater) { - SqlClientEventSource.Log.TrySNITraceEvent(" SNI Queue is full"); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.InternalSendAsync | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SNI Queue is full", ConnectionId, _sequenceNumber, _sendHighwater); return TdsEnums.SNI_QUEUE_FULL; } SNIPacket muxedPacket = SetPacketSMUXHeader(packet); muxedPacket.SetCompletionCallback(callback ?? HandleSendComplete); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.InternalSendAsync | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Sending packet", ConnectionId, _sequenceNumber, _sendHighwater); return _connection.SendAsync(muxedPacket, callback); } } @@ -222,7 +236,7 @@ private uint InternalSendAsync(SNIPacket packet, SNIAsyncCallback callback) /// SNI error code private uint SendPendingPackets() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | SCOPE | Entering Scope {0}"); SNIMarsQueuedPacket packet = null; try { @@ -239,16 +253,18 @@ private uint SendPendingPackets() if (result != TdsEnums.SNI_SUCCESS && result != TdsEnums.SNI_SUCCESS_IO_PENDING) { - SqlClientEventSource.Log.TrySNITraceEvent(" InternalSendAsync result is not SNI_SUCCESS and is not SNI_SUCCESS_IO_PENDING"); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | ERR | MARS Session Id {0}, InternalSendAsync result is not SNI_SUCCESS and is not SNI_SUCCESS_IO_PENDING", ConnectionId); return result; } _sendPacketQueue.Dequeue(); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | MARS Session Id {0}, _sendPacketQueue dequeued, count ", ConnectionId, _sendPacketQueue?.Count); continue; } else { _ackEvent.Set(); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | MARS Session Id {0}, _sendPacketQueue count found {1}, acknowledgment set", ConnectionId, _sendPacketQueue?.Count); } } @@ -272,7 +288,7 @@ private uint SendPendingPackets() /// SNI error code public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0} "); try { lock (this) @@ -281,6 +297,8 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul } SendPendingPackets(); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendAsync | SNI | INFO | MARS Session Id {0}, _sendPacketQueue enqueued, count {1}, ", ConnectionId, _sendPacketQueue?.Count); + return TdsEnums.SNI_SUCCESS_IO_PENDING; } finally @@ -296,7 +314,7 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul /// SNI error code public override uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0} "); try { lock (_receivedPacketQueue) @@ -305,12 +323,15 @@ public override uint ReceiveAsync(ref SNIPacket packet) if (_connectionError != null) { + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | ERR | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, _connectionError {5} ", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, _connectionError); return SNICommon.ReportSNIError(_connectionError); } if (queueCount == 0) { _asyncReceives++; + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | Info | MARS Session Id {0}, queueCount 0, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} ", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck); + return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -318,6 +339,9 @@ public override uint ReceiveAsync(ref SNIPacket packet) if (queueCount == 1) { +#if DEBUG + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | Info | MARS Session Id {0}, packet dequeued {1}, packet Owner {2}, packet refCount {3}, received Packet Queue count {4} ", ConnectionId, packet?._id, packet?._owner, packet?._refCount, _receivedPacketQueue?.Count); +#endif _packetEvent.Reset(); } } @@ -327,6 +351,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) _receiveHighwater++; } + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, queueCount {5} ", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, _receivedPacketQueue?.Count); SendAckIfNecessary(); return TdsEnums.SNI_SUCCESS; } @@ -341,16 +366,17 @@ public override uint ReceiveAsync(ref SNIPacket packet) /// public void HandleReceiveError(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleReceiveError | SNI | INFO | SCOPE | Entering Scope {0} "); try { // SNIMarsHandle should only receive calls to this function from the SNIMarsConnection aggregator class - // which should handle ownership of the packet because the individual mars handles are not aware of + // which should handle ownership of the packet because the individual mars handles are not aware of // each other and cannot know if they are the last one in the list and that it is safe to return the packet lock (_receivedPacketQueue) { _connectionError = SNILoadHandle.SingletonInstance.LastError; + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveError | SNI | ERR | MARS Session Id {0}, _connectionError to be handled: {1}", ConnectionId, _connectionError); _packetEvent.Set(); } @@ -369,7 +395,7 @@ public void HandleReceiveError(SNIPacket packet) /// SNI error code public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleSendComplete | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (this) @@ -379,6 +405,9 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) ((TdsParserStateObject)_callbackObject).WriteAsyncCallback(PacketHandle.FromManagedPacket(packet), sniErrorCode); } _connection.ReturnPacket(packet); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleSendComplete | SNI | INFO | MARS Session Id {0}, Returned Packet: {1}", ConnectionId, packet?._id); +#endif } finally { @@ -387,18 +416,19 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) } /// - /// Handle SMUX acknowledgement + /// Handle SMUX acknowledgment /// /// Send highwater mark public void HandleAck(uint highwater) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleAck | SNI | INFO | SCOPE | Entering Scope {0} "); try { lock (this) { if (_sendHighwater != highwater) { + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleAck | SNI | INFO | MARS Session Id {0}, Setting _sendHighwater {1} to highwater {2} and send pending packets.", ConnectionId, _sendHighwater, highwater); _sendHighwater = highwater; SendPendingPackets(); } @@ -417,13 +447,14 @@ public void HandleAck(uint highwater) /// SMUX header public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (this) { if (_sendHighwater != header.highwater) { + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, header.highwater {1}, _sendHighwater {2}, Handle Ack with header.highwater", ConnectionId, header.highwater, _sendHighwater); HandleAck(header.highwater); } @@ -433,11 +464,13 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { _receivedPacketQueue.Enqueue(packet); _packetEvent.Set(); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _receivedPacketQueue count {3}, packet event set", ConnectionId, _sequenceNumber, _sendHighwater, _receivedPacketQueue.Count); return; } _asyncReceives--; Debug.Assert(_callbackObject != null); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _asyncReceives {3}", ConnectionId, _sequenceNumber, _sendHighwater, _asyncReceives); ((TdsParserStateObject)_callbackObject).ReadAsyncCallback(PacketHandle.FromManagedPacket(packet), 0); } @@ -449,7 +482,7 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { _receiveHighwater++; } - + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.HandleReceiveComplete | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} ", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck); SendAckIfNecessary(); } finally @@ -475,6 +508,7 @@ private void SendAckIfNecessary() if (receiveHighwater - receiveHighwaterLastAck > ACK_THRESHOLD) { SendControlPacket(SNISMUXFlags.SMUX_ACK); + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.SendAckIfNecessary | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} Sending acknowledgment > ACK_THRESHOLD {5}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, ACK_THRESHOLD); } } @@ -486,7 +520,7 @@ private void SendAckIfNecessary() /// SNI error code public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0} "); try { packet = null; @@ -499,10 +533,12 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) { if (_connectionError != null) { + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | ERR | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _connectionError found: {3}.", ConnectionId, _sequenceNumber, _sendHighwater, _connectionError); return SNICommon.ReportSNIError(_connectionError); } queueCount = _receivedPacketQueue.Count; + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, W_receivedPacketQueue count {3}.", ConnectionId, _sequenceNumber, _sendHighwater, queueCount); if (queueCount > 0) { @@ -511,6 +547,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) if (queueCount == 1) { _packetEvent.Reset(); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, packet event reset, _receivedPacketQueue count 1.", ConnectionId, _sequenceNumber, _sendHighwater); } result = TdsEnums.SNI_SUCCESS; @@ -525,12 +562,15 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) } SendAckIfNecessary(); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, returning with result {3}.", ConnectionId, _sequenceNumber, _sendHighwater, result); return result; } + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for packet event.", ConnectionId, _sequenceNumber, _sendHighwater); if (!_packetEvent.Wait(timeoutInMilliseconds)) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _packetEvent wait timed out.", ConnectionId, _sequenceNumber, _sendHighwater); return TdsEnums.SNI_WAIT_TIMEOUT; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs index 2914ac3c1b..74c0bcb5c1 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs @@ -19,7 +19,6 @@ namespace Microsoft.Data.SqlClient.SNI internal sealed class SNINpHandle : SNIPhysicalHandle { internal const string DefaultPipePath = @"sql\query"; // e.g. \\HOSTNAME\pipe\sql\query - private const int MAX_PIPE_INSTANCES = 255; private readonly string _targetServer; private readonly object _sendSync; @@ -39,8 +38,8 @@ internal sealed class SNINpHandle : SNIPhysicalHandle public SNINpHandle(string serverName, string pipeName, long timerExpire) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(" Constructor"); - SqlClientEventSource.Log.TrySNITraceEvent(" Constructor. server name = {0}, pipe name = {1}", serverName, pipeName); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.ctor | SNI | INFO | SCOPE | Enter Scope"); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ctor | SNI | INFO | Connection Id {0}, Setting server name = {1}, pipe name = {2}", _connectionId, serverName, pipeName); try { _sendSync = new object(); @@ -57,7 +56,7 @@ public SNINpHandle(string serverName, string pipeName, long timerExpire) bool isInfiniteTimeOut = long.MaxValue == timerExpire; if (isInfiniteTimeOut) { - _pipeStream.Connect(System.Threading.Timeout.Infinite); + _pipeStream.Connect(Timeout.Infinite); } else { @@ -71,14 +70,14 @@ public SNINpHandle(string serverName, string pipeName, long timerExpire) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.ConnOpenFailedError, te); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent(" Timed out. Exception = {0}", te.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ctor | SNI | ERR | Connection Id {0}, Connection Timed out. Error Code 1 Exception = {1}", _connectionId, te?.Message); return; } catch (IOException ioe) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.ConnOpenFailedError, ioe); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent(" IOException = {0}", ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ctor | SNI | ERR | Connection Id {0}, IO Exception occurred. Error Code 1 Exception = {1}", _connectionId, ioe?.Message); return; } @@ -86,11 +85,11 @@ public SNINpHandle(string serverName, string pipeName, long timerExpire) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, 0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent(" Pipe stream is not connected or cannot write or read to/from it."); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ctor | SNI | ERR | Connection Id {0}, Pipe Stream not operational. Error Code 1 Exception = {1}", _connectionId, Strings.SNI_ERROR_1); return; } - _sslOverTdsStream = new SslOverTdsStream(_pipeStream); + _sslOverTdsStream = new SslOverTdsStream(_pipeStream, _connectionId); _sslStream = new SNISslStream(_sslOverTdsStream, true, new RemoteCertificateValidationCallback(ValidateServerCertificate)); _stream = _pipeStream; @@ -135,16 +134,17 @@ public override int ProtocolVersion public override uint CheckConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0} "); try { if (!_stream.CanWrite || !_stream.CanRead) { - SqlClientEventSource.Log.TrySNITraceEvent(" cannot write or read to/from the stream"); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.CheckConnection | SNI | ERR | Connection Id {0}, Cannot write or read to/from the stream", _connectionId); return TdsEnums.SNI_ERROR; } else { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.CheckConnection | SNI | INFO | Connection Id {0}, Can read and write to/from stream.", _connectionId); return TdsEnums.SNI_SUCCESS; } } @@ -178,12 +178,13 @@ public override void Dispose() //Release any references held by _stream. _stream = null; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Dispose | SNI | INFO | Connection Id {0}, All streams disposed and references cleared.", _connectionId); } } public override uint Receive(out SNIPacket packet, int timeout) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0} "); try { SNIPacket errorPacket; @@ -194,13 +195,14 @@ public override uint Receive(out SNIPacket packet, int timeout) { packet = RentPacket(headerSize: 0, dataSize: _bufferSize); packet.ReadFromStream(_stream); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Receive | SNI | INFO | Connection Id {0}, Rented and read packet, dataLeft {1}", _connectionId, packet?.DataLeft); if (packet.Length == 0) { errorPacket = packet; packet = null; var e = new Win32Exception(); - SqlClientEventSource.Log.TrySNITraceEvent(" packet length is 0."); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Receive | SNI | ERR | Connection Id {0}, Packet length found 0, Win32 exception raised: {1}", _connectionId, e?.Message); return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message); } } @@ -208,14 +210,14 @@ public override uint Receive(out SNIPacket packet, int timeout) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent(" ObjectDisposedException message = {0}.", ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (IOException ioe) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent(" IOException message = {0}.", ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Receive | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); return ReportErrorAndReleasePacket(errorPacket, ioe); } return TdsEnums.SNI_SUCCESS; @@ -229,7 +231,7 @@ public override uint Receive(out SNIPacket packet, int timeout) public override uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0} "); try { SNIPacket errorPacket; @@ -238,20 +240,21 @@ public override uint ReceiveAsync(ref SNIPacket packet) try { packet.ReadFromStreamAsync(_stream, _receiveCallback); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Rented and read packet asynchronously, dataLeft {1}", _connectionId, packet?.DataLeft); return TdsEnums.SNI_SUCCESS_IO_PENDING; } catch (ObjectDisposedException ode) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent(" ObjectDisposedException message = {0}.", ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReceiveAsync | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (IOException ioe) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent(" IOException message = {0}.", ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReceiveAsync | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); return ReportErrorAndReleasePacket(errorPacket, ioe); } } @@ -263,14 +266,14 @@ public override uint ReceiveAsync(ref SNIPacket packet) public override uint Send(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.Send | SNI | INFO | SCOPE | Entering Scope {0} "); try { bool releaseLock = false; try { // is the packet is marked out out-of-band (attention packets only) it must be - // sent immediately even if a send of recieve operation is already in progress + // sent immediately even if a send of receive operation is already in progress // because out of band packets are used to cancel ongoing operations // so try to take the lock if possible but continue even if it can't be taken if (packet.IsOutOfBand) @@ -290,18 +293,18 @@ public override uint Send(SNIPacket packet) { try { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Send | SNI | INFO | Connection Id {0}, Packet writing to stream, dataLeft {1}", _connectionId, packet?.DataLeft); packet.WriteToStream(_stream); return TdsEnums.SNI_SUCCESS; } catch (ObjectDisposedException ode) { - SqlClientEventSource.Log.TrySNITraceEvent(" ObjectDisposedException message = {0}.", ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Send | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); return ReportErrorAndReleasePacket(packet, ode); } catch (IOException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent(" IOException message = {0}.", ioe.Message); - + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Send | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); return ReportErrorAndReleasePacket(packet, ioe); } } @@ -322,10 +325,11 @@ public override uint Send(SNIPacket packet) public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { SNIAsyncCallback cb = callback ?? _sendCallback; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.SendAsync | SNI | INFO | Connection Id {0}, Packet writing to stream, dataLeft {1}", _connectionId, packet?.DataLeft); packet.WriteToStreamAsync(_stream, cb, SNIProviders.NP_PROV); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -343,23 +347,24 @@ public override void SetAsyncCallbacks(SNIAsyncCallback receiveCallback, SNIAsyn public override uint EnableSsl(uint options) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.EnableSsl | SNI | INFO | SCOPE | Entering Scope {0}"); try { _validateCert = (options & TdsEnums.SNI_SSL_VALIDATE_CERTIFICATE) != 0; try { + _sslStream.AuthenticateAsClient(_targetServer); _sslOverTdsStream.FinishHandshake(); } catch (AuthenticationException aue) { - SqlClientEventSource.Log.TrySNITraceEvent(" AuthenticationException message = {0}.", aue.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.EnableSsl | SNI | ERR | Connection Id {0}, AuthenticationException message = {1}.", ConnectionId, aue.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, aue); } catch (InvalidOperationException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("InvalidOperationException message = {0}.", ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.EnableSsl | SNI | ERR | Connection Id {0}, InvalidOperationException message = {1}.", ConnectionId, ioe.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, ioe); } _stream = _sslStream; @@ -391,15 +396,17 @@ public override void DisableSsl() /// true if valid private bool ValidateServerCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.ValidateServerCertificate | SNI | INFO | SCOPE | Entering Scope {0}"); try { if (!_validateCert) { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate validation not requested.", ConnectionId); return true; } - return SNICommon.ValidateSslServerCertificate(_targetServer, sender, cert, chain, policyErrors); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Proceeding to SSL certificate validation.", ConnectionId); + return SNICommon.ValidateSslServerCertificate(_targetServer, cert, policyErrors); } finally { @@ -422,6 +429,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, Exception sniExceptio { ReturnPacket(packet); } + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1} ", ConnectionId, sniException.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, sniException); } @@ -431,6 +439,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, uint nativeError, uin { ReturnPacket(packet); } + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1} ", ConnectionId, errorMessage); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, nativeError, sniError, errorMessage); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs index f25d916a65..8d1116ab38 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs @@ -36,6 +36,8 @@ internal sealed class SNIPacket internal readonly SNIHandle _owner; // used in debug builds to check that packets are being returned to the correct pool internal string _traceTag; // used in debug builds to assist tracing what steps the packet has been through + +#if TRACE_HISTORY [DebuggerDisplay("{Action.ToString(),nq}")] internal struct History { @@ -51,6 +53,7 @@ public enum Direction } internal List _history = null; +#endif /// /// uses the packet refcount in debug mode to identify if the packet is considered active @@ -58,14 +61,15 @@ public enum Direction /// public bool IsActive => _refCount == 1; - public SNIPacket(SNIHandle owner,int id) + public SNIPacket(SNIHandle owner, int id) : this() { #if TRACE_HISTORY - _history = new List(); + _history = new List(); #endif _id = id; _owner = owner; + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ctor | SNI | INFO | Connection Id {0}, Packet Id {1} instantiated,", _owner?.ConnectionId, _id); } // the finalizer is only included in debug builds and is used to ensure that all packets are correctly recycled @@ -75,7 +79,8 @@ public SNIPacket(SNIHandle owner,int id) { if (_data != null) { - Debug.Fail($@"finalizer called for unreleased SNIPacket, tag: {_traceTag}"); + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.dtor | SNI | ERR | Finalizer called for unreleased SNIPacket, Connection Id {0}, Packet Id {1}, _refCount {2}, DataLeft {3}, tag {4}", _owner?.ConnectionId, _id, _refCount, DataLeft, _traceTag); + //Debug.Fail($@"finalizer called for unreleased SNIPacket, tag: {_traceTag}"); } } @@ -119,7 +124,7 @@ public void SetCompletionCallback(SNIAsyncCallback completionCallback) } /// - /// Invoke the completion callback + /// Invoke the completion callback /// /// SNI error public void InvokeCompletionCallback(uint sniErrorCode) @@ -139,13 +144,16 @@ public void Allocate(int headerLength, int dataLength) _dataLength = 0; _dataOffset = 0; _headerLength = headerLength; +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.Allocate | SNI | INFO | Connection Id {0}, Packet Id {1} allocated with _headerLength {2}, _dataCapacity {3}", _owner?.ConnectionId, _id, _headerLength, _dataCapacity); +#endif } /// /// Read packet data into a buffer without removing it from the packet /// /// Buffer - /// Number of bytes read from the packet into the buffer + /// Number of bytes read from the packet into the buffer public void GetData(byte[] buffer, ref int dataSize) { Buffer.BlockCopy(_data, _headerLength, buffer, 0, _dataLength); @@ -162,6 +170,9 @@ public int TakeData(SNIPacket packet, int size) { int dataSize = TakeData(packet._data, packet._headerLength + packet._dataLength, size); packet._dataLength += dataSize; +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.TakeData | SNI | INFO | Connection Id {0}, Packet Id {1} took data from Packet Id {2} dataSize {3}, _dataLength {4}", _owner?.ConnectionId, _id, packet?._id, dataSize, packet._dataLength); +#endif return dataSize; } @@ -174,6 +185,9 @@ public void AppendData(byte[] data, int size) { Buffer.BlockCopy(data, 0, _data, _headerLength + _dataLength, size); _dataLength += size; +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.AppendData | SNI | INFO | Connection Id {0}, Packet Id {1} was appended with size {2}, _dataLength {3}", _owner?.ConnectionId, _id, size, _dataLength); +#endif } /// @@ -181,7 +195,7 @@ public void AppendData(byte[] data, int size) /// /// Buffer /// Data offset to write data at - /// Number of bytes to read from the packet into the buffer + /// Number of bytes to read from the packet into the buffer /// public int TakeData(byte[] buffer, int dataOffset, int size) { @@ -197,6 +211,9 @@ public int TakeData(byte[] buffer, int dataOffset, int size) Buffer.BlockCopy(_data, _headerLength + _dataOffset, buffer, dataOffset, size); _dataOffset += size; +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.TakeData | SNI | INFO | Connection Id {0}, Packet Id {1} took data size {2}, _dataLength {3}, _dataOffset {4}", _owner?.ConnectionId, _id, size, _dataLength, _dataOffset); +#endif return size; } @@ -214,6 +231,9 @@ public void SetHeaderActive() _dataCapacity += _headerLength; _dataLength += _headerLength; _headerLength = 0; +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.SetHeaderActive | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} header set to active.", _owner?.ConnectionId, _id, _dataLength); +#endif } /// @@ -229,6 +249,9 @@ public void Release() _data = null; _dataCapacity = 0; } +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.Release | SNI | INFO | Connection Id {0}, Packet Id {1} _headerLength {2} and _dataLength {3} released.", _owner?.ConnectionId, _id, _headerLength, _dataLength); +#endif _dataLength = 0; _dataOffset = 0; _headerLength = 0; @@ -243,6 +266,9 @@ public void Release() public void ReadFromStream(Stream stream) { _dataLength = stream.Read(_data, _headerLength, _dataCapacity); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ReadFromStream | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} read from stream.", _owner?.ConnectionId, _id, _dataLength); +#endif } /// @@ -270,15 +296,23 @@ private void ReadFromStreamAsyncContinuation(Task t, object state) if (e != null) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, e); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ReadFromStreamAsyncContinuation | SNI | ERR | Connection Id {0}, Internal Exception occurred while reading data: {1}", _owner?.ConnectionId, e.Message); +#endif error = true; } else { _dataLength = t.Result; - +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ReadFromStreamAsyncContinuation | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} read from stream.", _owner?.ConnectionId, _id, _dataLength); +#endif if (_dataLength == 0) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.ConnTerminatedError, Strings.SNI_ERROR_2); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ReadFromStreamAsyncContinuation | SNI | ERR | Connection Id {0}, No data read from stream, connection was terminated.", _owner?.ConnectionId); +#endif error = true; } } @@ -293,6 +327,9 @@ private void ReadFromStreamAsyncContinuation(Task t, object state) public void WriteToStream(Stream stream) { stream.Write(_data, _headerLength, _dataLength); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.WriteToStream | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} written to stream.", _owner?.ConnectionId, _id, _dataLength); +#endif } /// @@ -307,10 +344,16 @@ public async void WriteToStreamAsync(Stream stream, SNIAsyncCallback callback, S try { await stream.WriteAsync(_data, 0, _dataLength, CancellationToken.None).ConfigureAwait(false); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.WriteToStreamAsync | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} written to stream.", _owner?.ConnectionId, _id, _dataLength); +#endif } catch (Exception e) { SNILoadHandle.SingletonInstance.LastError = new SNIError(provider, SNICommon.InternalExceptionError, e); +#if DEBUG + SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.WriteToStreamAsync | SNI | ERR | Connection Id {0}, Internal Exception occurred while writing data: {1}", _owner?.ConnectionId, e.Message); +#endif status = TdsEnums.SNI_ERROR; } callback(this, status); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPhysicalHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPhysicalHandle.cs index 65a4432f80..ca11e8c4ac 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPhysicalHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPhysicalHandle.cs @@ -4,10 +4,8 @@ using System; using System.Diagnostics; -using System.Collections.Concurrent; using System.Threading; using System.Linq; -using System.Text; namespace Microsoft.Data.SqlClient.SNI { @@ -40,16 +38,18 @@ public override SNIPacket RentPacket(int headerSize, int dataSize) else { Debug.Assert(packet != null, "dequeue returned null SNIPacket"); - Debug.Assert(!packet.IsActive, "SNIPacket _refcount must be 1 or a lifetime issue has occured, trace with the #TRACE_HISTORY define"); + Debug.Assert(!packet.IsActive, "SNIPacket _refcount must be 1 or a lifetime issue has occurred, trace with the #TRACE_HISTORY define"); Debug.Assert(packet.IsInvalid, "dequeue returned valid packet"); GC.ReRegisterForFinalize(packet); } +#if TRACE_HISTORY if (packet._history != null) { packet._history.Add(new SNIPacket.History { Action = SNIPacket.History.Direction.Rent, Stack = GetStackParts(), RefCount = packet._refCount }); } +#endif Interlocked.Add(ref packet._refCount, 1); - Debug.Assert(packet.IsActive, "SNIPacket _refcount must be 1 or a lifetime issue has occured, trace with the #TRACE_HISTORY define"); + Debug.Assert(packet.IsActive, "SNIPacket _refcount must be 1 or a lifetime issue has occurred, trace with the #TRACE_HISTORY define"); #endif packet.Allocate(headerSize, dataSize); return packet; @@ -57,21 +57,23 @@ public override SNIPacket RentPacket(int headerSize, int dataSize) public override void ReturnPacket(SNIPacket packet) { - Debug.Assert(packet != null, "releasing null SNIPacket"); #if DEBUG - Debug.Assert(packet.IsActive, "SNIPacket _refcount must be 1 or a lifetime issue has occured, trace with the #TRACE_HISTORY define"); + Debug.Assert(packet != null, "releasing null SNIPacket"); + Debug.Assert(packet.IsActive, "SNIPacket _refcount must be 1 or a lifetime issue has occurred, trace with the #TRACE_HISTORY define"); Debug.Assert(ReferenceEquals(packet._owner, this), "releasing SNIPacket that belongs to another physical handle"); -#endif Debug.Assert(!packet.IsInvalid, "releasing already released SNIPacket"); +#endif packet.Release(); #if DEBUG Interlocked.Add(ref packet._refCount, -1); packet._traceTag = null; +#if TRACE_HISTORY if (packet._history != null) { packet._history.Add(new SNIPacket.History { Action = SNIPacket.History.Direction.Return, Stack = GetStackParts(), RefCount = packet._refCount }); } +#endif GC.SuppressFinalize(packet); #endif _pool.Return(packet); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs index 55ee594a0b..d12f94cfe0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs @@ -30,10 +30,7 @@ internal class SspiClientContextResult internal static readonly SNIProxy s_singleton = new SNIProxy(); - internal static SNIProxy GetInstance() - { - return s_singleton; - } + internal static SNIProxy GetInstance() => s_singleton; /// /// Enable SSL on a connection @@ -45,10 +42,12 @@ internal uint EnableSsl(SNIHandle handle, uint options) { try { + SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.EnableSsl | Info | Session Id {0}", handle?.ConnectionId); return handle.EnableSsl(options); } catch (Exception e) { + SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.EnableSsl | Err | Session Id {0}, SNI Handshake failed with exception: {1} ", handle?.ConnectionId, e?.Message); return SNICommon.ReportSNIError(SNIProviders.SSL_PROV, SNICommon.HandshakeFailureError, e); } } @@ -60,6 +59,7 @@ internal uint EnableSsl(SNIHandle handle, uint options) /// SNI error code internal uint DisableSsl(SNIHandle handle) { + SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.DisableSsl | Info | Session Id {0}", handle?.ConnectionId); handle.DisableSsl(); return TdsEnums.SNI_SUCCESS; } @@ -104,7 +104,7 @@ internal void GenSspiClientContext(SspiClientContextStatus sspiClientContextStat | ContextFlagsPal.Delegate | ContextFlagsPal.MutualAuth; - string serverSPN = System.Text.Encoding.UTF8.GetString(serverName); + string serverSPN = Encoding.UTF8.GetString(serverName); SecurityStatusPal statusCode = NegotiateStreamPal.InitializeSecurityContext( credentialsHandle, @@ -211,7 +211,7 @@ internal uint ReadSyncOverAsync(SNIHandle handle, out SNIPacket packet, int time internal uint GetConnectionId(SNIHandle handle, ref Guid clientConnectionId) { clientConnectionId = handle.ConnectionId; - + SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.GetConnectionId | Info | Session Id {0}", clientConnectionId); return TdsEnums.SNI_SUCCESS; } @@ -235,6 +235,7 @@ internal uint WritePacket(SNIHandle handle, SNIPacket packet, bool sync) result = handle.SendAsync(packet); } + SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.WritePacket | Info | Session Id {0}, SendAsync Result {1}", handle?.ConnectionId, result); return result; } @@ -302,6 +303,7 @@ internal SNIHandle CreateConnectionHandle(string fullServerName, bool ignoreSniO } } + SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.CreateConnectionHandle | Info | Session Id {0}, SNI Handle Type: {1}", sniHandle?.ConnectionId, sniHandle.GetType()); return sniHandle; } @@ -325,6 +327,7 @@ private static byte[] GetSqlServerSPN(DataSource dataSource) postfix = DefaultSqlServerPort.ToString(); } + SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.GetSqlServerSPN | Info | ServerName {0}, InstanceName {1}, Port {2}, postfix {3}", dataSource.ServerName, dataSource.InstanceName, dataSource.Port, postfix); return GetSqlServerSPN(hostName, postfix); } @@ -356,6 +359,9 @@ private static byte[] GetSqlServerSPN(string hostNameOrAddress, string portOrIns { serverSpn += $":{DefaultSqlServerPort}"; } + + SqlClientEventSource.Log.TryAdvancedTraceEvent("SNIProxy.GetSqlServerSPN | Info | ServerSPN {0}", serverSpn); + return Encoding.UTF8.GetBytes(serverSpn); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs index 1f900a43f0..912a40596b 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs @@ -67,6 +67,7 @@ public override void Dispose() //Release any references held by _stream. _stream = null; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Dispose | SNI | INFO | Connection Id {0}, All streams disposed.", _connectionId); } } @@ -118,126 +119,145 @@ public override int ProtocolVersion /// Used for DNS Cache public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo) { - _targetServer = serverName; - _sendSync = new object(); - - SQLDNSInfo cachedDNSInfo; - bool hasCachedDNSInfo = SQLFallbackDNSCache.Instance.GetDNSInfo(cachedFQDN, out cachedDNSInfo); - + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNITCPHandle.ctor | SNI | INFO | SCOPE | Enter Scope"); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Setting server name = {1}", _connectionId, serverName); try { - TimeSpan ts = default(TimeSpan); - - // In case the Timeout is Infinite, we will receive the max value of Int64 as the tick count - // The infinite Timeout is a function of ConnectionString Timeout=0 - bool isInfiniteTimeOut = long.MaxValue == timerExpire; - if (!isInfiniteTimeOut) - { - ts = DateTime.FromFileTime(timerExpire) - DateTime.Now; - ts = ts.Ticks < 0 ? TimeSpan.FromTicks(0) : ts; - } + _targetServer = serverName; + _sendSync = new object(); - bool reportError = true; + SQLDNSInfo cachedDNSInfo; + bool hasCachedDNSInfo = SQLFallbackDNSCache.Instance.GetDNSInfo(cachedFQDN, out cachedDNSInfo); - // We will always first try to connect with serverName as before and let the DNS server to resolve the serverName. - // If the DSN resolution fails, we will try with IPs in the DNS cache if existed. We try with IPv4 first and followed by IPv6 if - // IPv4 fails. The exceptions will be throw to upper level and be handled as before. try { - if (parallel) - { - _socket = TryConnectParallel(serverName, port, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo); - } - else + TimeSpan ts = default(TimeSpan); + + // In case the Timeout is Infinite, we will receive the max value of Int64 as the tick count + // The infinite Timeout is a function of ConnectionString Timeout=0 + bool isInfiniteTimeOut = long.MaxValue == timerExpire; + if (!isInfiniteTimeOut) { - _socket = Connect(serverName, port, ts, isInfiniteTimeOut, cachedFQDN, ref pendingDNSInfo); + ts = DateTime.FromFileTime(timerExpire) - DateTime.Now; + ts = ts.Ticks < 0 ? TimeSpan.FromTicks(0) : ts; } - } - catch (Exception ex) - { - // Retry with cached IP address - if (ex is SocketException || ex is ArgumentException || ex is AggregateException) + + bool reportError = true; + + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Connecting to serverName {1} and port {2}", _connectionId, serverName, port); + // We will always first try to connect with serverName as before and let the DNS server to resolve the serverName. + // If the DSN resolution fails, we will try with IPs in the DNS cache if existed. We try with IPv4 first and followed by IPv6 if + // IPv4 fails. The exceptions will be throw to upper level and be handled as before. + try { - if (hasCachedDNSInfo == false) + if (parallel) { - throw; + _socket = TryConnectParallel(serverName, port, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo); } else { - int portRetry = String.IsNullOrEmpty(cachedDNSInfo.Port) ? port : Int32.Parse(cachedDNSInfo.Port); - - try + _socket = Connect(serverName, port, ts, isInfiniteTimeOut, cachedFQDN, ref pendingDNSInfo); + } + } + catch (Exception ex) + { + // Retry with cached IP address + if (ex is SocketException || ex is ArgumentException || ex is AggregateException) + { + if (hasCachedDNSInfo == false) { - if (parallel) - { - _socket = TryConnectParallel(cachedDNSInfo.AddrIPv4, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo); - } - else - { - _socket = Connect(cachedDNSInfo.AddrIPv4, portRetry, ts, isInfiniteTimeOut, cachedFQDN, ref pendingDNSInfo); - } + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0}, Cached DNS Info not found, exception occurred thrown: {1}", _connectionId, ex?.Message); + throw; } - catch (Exception exRetry) + else { - if (exRetry is SocketException || exRetry is ArgumentNullException - || exRetry is ArgumentException || exRetry is ArgumentOutOfRangeException || exRetry is AggregateException) + int portRetry = string.IsNullOrEmpty(cachedDNSInfo.Port) ? port : int.Parse(cachedDNSInfo.Port); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Retrying with cached DNS IP Address {1} and port {2}", _connectionId, cachedDNSInfo.AddrIPv4, cachedDNSInfo.Port); + + try { if (parallel) { - _socket = TryConnectParallel(cachedDNSInfo.AddrIPv6, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo); + _socket = TryConnectParallel(cachedDNSInfo.AddrIPv4, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo); } else { - _socket = Connect(cachedDNSInfo.AddrIPv6, portRetry, ts, isInfiniteTimeOut, cachedFQDN, ref pendingDNSInfo); + _socket = Connect(cachedDNSInfo.AddrIPv4, portRetry, ts, isInfiniteTimeOut, cachedFQDN, ref pendingDNSInfo); } } - else + catch (Exception exRetry) { - throw; + if (exRetry is SocketException || exRetry is ArgumentNullException + || exRetry is ArgumentException || exRetry is ArgumentOutOfRangeException || exRetry is AggregateException) + { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Retrying exception {1}", _connectionId, exRetry?.Message); + if (parallel) + { + _socket = TryConnectParallel(cachedDNSInfo.AddrIPv6, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo); + } + else + { + _socket = Connect(cachedDNSInfo.AddrIPv6, portRetry, ts, isInfiniteTimeOut, cachedFQDN, ref pendingDNSInfo); + } + } + else + { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0}, Retry failed, exception occurred: {1}", _connectionId, exRetry?.Message); + throw; + } } } } + else + { + throw; + } } - else - { - throw; - } - } - if (_socket == null || !_socket.Connected) - { - if (_socket != null) + if (_socket == null || !_socket.Connected) { - _socket.Dispose(); - _socket = null; - } + if (_socket != null) + { + _socket.Dispose(); + _socket = null; + } - if (reportError) - { - ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); + if (reportError) + { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0} could not be opened, exception occurred: {1}", _connectionId, Strings.SNI_ERROR_40); + ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); + } + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket could not be opened.", _connectionId); + return; } + + _socket.NoDelay = true; + _tcpStream = new SNINetworkStream(_socket, true); + + _sslOverTdsStream = new SslOverTdsStream(_tcpStream, _connectionId); + _sslStream = new SNISslStream(_sslOverTdsStream, true, new RemoteCertificateValidationCallback(ValidateServerCertificate)); + } + catch (SocketException se) + { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket exception occurred: Error Code {1}, Message {2}", _connectionId, se.SocketErrorCode, se.Message); + ReportTcpSNIError(se); + return; + } + catch (Exception e) + { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Exception occurred: {1}", _connectionId, e.Message); + ReportTcpSNIError(e); return; } - _socket.NoDelay = true; - _tcpStream = new SNINetworkStream(_socket, true); - - _sslOverTdsStream = new SslOverTdsStream(_tcpStream); - _sslStream = new SNISslStream(_sslOverTdsStream, true, new RemoteCertificateValidationCallback(ValidateServerCertificate)); - } - catch (SocketException se) - { - ReportTcpSNIError(se); - return; + _stream = _tcpStream; + _status = TdsEnums.SNI_SUCCESS; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0} Socket opened successfully, TCP stream ready.", _connectionId); } - catch (Exception e) + finally { - ReportTcpSNIError(e); - return; + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); } - - _stream = _tcpStream; - _status = TdsEnums.SNI_SUCCESS; } // Connect to server with hostName and port in parellel mode. @@ -256,6 +276,7 @@ private Socket TryConnectParallel(string hostName, int port, TimeSpan ts, bool i { // Fail if above 64 to match legacy behavior callerReportError = false; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.TryConnectParallel | SNI | ERR | Connection Id {0} serverAddresses.Length {1} Exception: {2}", _connectionId, serverAddresses.Length, Strings.SNI_ERROR_47); ReportTcpSNIError(0, SNICommon.MultiSubnetFailoverWithMoreThan64IPs, Strings.SNI_ERROR_47); return availableSocket; } @@ -285,6 +306,7 @@ private Socket TryConnectParallel(string hostName, int port, TimeSpan ts, bool i if (!(isInfiniteTimeOut ? connectTask.Wait(-1) : connectTask.Wait(ts))) { callerReportError = false; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.TryConnectParallel | SNI | ERR | Connection Id {0} Connection timed out, Exception: {1}", _connectionId, Strings.SNI_ERROR_40); ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); return availableSocket; } @@ -341,7 +363,10 @@ void Cancel() sockets[i] = null; } } - catch { } + catch (Exception e) + { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Connect | SNI | ERR | THIS EXCEPTION IS BEING SWALLOWED: {0}", e?.Message); + } } } @@ -365,6 +390,7 @@ void Cancel() // enable keep-alive on socket SetKeepAliveValues(ref sockets[i]); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Connect | SNI | INFO | Connecting to IP address {0} and port {1}", ipAddresses[i], port); sockets[i].Connect(ipAddresses[i], port); if (sockets[i] != null) // sockets[i] can be null if cancel callback is executed during connect() { @@ -381,7 +407,10 @@ void Cancel() } } } - catch { } + catch (Exception e) + { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Connect | SNI | ERR | THIS EXCEPTION IS BEING SWALLOWED: {0}", e?.Message); + } } } finally @@ -508,14 +537,17 @@ public override uint EnableSsl(uint options) } catch (AuthenticationException aue) { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Authentication exception occurred: {1}", _connectionId, aue.Message); return ReportTcpSNIError(aue); } catch (InvalidOperationException ioe) { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Invalid Operation Exception occurred: {1}", _connectionId, ioe.Message); return ReportTcpSNIError(ioe); } _stream = _sslStream; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.EnableSsl | SNI | INFO | Connection Id {0}, SSL enabled successfully.", _connectionId); return TdsEnums.SNI_SUCCESS; } @@ -529,6 +561,7 @@ public override void DisableSsl() _sslOverTdsStream.Dispose(); _sslOverTdsStream = null; _stream = _tcpStream; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.DisableSsl | SNI | INFO | Connection Id {0}, SSL Disabled. Communication will continue on TCP Stream.", _connectionId); } /// @@ -543,10 +576,12 @@ private bool ValidateServerCertificate(object sender, X509Certificate cert, X509 { if (!_validateCert) { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate will not be validated.", _connectionId); return true; } - return SNICommon.ValidateSslServerCertificate(_targetServer, sender, cert, chain, policyErrors); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate will be validated for Target Server name", _connectionId); + return SNICommon.ValidateSslServerCertificate(_targetServer, cert, policyErrors); } /// @@ -567,45 +602,49 @@ public override uint Send(SNIPacket packet) { bool releaseLock = false; try + { + // is the packet is marked out out-of-band (attention packets only) it must be + // sent immediately even if a send of recieve operation is already in progress + // because out of band packets are used to cancel ongoing operations + // so try to take the lock if possible but continue even if it can't be taken + if (packet.IsOutOfBand) { - // is the packet is marked out out-of-band (attention packets only) it must be - // sent immediately even if a send of recieve operation is already in progress - // because out of band packets are used to cancel ongoing operations - // so try to take the lock if possible but continue even if it can't be taken - if (packet.IsOutOfBand) + Monitor.TryEnter(this, ref releaseLock); + } + else + { + Monitor.Enter(this); + releaseLock = true; + } + + // this lock ensures that two packets are not being written to the transport at the same time + // so that sending a standard and an out-of-band packet are both written atomically no data is + // interleaved + lock (_sendSync) + { + try { - Monitor.TryEnter(this, ref releaseLock); + packet.WriteToStream(_stream); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Send | SNI | INFO | Connection Id {0}, Data sent to stream synchronously", _connectionId); + return TdsEnums.SNI_SUCCESS; } - else + catch (ObjectDisposedException ode) { - Monitor.Enter(this); - releaseLock = true; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Send | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); + return ReportTcpSNIError(ode); } - - // this lock ensures that two packets are not being written to the transport at the same time - // so that sending a standard and an out-of-band packet are both written atomically no data is - // interleaved - lock (_sendSync) + catch (SocketException se) { - try - { - packet.WriteToStream(_stream); - return TdsEnums.SNI_SUCCESS; - } - catch (ObjectDisposedException ode) - { - return ReportTcpSNIError(ode); - } - catch (SocketException se) - { - return ReportTcpSNIError(se); - } - catch (IOException ioe) - { - return ReportTcpSNIError(ioe); - } + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Send | SNI | ERR | Connection Id {0}, SocketException occurred: {1}", _connectionId, se.Message); + return ReportTcpSNIError(se); + } + catch (IOException ioe) + { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Send | SNI | ERR | Connection Id {0}, IOException occurred: {1}", _connectionId, ioe.Message); + return ReportTcpSNIError(ioe); } } + } finally { if (releaseLock) @@ -641,6 +680,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) else { // otherwise it is timeout for 0 or less than -1 + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Error 258, Timeout error occurred.", _connectionId); ReportTcpSNIError(0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11); return TdsEnums.SNI_WAIT_TIMEOUT; } @@ -653,21 +693,25 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) errorPacket = packet; packet = null; var e = new Win32Exception(); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Win32 exception occurred: {1}", _connectionId, e.Message); return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message); } + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | INFO | Connection Id {0}, Data read from stream synchronously", _connectionId); return TdsEnums.SNI_SUCCESS; } catch (ObjectDisposedException ode) { errorPacket = packet; packet = null; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (SocketException se) { errorPacket = packet; packet = null; + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Socket exception occurred: {1}", _connectionId, se.Message); return ReportErrorAndReleasePacket(errorPacket, se); } catch (IOException ioe) @@ -677,9 +721,11 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) uint errorCode = ReportErrorAndReleasePacket(errorPacket, ioe); if (ioe.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut) { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred with Wait Timeout (error 258): {1}", _connectionId, ioe.Message); errorCode = TdsEnums.SNI_WAIT_TIMEOUT; } + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred: {1}", _connectionId, ioe.Message); return errorCode; } finally @@ -708,11 +754,12 @@ public override void SetAsyncCallbacks(SNIAsyncCallback receiveCallback, SNIAsyn /// SNI error code public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNITcpHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { SNIAsyncCallback cb = callback ?? _sendCallback; packet.WriteToStreamAsync(_stream, cb, SNIProviders.TCP_PROV); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.SendAsync | SNI | INFO | Connection Id {0}, Data sent to stream asynchronously", _connectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } finally @@ -734,6 +781,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) try { packet.ReadFromStreamAsync(_stream, _receiveCallback); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Data received from stream asynchronously", _connectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } catch (Exception e) when (e is ObjectDisposedException || e is SocketException || e is IOException) @@ -764,15 +812,18 @@ public override uint CheckConnection() // return true we can safely determine that the connection is no longer active. if (!_socket.Connected || (_socket.Poll(100, SelectMode.SelectRead) && _socket.Available == 0)) { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket not usable.", _connectionId); return TdsEnums.SNI_ERROR; } } catch (SocketException se) { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket Exception occurred: {1}", _connectionId, se.Message); return ReportTcpSNIError(se); } catch (ObjectDisposedException ode) { + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); return ReportTcpSNIError(ode); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs index e4359e2c42..781d914f63 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs @@ -37,7 +37,7 @@ internal static int GetPortByInstanceName(string browserHostName, string instanc } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent(" SocketException Message = {0}", se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SSRP.GetPortByInstanceName |SNI|ERR> SocketException Message = {0}", se.Message); throw new Exception(SQLMessage.SqlServerBrowserNotAccessible(), se); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs index 58384dfd58..552a17fcf2 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -16,6 +16,7 @@ namespace Microsoft.Data.SqlClient.SNI internal sealed partial class SslOverTdsStream : Stream { private readonly Stream _stream; + private Guid _connectionId; private int _packetBytes = 0; private bool _encapsulate; @@ -27,9 +28,11 @@ internal sealed partial class SslOverTdsStream : Stream /// Constructor /// /// Underlying stream - public SslOverTdsStream(Stream stream) + /// Connection Id of parent stream handle + public SslOverTdsStream(Stream stream, Guid connectionId) { _stream = stream; + _connectionId = connectionId; _encapsulate = true; } @@ -39,7 +42,7 @@ public SslOverTdsStream(Stream stream) public void FinishHandshake() { _encapsulate = false; - SqlClientEventSource.Log.TrySNITraceEvent(" switched from encapsulation to passthrough mode"); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SslOverTdsStream.FinishHandshake | SNI | INFO | Connection Id {0}, Switched from encapsulation to passthrough mode", _connectionId); } /// @@ -58,6 +61,7 @@ public override void Flush() if (!(_stream is PipeStream)) { _stream.Flush(); + SqlClientEventSource.Log.TrySNITraceEvent("SNI.SslOverTdsStream.Flush | SNI | INFO | Connection Id {0}, Flushed stream", _connectionId); } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 9ce5c7a8d2..a3fa23baca 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -340,6 +340,7 @@ internal void ProcessPendingAck(TdsParserStateObject stateObj) { if (stateObj._attentionSent) { + SqlClientEventSource.Log.TryTraceEvent("TdsParser.ProcessPendingAck | INFO | Connection Object Id {0}, State Obj Id {1}, Processing Attention.", _connHandler._objectID, stateObj.ObjectID); ProcessAttention(stateObj); } } @@ -379,36 +380,8 @@ internal void Connect( else { _sniSpnBuffer = null; - switch (authType) - { - case SqlAuthenticationMethod.ActiveDirectoryPassword: - SqlClientEventSource.Log.TryTraceEvent(" Active Directory Password authentication"); - break; - case SqlAuthenticationMethod.ActiveDirectoryIntegrated: - SqlClientEventSource.Log.TryTraceEvent(" Active Directory Integrated authentication"); - break; - case SqlAuthenticationMethod.ActiveDirectoryInteractive: - SqlClientEventSource.Log.TryTraceEvent(" Active Directory Interactive authentication"); - break; - case SqlAuthenticationMethod.ActiveDirectoryServicePrincipal: - SqlClientEventSource.Log.TryTraceEvent(" Active Directory Service Principal authentication"); - break; - case SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow: - SqlClientEventSource.Log.TryTraceEvent(" Active Directory Device Code Flow authentication"); - break; - case SqlAuthenticationMethod.ActiveDirectoryManagedIdentity: - SqlClientEventSource.Log.TryTraceEvent(" Active Directory Managed Identity authentication"); - break; - case SqlAuthenticationMethod.ActiveDirectoryMSI: - SqlClientEventSource.Log.TryTraceEvent(" Active Directory MSI authentication"); - break; - case SqlAuthenticationMethod.SqlPassword: - SqlClientEventSource.Log.TryTraceEvent(" SQL Password authentication"); - break; - default: - SqlClientEventSource.Log.TryTraceEvent(" SQL authentication"); - break; - } + SqlClientEventSource.Log.TryTraceEvent("TdsParser.Connect | SEC | Connection Object Id {0}, Authentication Mode: {1}", _connHandler._objectID, + authType == SqlAuthenticationMethod.NotSpecified ? SqlAuthenticationMethod.SqlPassword.ToString() : authType.ToString()); } _sniSpnBuffer = null; @@ -417,7 +390,7 @@ internal void Connect( if (integratedSecurity || authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated) { LoadSSPILibrary(); - SqlClientEventSource.Log.TryTraceEvent(" SSPI or Active Directory Authentication Library for SQL Server based integrated authentication"); + SqlClientEventSource.Log.TryTraceEvent("TdsParser.Connect | SEC | SSPI or Active Directory Authentication Library loaded for SQL Server based integrated authentication"); } byte[] instanceName = null; diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs index 5d36213755..a7b963ab2e 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs @@ -48,7 +48,7 @@ internal enum SnapshottedStateFlags : byte private const long CheckConnectionWindow = 50000; - protected readonly TdsParser _parser; // TdsParser pointer + protected readonly TdsParser _parser; // TdsParser pointer private readonly WeakReference _owner = new WeakReference(null); // the owner of this session, used to track when it's been orphaned internal SqlDataReader.SharedState _readerState; // susbset of SqlDataReader state (if it is the owner) necessary for parsing abandoned results in TDS private int _activateCount; // 0 when we're in the pool, 1 when we're not, all others are an error @@ -238,8 +238,8 @@ internal enum SnapshottedStateFlags : byte // instead of blocking it will fail. internal static bool _failAsyncPends = false; - // If this is set and an async read is made, then - // we will switch to syncOverAsync mode for the + // If this is set and an async read is made, then + // we will switch to syncOverAsync mode for the // remainder of the async operation. internal static bool _forceSyncOverAsyncAfterFirstPend = false; @@ -542,8 +542,8 @@ internal bool TryInitialize(TdsParserStateObject stateObj, int columnsCount) return false; } - SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, NBCROW bitmap received, column count = {1}", stateObj.ObjectID, columnsCount); - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" NBCROW bitmap data: ", _nullBitmap, (ushort)_nullBitmap.Length); + SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap received, column count = {1}", stateObj._objectID, columnsCount); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap data: ", stateObj._objectID, _nullBitmap, (ushort)_nullBitmap.Length); return true; } @@ -567,7 +567,7 @@ internal void Clean() } /// - /// If this method returns true, the value is guaranteed to be null. This is not true vice versa: + /// If this method returns true, the value is guaranteed to be null. This is not true vice versa: /// if the bitmap value is false (if this method returns false), the value can be either null or non-null - no guarantee in this case. /// To determine whether it is null or not, read it from the TDS (per NBCROW design spec, for IMAGE/TEXT/NTEXT columns server might send /// bitmap = 0, when the actual value is null). @@ -677,12 +677,12 @@ internal void Cancel(object caller) internal void CancelRequest() { ResetBuffer(); // clear out unsent buffer - // If the first sqlbulkcopy timeout, _outputPacketNumber may not be 1, - // the next sqlbulkcopy (same connection string) requires this to be 1, hence reset + // If the first sqlbulkcopy timeout, _outputPacketNumber may not be 1, + // the next sqlbulkcopy (same connection string) requires this to be 1, hence reset // it here when exception happens in the first sqlbulkcopy ResetPacketCounters(); - // VSDD#907507, if bulkcopy write timeout happens, it already sent the attention, + // VSDD#907507, if bulkcopy write timeout happens, it already sent the attention, // so no need to send it again if (!_bulkCopyWriteTimeout) { @@ -859,6 +859,7 @@ internal void DecrementOpenResultCount() { // If we were not executed under a transaction - decrement the global count // on the parser. + SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.DecrementOpenResultCount | INFO | State Object Id {0}, Processing Attention.", _objectID); _parser.DecrementNonTransactedOpenResultCount(); } else @@ -873,7 +874,7 @@ internal void DecrementOpenResultCount() internal int DecrementPendingCallbacks(bool release) { int remaining = Interlocked.Decrement(ref _pendingCallbacks); - SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, after decrementing _pendingCallbacks: {1}", ObjectID, _pendingCallbacks); + SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.DecrementPendingCallbacks | ADV | State Object Id {0}, after decrementing _pendingCallbacks: {1}", _objectID, _pendingCallbacks); FreeGcHandle(remaining, release); // NOTE: TdsParserSessionPool may call DecrementPendingCallbacks on a TdsParserStateObject which is already disposed // This is not dangerous (since the stateObj is no longer in use), but we need to add a workaround in the assert for it @@ -926,7 +927,7 @@ internal int IncrementAndObtainOpenResultCount(SqlInternalTransaction transactio internal int IncrementPendingCallbacks() { int remaining = Interlocked.Increment(ref _pendingCallbacks); - SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, after incrementing _pendingCallbacks: {1}", ObjectID, _pendingCallbacks); + SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.IncrementPendingCallbacks | ADV | State Object Id {0}, after incrementing _pendingCallbacks: {1}", _objectID, _pendingCallbacks); Debug.Assert(0 < remaining && remaining <= 3, $"_pendingCallbacks values is invalid after incrementing: {remaining}"); return remaining; } @@ -1000,7 +1001,7 @@ internal bool TryProcessHeader() // if the header splits buffer reads - special case! if ((_partialHeaderBytesRead > 0) || (_inBytesUsed + _inputHeaderLen > _inBytesRead)) { - // VSTS 219884: when some kind of MITM (man-in-the-middle) tool splits the network packets, the message header can be split over + // VSTS 219884: when some kind of MITM (man-in-the-middle) tool splits the network packets, the message header can be split over // several network packets. // Note: cannot use ReadByteArray here since it uses _inBytesPacket which is not set yet. do @@ -2216,7 +2217,7 @@ internal void ReadSniSyncOverAsync() internal void OnConnectionClosed() { // the stateObj is not null, so the async invocation that registered this callback - // via the SqlReferenceCollection has not yet completed. We will look for a + // via the SqlReferenceCollection has not yet completed. We will look for a // _networkPacketTaskSource and mark it faulted. If we don't find it, then // TdsParserStateObject.ReadSni will abort when it does look to see if the parser // is open. If we do, then when the call that created it completes and a continuation @@ -2230,7 +2231,7 @@ internal void OnConnectionClosed() Parser.State = TdsParserState.Broken; Parser.Connection.BreakConnection(); - // Ensure that changing state occurs before checking _networkPacketTaskSource + // Ensure that changing state occurs before checking _networkPacketTaskSource Interlocked.MemoryBarrier(); // then check for networkPacketTaskSource @@ -2327,7 +2328,7 @@ private void OnTimeout(object state) } } - // Ensure that the connection is no longer usable + // Ensure that the connection is no longer usable // This is needed since the timeout error added above is non-fatal (and so throwing it won't break the connection) _parser.State = TdsParserState.Broken; _parser.Connection.BreakConnection(); @@ -2506,7 +2507,7 @@ internal bool IsConnectionAlive(bool throwOnException) if ((error != TdsEnums.SNI_SUCCESS) && (error != TdsEnums.SNI_WAIT_TIMEOUT)) { // Connection is dead - SqlClientEventSource.Log.TryTraceEvent(" received error {0} on idle connection", (int)error); + SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.IsConnectionAlive | Info | State Object Id {0}, received error {1} on idle connection", _objectID, (int)error); isAlive = false; if (throwOnException) { @@ -2526,7 +2527,7 @@ internal bool IsConnectionAlive(bool throwOnException) } /// - /// Checks to see if the underlying connection is still valid (used by idle connection resiliency - for active connections) + /// Checks to see if the underlying connection is still valid (used by idle connection resiliency - for active connections) /// NOTE: This is not safe to do on a connection that is currently in use /// NOTE: This will mark the connection as broken if it is found to be dead /// @@ -2788,7 +2789,8 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error) Debug.Assert(CheckPacket(packet, source) && source != null, "AsyncResult null on callback"); if (_parser.MARSOn) - { // Only take reset lock on MARS and Async. + { + // Only take reset lock on MARS and Async. CheckSetResetConnectionState(error, CallbackType.Read); } @@ -2875,7 +2877,7 @@ private void ReadAsyncCallbackCaptureException(TaskCompletionSource sour Debug.Assert(_parser.State == TdsParserState.Broken || _parser.State == TdsParserState.Closed || _parser.Connection.IsConnectionDoomed, "Failed to capture exception while the connection was still healthy"); // The safest thing to do is to ensure that the connection is broken and attempt to cancel the task - // This must be done from another thread to not block the callback thread + // This must be done from another thread to not block the callback thread Task.Factory.StartNew(() => { _parser.State = TdsParserState.Broken; @@ -2895,7 +2897,7 @@ public void WriteAsyncCallback(IntPtr key, PacketHandle packet, uint sniError) { if (sniError != TdsEnums.SNI_SUCCESS) { - SqlClientEventSource.Log.TryTraceEvent(" write async returned error code {0}", (int)sniError); + SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.WriteAsyncCallback | Info | State Object Id {0}, Write async returned error code {1}", _objectID, (int)sniError); try { AddError(_parser.ProcessSNIError(this)); @@ -3075,7 +3077,7 @@ internal Task WriteByteArray(byte[] b, int len, int offsetBuffer, bool canAccumu // Takes a span or a byte array and writes it to the buffer // If you pass in a span and a null array then the span wil be used. // If you pass in a non-null array then the array will be used and the span is ignored. - // if the span cannot be written into the current packet then the remaining contents of the span are copied to a + // if the span cannot be written into the current packet then the remaining contents of the span are copied to a // new heap allocated array that will used to callback into the method to continue the write operation. private Task WriteBytes(ReadOnlySpan b, int len, int offsetBuffer, bool canAccumulate = true, TaskCompletionSource completion = null, byte[] array = null) { @@ -3371,7 +3373,7 @@ private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccu #if DEBUG else if (!sync && !canAccumulate && SqlCommand.DebugForceAsyncWriteDelay > 0) { - // Executed synchronously - callback will not be called + // Executed synchronously - callback will not be called TaskCompletionSource completion = new TaskCompletionSource(); uint error = sniError; new Timer(obj => @@ -3385,7 +3387,7 @@ private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccu if (error != TdsEnums.SNI_SUCCESS) { - SqlClientEventSource.Log.TryTraceEvent(" write async returned error code {0}", (int)error); + SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.SNIWritePacket | Info | State Object Id {0}, Write async returned error code {1}", _objectID, (int)error); AddError(_parser.ProcessSNIError(this)); ThrowExceptionAndWarning(); } @@ -3420,7 +3422,7 @@ private Task SNIWritePacket(PacketHandle packet, out uint sniError, bool canAccu } else { - SqlClientEventSource.Log.TryTraceEvent(" write async returned error code {0}", (int)sniError); + SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.SNIWritePacket | Info | State Object Id {0}, Write async returned error code {1}", _objectID, (int)sniError); AddError(_parser.ProcessSNIError(this)); ThrowExceptionAndWarning(callerHasConnectionLock); } @@ -3476,9 +3478,9 @@ internal void SendAttention(bool mustTakeWriteLock = false) } uint sniError; - _parser._asyncWrite = false; // stop async write + _parser._asyncWrite = false; // stop async write SNIWritePacket(attnPacket, out sniError, canAccumulate: false, callerHasConnectionLock: false); - SqlClientEventSource.Log.TryTraceEvent(" Send Attention ASync."); + SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.SendAttention | Info | State Object Id {0}, Sent Attention.", _objectID); } finally { @@ -3500,8 +3502,8 @@ internal void SendAttention(bool mustTakeWriteLock = false) _attentionSending = false; } - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" Packet sent", _outBuff, (ushort)_outBytesUsed); - SqlClientEventSource.Log.TryTraceEvent(" Attention sent to the server."); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.SendAttention | INFO | ADV | State Object Id {0}, Out Buffer {1}, Packet sent: {2}", _objectID, _outBuff, (ushort)_outBytesUsed); + SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.SendAttention | Info | State Object Id {0}, Attention sent to the server.", _objectID); AssertValidState(); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs index 48a6196f28..5ab255f77b 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObjectManaged.cs @@ -35,14 +35,15 @@ protected override bool CheckPacket(PacketHandle packet, TaskCompletionSource {0}, NBCROW bitmap received, column count = {1}", stateObj.ObjectID, columnsCount); - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" NBCROW bitmap data: ", _nullBitmap, (ushort)_nullBitmap.Length); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" State Object Id {0}, Null Bitmap {1}, NBCROW bitmap data: {2}", stateObj.ObjectID, _nullBitmap, (ushort)_nullBitmap.Length); return true; } @@ -2811,7 +2811,7 @@ public void ProcessSniPacket(IntPtr packet, UInt32 error) } } SniReadStatisticsAndTracing(); - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" Packet read", _inBuff, (ushort)_inBytesRead); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" State Object Id {0}, In Buffer {1}, Packet read: {2}", ObjectID, _inBuff, (ushort)_inBytesRead); AssertValidState(); } else @@ -3557,8 +3557,8 @@ internal void SendAttention(bool mustTakeWriteLock = false) _attentionSending = false; } - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" Packet sent", _outBuff, (ushort)_outBytesUsed); - SqlClientEventSource.Log.TryTraceEvent(" Attention sent to the server.", "Info"); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" State Object Id {0}, Out Buffer {1}, Packet sent {2}", ObjectID, _outBuff, (ushort)_outBytesUsed); + SqlClientEventSource.Log.TryTraceEvent(" Attention sent to the server."); AssertValidState(); } @@ -3744,7 +3744,7 @@ private void SniWriteStatisticsAndTracing() _traceChangePasswordLength = 0; } } - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" Packet sent", _outBuff, (ushort)_outBytesUsed); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" State Object Id {0}, Out buffer: {1}, Packet sent: {2}", ObjectID, _outBuff, (ushort)_outBytesUsed); } [Conditional("DEBUG")] diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs index 971f719ec9..69bcc553e9 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs @@ -415,7 +415,7 @@ internal void TryScopeLeaveEvent(long scopeId) { if (Log.IsScopeEnabled()) { - ScopeLeave(scopeId); + ScopeLeave(string.Format("Exiting Scope {0}", scopeId)); } } #endregion @@ -525,7 +525,7 @@ internal void TryNotificationScopeLeaveEvent(long scopeId) { if (Log.IsNotificationScopeEnabled()) { - NotificationScopeLeave(scopeId); + NotificationScopeLeave(string.Format("Exiting Notification Scope {0}", scopeId)); } } #endregion @@ -584,7 +584,7 @@ internal void TryPoolerScopeLeaveEvent(long scopeId) { if (Log.IsPoolerScopeEnabled()) { - PoolerScopeLeave(scopeId); + PoolerScopeLeave(string.Format("Exiting Pooler Scope {0}", scopeId)); } } #endregion @@ -663,6 +663,15 @@ internal void TryAdvancedTraceEvent(string message, T0 args0, T1 } } + [NonEvent] + internal void TryAdvancedTraceEvent(string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4) + { + if (Log.IsAdvancedTraceOn()) + { + AdvancedTrace(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr, args4?.ToString() ?? NullStr)); + } + } + [NonEvent] internal void TryAdvancedTraceEvent(string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4, T5 args5) { @@ -696,16 +705,16 @@ internal void TryAdvanceScopeLeave(long scopeId) { if (Log.IsAdvancedTraceOn()) { - AdvancedScopeLeave(scopeId); + AdvancedScopeLeave(string.Format("Exiting Advanced Scope {0}", scopeId)); } } [NonEvent] - internal void TryAdvancedTraceBinEvent(string message, T0 args0, T1 args1) + internal void TryAdvancedTraceBinEvent(string message, T0 args0, T1 args1, T2 args) { if (Log.IsAdvancedTraceOn()) { - AdvancedTraceBin(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr)); + AdvancedTraceBin(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args1?.ToString() ?? NullStr)); } } @@ -852,52 +861,12 @@ internal long TrySNIScopeEnterEvent(string message) return 0; } - [NonEvent] - internal long TrySNIScopeEnterEvent(string message, T0 args0) - { - if (Log.IsSNIScopeEnabled()) - { - return SNIScopeEnter(string.Format(message, args0?.ToString() ?? NullStr)); - } - return 0; - } - - [NonEvent] - internal long TrySNIScopeEnterEvent(string message, T0 args0, T1 args1) - { - if (Log.IsSNIScopeEnabled()) - { - return SNIScopeEnter(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr)); - } - return 0; - } - - [NonEvent] - internal long TrySNIScopeEnterEvent(string message, T0 args0, T1 args1, T2 args2) - { - if (Log.IsSNIScopeEnabled()) - { - return SNIScopeEnter(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr)); - } - return 0; - } - - [NonEvent] - internal long TrySNIScopeEnterEvent(string message, T0 args0, T1 args1, T2 args2, T3 args3) - { - if (Log.IsSNIScopeEnabled()) - { - return SNIScopeEnter(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr)); - } - return 0; - } - [NonEvent] internal void TrySNIScopeLeaveEvent(long scopeId) { if (Log.IsSNIScopeEnabled()) { - SNIScopeLeave(scopeId); + SNIScopeLeave(string.Format("Exiting SNI Scope {0}", scopeId)); } } #endregion @@ -924,148 +893,111 @@ internal void EndExecute(int objectId, int compositeState, int sqlExceptionNumbe } [Event(TraceEventId, Level = EventLevel.Informational, Keywords = Keywords.Trace)] - internal void Trace(string message) - { + internal void Trace(string message) => WriteEvent(TraceEventId, message); - } [Event(ScopeEnterId, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Keywords = Keywords.Scope)] internal long ScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextScopeId); - WriteEvent(ScopeEnterId, message); + WriteEvent(ScopeEnterId, string.Format(message, scopeId)); return scopeId; } [Event(ScopeExitId, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Keywords = Keywords.Scope)] - internal void ScopeLeave(long scopeId) - { - WriteEvent(ScopeExitId, scopeId); - } + internal void ScopeLeave(string message) => + WriteEvent(ScopeExitId, message); [Event(NotificationTraceId, Level = EventLevel.Informational, Keywords = Keywords.NotificationTrace)] - internal void NotificationTrace(string message) - { + internal void NotificationTrace(string message) => WriteEvent(NotificationTraceId, message); - } [Event(NotificationScopeEnterId, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Keywords = Keywords.NotificationScope)] internal long NotificationScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextNotificationScopeId); - WriteEvent(NotificationScopeEnterId, message); + WriteEvent(NotificationScopeEnterId, string.Format(message, scopeId)); return scopeId; } [Event(NotificationScopeExitId, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Keywords = Keywords.NotificationScope)] - internal void NotificationScopeLeave(long scopeId) - { - WriteEvent(NotificationScopeExitId, scopeId); - } + internal void NotificationScopeLeave(string message) => + WriteEvent(NotificationScopeExitId, message); [Event(PoolerTraceId, Level = EventLevel.Informational, Keywords = Keywords.PoolerTrace)] - internal void PoolerTrace(string message) - { + internal void PoolerTrace(string message) => WriteEvent(PoolerTraceId, message); - } [Event(PoolerScopeEnterId, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Keywords = Keywords.PoolerScope)] internal long PoolerScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextPoolerScopeId); - WriteEvent(PoolerScopeEnterId, message); + WriteEvent(PoolerScopeEnterId, string.Format(message, scopeId)); return scopeId; } [Event(PoolerScopeExitId, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Keywords = Keywords.PoolerScope)] - internal void PoolerScopeLeave(long scopeId) - { - WriteEvent(PoolerScopeExitId, scopeId); - } + internal void PoolerScopeLeave(string message) => + WriteEvent(PoolerScopeExitId, message); [Event(AdvancedTraceId, Level = EventLevel.Verbose, Keywords = Keywords.AdvancedTrace)] - internal void AdvancedTrace(string message) - { + internal void AdvancedTrace(string message) => WriteEvent(AdvancedTraceId, message); - } [Event(AdvancedScopeEnterId, Level = EventLevel.Verbose, Opcode = EventOpcode.Start, Keywords = Keywords.AdvancedTrace)] internal long AdvancedScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextScopeId); - WriteEvent(AdvancedScopeEnterId, message); + WriteEvent(AdvancedScopeEnterId, string.Format(message, scopeId)); return scopeId; } [Event(AdvancedScopeExitId, Level = EventLevel.Verbose, Opcode = EventOpcode.Stop, Keywords = Keywords.AdvancedTrace)] - internal void AdvancedScopeLeave(long scopeId) - { - WriteEvent(AdvancedScopeExitId, scopeId); - } + internal void AdvancedScopeLeave(string message) => + WriteEvent(AdvancedScopeExitId, message); [Event(AdvancedTraceBinId, Level = EventLevel.Verbose, Keywords = Keywords.AdvancedTraceBin)] - internal void AdvancedTraceBin(string message) - { + internal void AdvancedTraceBin(string message) => WriteEvent(AdvancedTraceBinId, message); - } [Event(AdvancedTraceErrorId, Level = EventLevel.Error, Keywords = Keywords.AdvancedTrace)] - internal void AdvancedTraceError(string message) - { + internal void AdvancedTraceError(string message) => WriteEvent(AdvancedTraceErrorId, message); - } [Event(CorrelationTraceId, Level = EventLevel.Informational, Keywords = Keywords.CorrelationTrace, Opcode = EventOpcode.Start)] - internal void CorrelationTrace(string message) - { + internal void CorrelationTrace(string message) => WriteEvent(CorrelationTraceId, message); - } [Event(StateDumpEventId, Level = EventLevel.Verbose, Keywords = Keywords.StateDump)] - internal void StateDump(string message) - { + internal void StateDump(string message) => WriteEvent(StateDumpEventId, message); - } [Event(SNITraceEventId, Level = EventLevel.Informational, Keywords = Keywords.SNITrace)] - internal void SNITrace(string message) - { + internal void SNITrace(string message) => WriteEvent(SNITraceEventId, message); - } [Event(SNIScopeEnterId, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Keywords = Keywords.SNIScope)] internal long SNIScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextSNIScopeId); - WriteEvent(SNIScopeEnterId, message); + WriteEvent(SNIScopeEnterId, string.Format(message, scopeId)); return scopeId; } [Event(SNIScopeExitId, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Keywords = Keywords.SNIScope)] - internal void SNIScopeLeave(long scopeId) - { - WriteEvent(SNIScopeExitId, scopeId); - } + internal void SNIScopeLeave(string message) => + WriteEvent(SNIScopeExitId, message); #endregion } internal readonly struct SNIEventScope : IDisposable { - private readonly long _scopeID; + private readonly long scopeId; - public SNIEventScope(long scopeID) - { - _scopeID = scopeID; - } + public SNIEventScope(long scopeID) => scopeId = scopeID; + public void Dispose() => + SqlClientEventSource.Log.SNIScopeLeave(string.Format("Exiting SNI Scope {0}", scopeId)); - public void Dispose() - { - SqlClientEventSource.Log.SNIScopeLeave(_scopeID); - } - - public static SNIEventScope Create(string message) - { - return new SNIEventScope(SqlClientEventSource.Log.SNIScopeEnter(message)); - } + public static SNIEventScope Create(string message) => new SNIEventScope(SqlClientEventSource.Log.SNIScopeEnter(message)); } } From 72d33118b3ce8c0d304a08dfa0a5bf3554f39baa Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Mon, 1 Feb 2021 11:37:36 -0800 Subject: [PATCH 02/11] SNI Tracing improvements --- .../Data/SqlClient/SNI/LocalDB.Windows.cs | 34 +++--- .../Data/SqlClient/SNI/SNINpHandle.cs | 64 +++++------ .../Microsoft/Data/SqlClient/SNI/SNIProxy.cs | 14 +-- .../Data/SqlClient/SNI/SNITcpHandle.cs | 82 +++++++------- .../src/Microsoft/Data/SqlClient/SNI/SSRP.cs | 49 +++++---- .../SNI/SslOverTdsStream.NetCoreApp.cs | 101 +++++++++++------- .../SNI/SslOverTdsStream.NetStandard.cs | 77 ++++++++----- .../Data/SqlClient/SNI/SslOverTdsStream.cs | 4 +- 8 files changed, 232 insertions(+), 193 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs index d449b32235..aa17caa49a 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs @@ -64,26 +64,16 @@ internal static uint MapLocalDBErrorStateToCode(LocalDBErrorState errorState) switch (errorState) { case LocalDBErrorState.NO_INSTALLATION: - SqlClientEventSource.Log.TrySNITraceEvent(" LocalDB is not installed. Error State ={0}", errorState); return SNICommon.LocalDBNoInstallation; - case LocalDBErrorState.INVALID_CONFIG: - SqlClientEventSource.Log.TrySNITraceEvent(" Invalid configuration. Error State ={0}", errorState); return SNICommon.LocalDBInvalidConfig; - case LocalDBErrorState.NO_SQLUSERINSTANCEDLL_PATH: - SqlClientEventSource.Log.TrySNITraceEvent(" No SQL user instance path. Error State ={0}", errorState); return SNICommon.LocalDBNoSqlUserInstanceDllPath; - case LocalDBErrorState.INVALID_SQLUSERINSTANCEDLL_PATH: - SqlClientEventSource.Log.TrySNITraceEvent(" Invalid SQL user instance path. Error State ={0}", errorState); return SNICommon.LocalDBInvalidSqlUserInstanceDllPath; - case LocalDBErrorState.NONE: return 0; - default: - SqlClientEventSource.Log.TrySNITraceEvent(" Invalid configuration. Error State ={0}", errorState); return SNICommon.LocalDBInvalidConfig; } } @@ -112,7 +102,7 @@ internal static string MapLocalDBErrorStateToErrorMessage(LocalDBErrorState erro /// private bool LoadUserInstanceDll() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.LocalDB.Windows.LoadUserInstanceDll | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | INFO | SCOPE | Entering Scope {0}"); try { // Check in a non thread-safe way if the handle is already set for performance. @@ -137,7 +127,7 @@ private bool LoadUserInstanceDll() if (dllPath == null) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, MapLocalDBErrorStateToCode(registryQueryErrorState), MapLocalDBErrorStateToErrorMessage(registryQueryErrorState)); - SqlClientEventSource.Log.TrySNITraceEvent("User instance DLL path is null."); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | ERR | User instance DLL path is null."); return false; } @@ -145,7 +135,7 @@ private bool LoadUserInstanceDll() if (string.IsNullOrWhiteSpace(dllPath)) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBInvalidSqlUserInstanceDllPath, Strings.SNI_ERROR_55); - SqlClientEventSource.Log.TrySNITraceEvent(" User instance DLL path is invalid. DLL path ={0}", dllPath); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | ERR | User instance DLL path is invalid. DLL path = {0}", dllPath); return false; } @@ -155,7 +145,7 @@ private bool LoadUserInstanceDll() if (libraryHandle.IsInvalid) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBFailedToLoadDll, Strings.SNI_ERROR_56); - SqlClientEventSource.Log.TrySNITraceEvent(" Library Handle is invalid. Could not load the dll."); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | ERR | Library Handle is invalid. Could not load the dll."); libraryHandle.Dispose(); return false; } @@ -166,7 +156,7 @@ private bool LoadUserInstanceDll() if (_startInstanceHandle == IntPtr.Zero) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, Strings.SNI_ERROR_57); - SqlClientEventSource.Log.TrySNITraceEvent(" Was not able to load the PROC from DLL. Bad Runtime."); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | ERR | Was not able to load the PROC from DLL. Bad Runtime."); libraryHandle.Dispose(); return false; } @@ -183,7 +173,7 @@ private bool LoadUserInstanceDll() } _sqlUserInstanceLibraryHandle = libraryHandle; - SqlClientEventSource.Log.TrySNITraceEvent(" User Instance DLL was loaded successfully."); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | INFO | User Instance DLL was loaded successfully."); return true; } } @@ -200,7 +190,7 @@ private bool LoadUserInstanceDll() /// private string GetUserInstanceDllPath(out LocalDBErrorState errorState) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(" GetUserInstanceDllPath"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | SCOPE | INFO | Entering Scope {0} "); try { string dllPath = null; @@ -209,7 +199,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (key == null) { errorState = LocalDBErrorState.NO_INSTALLATION; - SqlClientEventSource.Log.TrySNITraceEvent(" not installed. Error state ={0}.", errorState); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | No installation found."); return null; } @@ -224,7 +214,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (!Version.TryParse(subKey, out currentKeyVersion)) { errorState = LocalDBErrorState.INVALID_CONFIG; - SqlClientEventSource.Log.TrySNITraceEvent(" Invalid Configuration. state ={0}.", errorState); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | Invalid Configuration."); return null; } @@ -238,7 +228,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (latestVersion.Equals(zeroVersion)) { errorState = LocalDBErrorState.INVALID_CONFIG; - SqlClientEventSource.Log.TrySNITraceEvent(" Invalid Configuration. state ={0}.", errorState); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | Invalid Configuration."); return null; } @@ -251,7 +241,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (instanceAPIPathRegistryObject == null) { errorState = LocalDBErrorState.NO_SQLUSERINSTANCEDLL_PATH; - SqlClientEventSource.Log.TrySNITraceEvent(" No SQL user instance DLL. Instance API Path Registry Object Error. state ={0}.", errorState); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | No SQL user instance DLL. Instance API Path Registry Object Error."); return null; } @@ -260,7 +250,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (valueKind != RegistryValueKind.String) { errorState = LocalDBErrorState.INVALID_SQLUSERINSTANCEDLL_PATH; - SqlClientEventSource.Log.TrySNITraceEvent(" No SQL user instance DLL. state ={0}. Registry value kind error.", errorState); + SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | Invalid SQL user instance DLL path. Registry value kind mismatch."); return null; } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs index 74c0bcb5c1..a8ca34b6f9 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs @@ -38,8 +38,8 @@ internal sealed class SNINpHandle : SNIPhysicalHandle public SNINpHandle(string serverName, string pipeName, long timerExpire) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.ctor | SNI | INFO | SCOPE | Enter Scope"); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ctor | SNI | INFO | Connection Id {0}, Setting server name = {1}, pipe name = {2}", _connectionId, serverName, pipeName); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.ctor | SNI | INFO | SCOPE | Entering Scope {0}"); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ctor | SNI | INFO | Connection Id {0}, Setting server name = {1}, pipe name = {2}", _connectionId, serverName, pipeName); try { _sendSync = new object(); @@ -70,14 +70,14 @@ public SNINpHandle(string serverName, string pipeName, long timerExpire) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.ConnOpenFailedError, te); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ctor | SNI | ERR | Connection Id {0}, Connection Timed out. Error Code 1 Exception = {1}", _connectionId, te?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ctor | SNI | ERR | Connection Id {0}, Connection Timed out. Error Code 1 Exception = {1}", _connectionId, te?.Message); return; } catch (IOException ioe) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.ConnOpenFailedError, ioe); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ctor | SNI | ERR | Connection Id {0}, IO Exception occurred. Error Code 1 Exception = {1}", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ctor | SNI | ERR | Connection Id {0}, IO Exception occurred. Error Code 1 Exception = {1}", _connectionId, ioe?.Message); return; } @@ -85,7 +85,7 @@ public SNINpHandle(string serverName, string pipeName, long timerExpire) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, 0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ctor | SNI | ERR | Connection Id {0}, Pipe Stream not operational. Error Code 1 Exception = {1}", _connectionId, Strings.SNI_ERROR_1); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ctor | SNI | ERR | Connection Id {0}, Pipe Stream not operational. Error Code 1 Exception = {1}", _connectionId, Strings.SNI_ERROR_1); return; } @@ -134,17 +134,17 @@ public override int ProtocolVersion public override uint CheckConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0} "); try { if (!_stream.CanWrite || !_stream.CanRead) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.CheckConnection | SNI | ERR | Connection Id {0}, Cannot write or read to/from the stream", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.CheckConnection | SNI | ERR | Connection Id {0}, Cannot write or read to/from the stream", _connectionId); return TdsEnums.SNI_ERROR; } else { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.CheckConnection | SNI | INFO | Connection Id {0}, Can read and write to/from stream.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.CheckConnection | SNI | INFO | Connection Id {0}, Can read and write to/from stream.", _connectionId); return TdsEnums.SNI_SUCCESS; } } @@ -178,13 +178,13 @@ public override void Dispose() //Release any references held by _stream. _stream = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Dispose | SNI | INFO | Connection Id {0}, All streams disposed and references cleared.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Dispose | SNI | INFO | Connection Id {0}, All streams disposed and references cleared.", _connectionId); } } public override uint Receive(out SNIPacket packet, int timeout) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0} "); try { SNIPacket errorPacket; @@ -195,14 +195,14 @@ public override uint Receive(out SNIPacket packet, int timeout) { packet = RentPacket(headerSize: 0, dataSize: _bufferSize); packet.ReadFromStream(_stream); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Receive | SNI | INFO | Connection Id {0}, Rented and read packet, dataLeft {1}", _connectionId, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Receive | SNI | INFO | Connection Id {0}, Rented and read packet, dataLeft {1}", _connectionId, packet?.DataLeft); if (packet.Length == 0) { errorPacket = packet; packet = null; var e = new Win32Exception(); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Receive | SNI | ERR | Connection Id {0}, Packet length found 0, Win32 exception raised: {1}", _connectionId, e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Receive | SNI | ERR | Connection Id {0}, Packet length found 0, Win32 exception raised: {1}", _connectionId, e?.Message); return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message); } } @@ -210,14 +210,14 @@ public override uint Receive(out SNIPacket packet, int timeout) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (IOException ioe) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Receive | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Receive | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); return ReportErrorAndReleasePacket(errorPacket, ioe); } return TdsEnums.SNI_SUCCESS; @@ -231,7 +231,7 @@ public override uint Receive(out SNIPacket packet, int timeout) public override uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0} "); try { SNIPacket errorPacket; @@ -240,21 +240,21 @@ public override uint ReceiveAsync(ref SNIPacket packet) try { packet.ReadFromStreamAsync(_stream, _receiveCallback); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Rented and read packet asynchronously, dataLeft {1}", _connectionId, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Rented and read packet asynchronously, dataLeft {1}", _connectionId, packet?.DataLeft); return TdsEnums.SNI_SUCCESS_IO_PENDING; } catch (ObjectDisposedException ode) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReceiveAsync | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReceiveAsync | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (IOException ioe) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReceiveAsync | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReceiveAsync | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); return ReportErrorAndReleasePacket(errorPacket, ioe); } } @@ -266,7 +266,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) public override uint Send(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.Send | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.Send | SNI | INFO | SCOPE | Entering Scope {0} "); try { bool releaseLock = false; @@ -293,18 +293,18 @@ public override uint Send(SNIPacket packet) { try { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Send | SNI | INFO | Connection Id {0}, Packet writing to stream, dataLeft {1}", _connectionId, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Send | SNI | INFO | Connection Id {0}, Packet writing to stream, dataLeft {1}", _connectionId, packet?.DataLeft); packet.WriteToStream(_stream); return TdsEnums.SNI_SUCCESS; } catch (ObjectDisposedException ode) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Send | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Send | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); return ReportErrorAndReleasePacket(packet, ode); } catch (IOException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.Send | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Send | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); return ReportErrorAndReleasePacket(packet, ioe); } } @@ -325,11 +325,11 @@ public override uint Send(SNIPacket packet) public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { SNIAsyncCallback cb = callback ?? _sendCallback; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.SendAsync | SNI | INFO | Connection Id {0}, Packet writing to stream, dataLeft {1}", _connectionId, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.SendAsync | SNI | INFO | Connection Id {0}, Packet writing to stream, dataLeft {1}", _connectionId, packet?.DataLeft); packet.WriteToStreamAsync(_stream, cb, SNIProviders.NP_PROV); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -347,7 +347,7 @@ public override void SetAsyncCallbacks(SNIAsyncCallback receiveCallback, SNIAsyn public override uint EnableSsl(uint options) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.EnableSsl | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.EnableSsl | SNI | INFO | SCOPE | Entering Scope {0}"); try { _validateCert = (options & TdsEnums.SNI_SSL_VALIDATE_CERTIFICATE) != 0; @@ -359,12 +359,12 @@ public override uint EnableSsl(uint options) } catch (AuthenticationException aue) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.EnableSsl | SNI | ERR | Connection Id {0}, AuthenticationException message = {1}.", ConnectionId, aue.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.EnableSsl | SNI | ERR | Connection Id {0}, AuthenticationException message = {1}.", ConnectionId, aue.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, aue); } catch (InvalidOperationException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.EnableSsl | SNI | ERR | Connection Id {0}, InvalidOperationException message = {1}.", ConnectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.EnableSsl | SNI | ERR | Connection Id {0}, InvalidOperationException message = {1}.", ConnectionId, ioe.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, ioe); } _stream = _sslStream; @@ -396,16 +396,16 @@ public override void DisableSsl() /// true if valid private bool ValidateServerCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNINpHandle.ValidateServerCertificate | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.ValidateServerCertificate | SNI | INFO | SCOPE | Entering Scope {0}"); try { if (!_validateCert) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate validation not requested.", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate validation not requested.", ConnectionId); return true; } - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Proceeding to SSL certificate validation.", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Proceeding to SSL certificate validation.", ConnectionId); return SNICommon.ValidateSslServerCertificate(_targetServer, cert, policyErrors); } finally @@ -429,7 +429,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, Exception sniExceptio { ReturnPacket(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1} ", ConnectionId, sniException.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1} ", ConnectionId, sniException.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, sniException); } @@ -439,7 +439,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, uint nativeError, uin { ReturnPacket(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1} ", ConnectionId, errorMessage); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1} ", ConnectionId, errorMessage); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, nativeError, sniError, errorMessage); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs index d12f94cfe0..aede8fd3d8 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs @@ -42,12 +42,12 @@ internal uint EnableSsl(SNIHandle handle, uint options) { try { - SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.EnableSsl | Info | Session Id {0}", handle?.ConnectionId); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.EnableSsl | Info | Session Id {0}", handle?.ConnectionId); return handle.EnableSsl(options); } catch (Exception e) { - SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.EnableSsl | Err | Session Id {0}, SNI Handshake failed with exception: {1} ", handle?.ConnectionId, e?.Message); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.EnableSsl | Err | Session Id {0}, SNI Handshake failed with exception: {1} ", handle?.ConnectionId, e?.Message); return SNICommon.ReportSNIError(SNIProviders.SSL_PROV, SNICommon.HandshakeFailureError, e); } } @@ -59,7 +59,7 @@ internal uint EnableSsl(SNIHandle handle, uint options) /// SNI error code internal uint DisableSsl(SNIHandle handle) { - SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.DisableSsl | Info | Session Id {0}", handle?.ConnectionId); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.DisableSsl | Info | Session Id {0}", handle?.ConnectionId); handle.DisableSsl(); return TdsEnums.SNI_SUCCESS; } @@ -211,7 +211,7 @@ internal uint ReadSyncOverAsync(SNIHandle handle, out SNIPacket packet, int time internal uint GetConnectionId(SNIHandle handle, ref Guid clientConnectionId) { clientConnectionId = handle.ConnectionId; - SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.GetConnectionId | Info | Session Id {0}", clientConnectionId); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.GetConnectionId | Info | Session Id {0}", clientConnectionId); return TdsEnums.SNI_SUCCESS; } @@ -235,7 +235,7 @@ internal uint WritePacket(SNIHandle handle, SNIPacket packet, bool sync) result = handle.SendAsync(packet); } - SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.WritePacket | Info | Session Id {0}, SendAsync Result {1}", handle?.ConnectionId, result); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.WritePacket | Info | Session Id {0}, SendAsync Result {1}", handle?.ConnectionId, result); return result; } @@ -303,7 +303,7 @@ internal SNIHandle CreateConnectionHandle(string fullServerName, bool ignoreSniO } } - SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.CreateConnectionHandle | Info | Session Id {0}, SNI Handle Type: {1}", sniHandle?.ConnectionId, sniHandle.GetType()); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.CreateConnectionHandle | Info | Session Id {0}, SNI Handle Type: {1}", sniHandle?.ConnectionId, sniHandle.GetType()); return sniHandle; } @@ -327,7 +327,7 @@ private static byte[] GetSqlServerSPN(DataSource dataSource) postfix = DefaultSqlServerPort.ToString(); } - SqlClientEventSource.Log.TryTraceEvent("SNI.SNIProxy.GetSqlServerSPN | Info | ServerName {0}, InstanceName {1}, Port {2}, postfix {3}", dataSource.ServerName, dataSource.InstanceName, dataSource.Port, postfix); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.GetSqlServerSPN | Info | ServerName {0}, InstanceName {1}, Port {2}, postfix {3}", dataSource.ServerName, dataSource.InstanceName, dataSource.Port, postfix); return GetSqlServerSPN(hostName, postfix); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs index 912a40596b..9c6cdcc630 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs @@ -67,7 +67,7 @@ public override void Dispose() //Release any references held by _stream. _stream = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Dispose | SNI | INFO | Connection Id {0}, All streams disposed.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Dispose | SNI | INFO | Connection Id {0}, All streams disposed.", _connectionId); } } @@ -119,8 +119,8 @@ public override int ProtocolVersion /// Used for DNS Cache public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNITCPHandle.ctor | SNI | INFO | SCOPE | Enter Scope"); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Setting server name = {1}", _connectionId, serverName); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNITCPHandle.ctor | SNI | INFO | SCOPE | Entering Scope {0}"); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Setting server name = {1}", _connectionId, serverName); try { _targetServer = serverName; @@ -144,7 +144,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel bool reportError = true; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Connecting to serverName {1} and port {2}", _connectionId, serverName, port); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Connecting to serverName {1} and port {2}", _connectionId, serverName, port); // We will always first try to connect with serverName as before and let the DNS server to resolve the serverName. // If the DSN resolution fails, we will try with IPs in the DNS cache if existed. We try with IPv4 first and followed by IPv6 if // IPv4 fails. The exceptions will be throw to upper level and be handled as before. @@ -166,13 +166,13 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel { if (hasCachedDNSInfo == false) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0}, Cached DNS Info not found, exception occurred thrown: {1}", _connectionId, ex?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0}, Cached DNS Info not found, exception occurred thrown: {1}", _connectionId, ex?.Message); throw; } else { int portRetry = string.IsNullOrEmpty(cachedDNSInfo.Port) ? port : int.Parse(cachedDNSInfo.Port); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Retrying with cached DNS IP Address {1} and port {2}", _connectionId, cachedDNSInfo.AddrIPv4, cachedDNSInfo.Port); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Retrying with cached DNS IP Address {1} and port {2}", _connectionId, cachedDNSInfo.AddrIPv4, cachedDNSInfo.Port); try { @@ -190,7 +190,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel if (exRetry is SocketException || exRetry is ArgumentNullException || exRetry is ArgumentException || exRetry is ArgumentOutOfRangeException || exRetry is AggregateException) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Retrying exception {1}", _connectionId, exRetry?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Retrying exception {1}", _connectionId, exRetry?.Message); if (parallel) { _socket = TryConnectParallel(cachedDNSInfo.AddrIPv6, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo); @@ -202,7 +202,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel } else { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0}, Retry failed, exception occurred: {1}", _connectionId, exRetry?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0}, Retry failed, exception occurred: {1}", _connectionId, exRetry?.Message); throw; } } @@ -224,10 +224,10 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel if (reportError) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0} could not be opened, exception occurred: {1}", _connectionId, Strings.SNI_ERROR_40); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} could not be opened, exception occurred: {1}", _connectionId, Strings.SNI_ERROR_40); ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); } - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket could not be opened.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket could not be opened.", _connectionId); return; } @@ -239,20 +239,20 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket exception occurred: Error Code {1}, Message {2}", _connectionId, se.SocketErrorCode, se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket exception occurred: Error Code {1}, Message {2}", _connectionId, se.SocketErrorCode, se.Message); ReportTcpSNIError(se); return; } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Exception occurred: {1}", _connectionId, e.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Exception occurred: {1}", _connectionId, e.Message); ReportTcpSNIError(e); return; } _stream = _tcpStream; _status = TdsEnums.SNI_SUCCESS; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ctor | SNI | INFO | Connection Id {0} Socket opened successfully, TCP stream ready.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0} Socket opened successfully, TCP stream ready.", _connectionId); } finally { @@ -276,7 +276,7 @@ private Socket TryConnectParallel(string hostName, int port, TimeSpan ts, bool i { // Fail if above 64 to match legacy behavior callerReportError = false; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.TryConnectParallel | SNI | ERR | Connection Id {0} serverAddresses.Length {1} Exception: {2}", _connectionId, serverAddresses.Length, Strings.SNI_ERROR_47); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.TryConnectParallel | SNI | ERR | Connection Id {0} serverAddresses.Length {1} Exception: {2}", _connectionId, serverAddresses.Length, Strings.SNI_ERROR_47); ReportTcpSNIError(0, SNICommon.MultiSubnetFailoverWithMoreThan64IPs, Strings.SNI_ERROR_47); return availableSocket; } @@ -306,7 +306,7 @@ private Socket TryConnectParallel(string hostName, int port, TimeSpan ts, bool i if (!(isInfiniteTimeOut ? connectTask.Wait(-1) : connectTask.Wait(ts))) { callerReportError = false; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.TryConnectParallel | SNI | ERR | Connection Id {0} Connection timed out, Exception: {1}", _connectionId, Strings.SNI_ERROR_40); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.TryConnectParallel | SNI | ERR | Connection Id {0} Connection timed out, Exception: {1}", _connectionId, Strings.SNI_ERROR_40); ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); return availableSocket; } @@ -365,7 +365,7 @@ void Cancel() } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Connect | SNI | ERR | THIS EXCEPTION IS BEING SWALLOWED: {0}", e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Connect | SNI | ERR | THIS EXCEPTION IS BEING SWALLOWED: {0}", e?.Message); } } } @@ -390,7 +390,7 @@ void Cancel() // enable keep-alive on socket SetKeepAliveValues(ref sockets[i]); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Connect | SNI | INFO | Connecting to IP address {0} and port {1}", ipAddresses[i], port); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Connect | SNI | INFO | Connecting to IP address {0} and port {1}", ipAddresses[i], port); sockets[i].Connect(ipAddresses[i], port); if (sockets[i] != null) // sockets[i] can be null if cancel callback is executed during connect() { @@ -409,7 +409,7 @@ void Cancel() } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Connect | SNI | ERR | THIS EXCEPTION IS BEING SWALLOWED: {0}", e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Connect | SNI | ERR | THIS EXCEPTION IS BEING SWALLOWED: {0}", e?.Message); } } } @@ -537,17 +537,17 @@ public override uint EnableSsl(uint options) } catch (AuthenticationException aue) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Authentication exception occurred: {1}", _connectionId, aue.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Authentication exception occurred: {1}", _connectionId, aue.Message); return ReportTcpSNIError(aue); } catch (InvalidOperationException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Invalid Operation Exception occurred: {1}", _connectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Invalid Operation Exception occurred: {1}", _connectionId, ioe.Message); return ReportTcpSNIError(ioe); } _stream = _sslStream; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.EnableSsl | SNI | INFO | Connection Id {0}, SSL enabled successfully.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | INFO | Connection Id {0}, SSL enabled successfully.", _connectionId); return TdsEnums.SNI_SUCCESS; } @@ -561,7 +561,7 @@ public override void DisableSsl() _sslOverTdsStream.Dispose(); _sslOverTdsStream = null; _stream = _tcpStream; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.DisableSsl | SNI | INFO | Connection Id {0}, SSL Disabled. Communication will continue on TCP Stream.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.DisableSsl | SNI | INFO | Connection Id {0}, SSL Disabled. Communication will continue on TCP Stream.", _connectionId); } /// @@ -576,11 +576,11 @@ private bool ValidateServerCertificate(object sender, X509Certificate cert, X509 { if (!_validateCert) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate will not be validated.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate will not be validated.", _connectionId); return true; } - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate will be validated for Target Server name", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate will be validated for Target Server name", _connectionId); return SNICommon.ValidateSslServerCertificate(_targetServer, cert, policyErrors); } @@ -625,22 +625,22 @@ public override uint Send(SNIPacket packet) try { packet.WriteToStream(_stream); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Send | SNI | INFO | Connection Id {0}, Data sent to stream synchronously", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Send | SNI | INFO | Connection Id {0}, Data sent to stream synchronously", _connectionId); return TdsEnums.SNI_SUCCESS; } catch (ObjectDisposedException ode) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Send | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Send | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); return ReportTcpSNIError(ode); } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Send | SNI | ERR | Connection Id {0}, SocketException occurred: {1}", _connectionId, se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Send | SNI | ERR | Connection Id {0}, SocketException occurred: {1}", _connectionId, se.Message); return ReportTcpSNIError(se); } catch (IOException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Send | SNI | ERR | Connection Id {0}, IOException occurred: {1}", _connectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Send | SNI | ERR | Connection Id {0}, IOException occurred: {1}", _connectionId, ioe.Message); return ReportTcpSNIError(ioe); } } @@ -680,7 +680,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) else { // otherwise it is timeout for 0 or less than -1 - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Error 258, Timeout error occurred.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Error 258, Timeout error occurred.", _connectionId); ReportTcpSNIError(0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11); return TdsEnums.SNI_WAIT_TIMEOUT; } @@ -693,25 +693,25 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) errorPacket = packet; packet = null; var e = new Win32Exception(); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Win32 exception occurred: {1}", _connectionId, e.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Win32 exception occurred: {1}", _connectionId, e.Message); return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message); } - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | INFO | Connection Id {0}, Data read from stream synchronously", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | INFO | Connection Id {0}, Data read from stream synchronously", _connectionId); return TdsEnums.SNI_SUCCESS; } catch (ObjectDisposedException ode) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (SocketException se) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Socket exception occurred: {1}", _connectionId, se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Socket exception occurred: {1}", _connectionId, se.Message); return ReportErrorAndReleasePacket(errorPacket, se); } catch (IOException ioe) @@ -721,11 +721,11 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) uint errorCode = ReportErrorAndReleasePacket(errorPacket, ioe); if (ioe.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred with Wait Timeout (error 258): {1}", _connectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred with Wait Timeout (error 258): {1}", _connectionId, ioe.Message); errorCode = TdsEnums.SNI_WAIT_TIMEOUT; } - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred: {1}", _connectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred: {1}", _connectionId, ioe.Message); return errorCode; } finally @@ -754,12 +754,12 @@ public override void SetAsyncCallbacks(SNIAsyncCallback receiveCallback, SNIAsyn /// SNI error code public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNI.SNITcpHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNITcpHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { SNIAsyncCallback cb = callback ?? _sendCallback; packet.WriteToStreamAsync(_stream, cb, SNIProviders.TCP_PROV); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.SendAsync | SNI | INFO | Connection Id {0}, Data sent to stream asynchronously", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.SendAsync | SNI | INFO | Connection Id {0}, Data sent to stream asynchronously", _connectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } finally @@ -781,7 +781,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) try { packet.ReadFromStreamAsync(_stream, _receiveCallback); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Data received from stream asynchronously", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Data received from stream asynchronously", _connectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } catch (Exception e) when (e is ObjectDisposedException || e is SocketException || e is IOException) @@ -812,18 +812,18 @@ public override uint CheckConnection() // return true we can safely determine that the connection is no longer active. if (!_socket.Connected || (_socket.Poll(100, SelectMode.SelectRead) && _socket.Available == 0)) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket not usable.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket not usable.", _connectionId); return TdsEnums.SNI_ERROR; } } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket Exception occurred: {1}", _connectionId, se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket Exception occurred: {1}", _connectionId, se.Message); return ReportTcpSNIError(se); } catch (ObjectDisposedException ode) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); return ReportTcpSNIError(ode); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs index 781d914f63..72e34e5803 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs @@ -26,7 +26,7 @@ internal static int GetPortByInstanceName(string browserHostName, string instanc { Debug.Assert(!string.IsNullOrWhiteSpace(browserHostName), "browserHostName should not be null, empty, or whitespace"); Debug.Assert(!string.IsNullOrWhiteSpace(instanceName), "instanceName should not be null, empty, or whitespace"); - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SSRP.GetPortByInstanceName | SNI | SCOPE | Entering Scope {0}"); try { byte[] instanceInfoRequest = CreateInstanceInfoRequest(instanceName); @@ -37,7 +37,7 @@ internal static int GetPortByInstanceName(string browserHostName, string instanc } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SSRP.GetPortByInstanceName |SNI|ERR> SocketException Message = {0}", se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SSRP.GetPortByInstanceName | SNI | ERR | SocketException Message = {0}", se.Message); throw new Exception(SQLMessage.SqlServerBrowserNotAccessible(), se); } @@ -73,7 +73,7 @@ internal static int GetPortByInstanceName(string browserHostName, string instanc private static byte[] CreateInstanceInfoRequest(string instanceName) { Debug.Assert(!string.IsNullOrWhiteSpace(instanceName), "instanceName should not be null, empty, or whitespace"); - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(""); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SSRP.CreateInstanceInfoRequest | SNI | SCOPE | Entering Scope {0}"); try { const byte ClntUcastInst = 0x04; @@ -150,28 +150,39 @@ private static byte[] CreateDacPortInfoRequest(string instanceName) /// response packet from UDP server private static byte[] SendUDPRequest(string browserHostname, int port, byte[] requestPacket) { - Debug.Assert(!string.IsNullOrWhiteSpace(browserHostname), "browserhostname should not be null, empty, or whitespace"); - Debug.Assert(port >= 0 && port <= 65535, "Invalid port"); - Debug.Assert(requestPacket != null && requestPacket.Length > 0, "requestPacket should not be null or 0-length array"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SSRP.SendUDPRequest | SNI | SCOPE | Entering Scope {0}"); + try + { + Debug.Assert(!string.IsNullOrWhiteSpace(browserHostname), "browserhostname should not be null, empty, or whitespace"); + Debug.Assert(port >= 0 && port <= 65535, "Invalid port"); + Debug.Assert(requestPacket != null && requestPacket.Length > 0, "requestPacket should not be null or 0-length array"); - const int sendTimeOutMs = 1000; - const int receiveTimeOutMs = 1000; + const int sendTimeOutMs = 1000; + const int receiveTimeOutMs = 1000; - IPAddress address = null; - bool isIpAddress = IPAddress.TryParse(browserHostname, out address); + IPAddress address = null; + bool isIpAddress = IPAddress.TryParse(browserHostname, out address); - byte[] responsePacket = null; - using (UdpClient client = new UdpClient(!isIpAddress ? AddressFamily.InterNetwork : address.AddressFamily)) - { - Task sendTask = client.SendAsync(requestPacket, requestPacket.Length, browserHostname, port); - Task receiveTask = null; - if (sendTask.Wait(sendTimeOutMs) && (receiveTask = client.ReceiveAsync()).Wait(receiveTimeOutMs)) + byte[] responsePacket = null; + using (UdpClient client = new UdpClient(!isIpAddress ? AddressFamily.InterNetwork : address.AddressFamily)) { - responsePacket = receiveTask.Result.Buffer; + Task sendTask = client.SendAsync(requestPacket, requestPacket.Length, browserHostname, port); + Task receiveTask = null; + + SqlClientEventSource.Log.TrySNITraceEvent("SSRP.SendUDPRequest | SNI | INFO | Waiting for UDP Client to fetch Port info."); + if (sendTask.Wait(sendTimeOutMs) && (receiveTask = client.ReceiveAsync()).Wait(receiveTimeOutMs)) + { + SqlClientEventSource.Log.TrySNITraceEvent("SSRP.SendUDPRequest | SNI | INFO | Received Port info from UDP Client."); + responsePacket = receiveTask.Result.Buffer; + } } - } - return responsePacket; + return responsePacket; + } + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetCoreApp.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetCoreApp.cs index 97a1181ae3..ab1ef65a68 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetCoreApp.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetCoreApp.cs @@ -25,13 +25,13 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati public override int Read(Span buffer) { - if (!_encapsulate) - { - return _stream.Read(buffer); - } - - using (SNIEventScope.Create(" reading encapsulated bytes")) + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.Read | SNI | INFO | SCOPE | Entering Scope {0}"); + try { + if (!_encapsulate) + { + return _stream.Read(buffer); + } if (_packetBytes > 0) { // there are queued bytes from a previous packet available @@ -72,28 +72,33 @@ public override int Read(Span buffer) return packetBytesRead; } } + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) { - if (!_encapsulate) + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.ReadAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + try { - int read; + if (!_encapsulate) { - ValueTask readValueTask = _stream.ReadAsync(buffer, cancellationToken); - if (readValueTask.IsCompletedSuccessfully) - { - read = readValueTask.Result; - } - else + int read; { - read = await readValueTask.ConfigureAwait(false); + ValueTask readValueTask = _stream.ReadAsync(buffer, cancellationToken); + if (readValueTask.IsCompletedSuccessfully) + { + read = readValueTask.Result; + } + else + { + read = await readValueTask.ConfigureAwait(false); + } } + return read; } - return read; - } - using (SNIEventScope.Create(" reading encapsulated bytes")) - { if (_packetBytes > 0) { // there are queued bytes from a previous packet available @@ -171,21 +176,26 @@ public override async ValueTask ReadAsync(Memory buffer, Cancellation return packetBytesRead; } } + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } public override void Write(ReadOnlySpan buffer) { - // During the SSL negotiation phase, SSL is tunnelled over TDS packet type 0x12. After - // negotiation, the underlying socket only sees SSL frames. - if (!_encapsulate) + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.Write | SNI | INFO | SCOPE | Entering Scope {0}"); + try { - _stream.Write(buffer); - _stream.Flush(); - return; - } + // During the SSL negotiation phase, SSL is tunnelled over TDS packet type 0x12. After + // negotiation, the underlying socket only sees SSL frames. + if (!_encapsulate) + { + _stream.Write(buffer); + _stream.Flush(); + return; + } - using (SNIEventScope.Create(" writing encapsulated bytes")) - { ReadOnlySpan remaining = buffer; byte[] packetBuffer = null; try @@ -224,29 +234,34 @@ public override void Write(ReadOnlySpan buffer) } } } + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) { - if (!_encapsulate) + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.WriteAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + try { + if (!_encapsulate) { - ValueTask valueTask = _stream.WriteAsync(buffer, cancellationToken); - if (!valueTask.IsCompletedSuccessfully) { - await valueTask.ConfigureAwait(false); + ValueTask valueTask = _stream.WriteAsync(buffer, cancellationToken); + if (!valueTask.IsCompletedSuccessfully) + { + await valueTask.ConfigureAwait(false); + } } + Task flushTask = _stream.FlushAsync(); + if (flushTask.IsCompletedSuccessfully) + { + await flushTask.ConfigureAwait(false); + } + return; } - Task flushTask = _stream.FlushAsync(); - if (flushTask.IsCompletedSuccessfully) - { - await flushTask.ConfigureAwait(false); - } - return; - } - using (SNIEventScope.Create(" writing encapsulated bytes")) - { ReadOnlyMemory remaining = buffer; byte[] packetBuffer = null; try @@ -291,6 +306,10 @@ public override async ValueTask WriteAsync(ReadOnlyMemory buffer, Cancella } } } + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetStandard.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetStandard.cs index 7798b25d06..3ee7f77c93 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetStandard.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetStandard.cs @@ -14,13 +14,14 @@ internal sealed partial class SslOverTdsStream : Stream { public override int Read(byte[] buffer, int offset, int count) { - if (!_encapsulate) + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.Read | SNI | INFO | SCOPE | Entering Scope {0}"); + try { - return _stream.Read(buffer, offset, count); - } + if (!_encapsulate) + { + return _stream.Read(buffer, offset, count); + } - using (SNIEventScope.Create(" reading encapsulated bytes")) - { if (_packetBytes > 0) { // there are queued bytes from a previous packet available @@ -65,18 +66,22 @@ public override int Read(byte[] buffer, int offset, int count) return packetBytesRead; } } - + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { - if (!_encapsulate) + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.ReadAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + try { - return await _stream.ReadAsync(buffer, offset, count, cancellationToken); - } + if (!_encapsulate) + { + return await _stream.ReadAsync(buffer, offset, count, cancellationToken); + } - using (SNIEventScope.Create(" reading encapsulated bytes")) - { if (_packetBytes > 0) { // there are queued bytes from a previous packet available @@ -121,21 +126,26 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, return packetBytesRead; } } + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } public override void Write(byte[] buffer, int offset, int count) { - // During the SSL negotiation phase, SSL is tunnelled over TDS packet type 0x12. After - // negotiation, the underlying socket only sees SSL frames. - if (!_encapsulate) + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.Write | SNI | INFO | SCOPE | Entering Scope {0}"); + try { - _stream.Write(buffer, offset, count); - _stream.Flush(); - return; - } + // During the SSL negotiation phase, SSL is tunnelled over TDS packet type 0x12. After + // negotiation, the underlying socket only sees SSL frames. + if (!_encapsulate) + { + _stream.Write(buffer, offset, count); + _stream.Flush(); + return; + } - using (SNIEventScope.Create(" writing encapsulated bytes")) - { int remainingBytes = count; int dataOffset = offset; byte[] packetBuffer = null; @@ -169,23 +179,28 @@ public override void Write(byte[] buffer, int offset, int count) ArrayPool.Shared.Return(packetBuffer, clearArray: true); } } + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { - if (!_encapsulate) + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.WriteAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + try { - await _stream.WriteAsync(buffer, offset, count).ConfigureAwait(false); - Task flushTask = _stream.FlushAsync(); - if (flushTask.Status == TaskStatus.RanToCompletion) + if (!_encapsulate) { - await flushTask.ConfigureAwait(false); + await _stream.WriteAsync(buffer, offset, count).ConfigureAwait(false); + Task flushTask = _stream.FlushAsync(); + if (flushTask.Status == TaskStatus.RanToCompletion) + { + await flushTask.ConfigureAwait(false); + } + return; } - return; - } - using (SNIEventScope.Create(" writing encapsulated bytes")) - { int remainingBytes = count; int dataOffset = offset; byte[] packetBuffer = null; @@ -219,6 +234,10 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc ArrayPool.Shared.Return(packetBuffer, clearArray: true); } } + finally + { + SqlClientEventSource.Log.TrySNIScopeLeaveEvent(scopeID); + } } } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs index 552a17fcf2..9b14452429 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -42,7 +42,7 @@ public SslOverTdsStream(Stream stream, Guid connectionId) public void FinishHandshake() { _encapsulate = false; - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SslOverTdsStream.FinishHandshake | SNI | INFO | Connection Id {0}, Switched from encapsulation to passthrough mode", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SslOverTdsStream.FinishHandshake | SNI | INFO | Connection Id {0}, Switched from encapsulation to passthrough mode", _connectionId); } /// @@ -61,7 +61,7 @@ public override void Flush() if (!(_stream is PipeStream)) { _stream.Flush(); - SqlClientEventSource.Log.TrySNITraceEvent("SNI.SslOverTdsStream.Flush | SNI | INFO | Connection Id {0}, Flushed stream", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SslOverTdsStream.Flush | SNI | INFO | Connection Id {0}, Flushed stream", _connectionId); } } From 7f792f2d68c03665f27e856a4a22d394ae541a9a Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Mon, 1 Feb 2021 16:31:23 -0800 Subject: [PATCH 03/11] Fix tests --- .../src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs index 9b14452429..a1eb4125ad 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -29,7 +29,7 @@ internal sealed partial class SslOverTdsStream : Stream /// /// Underlying stream /// Connection Id of parent stream handle - public SslOverTdsStream(Stream stream, Guid connectionId) + public SslOverTdsStream(Stream stream, Guid connectionId = default) { _stream = stream; _connectionId = connectionId; From 47f588c7d402d881c90e1327dd2dff47bcc70760 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 3 Feb 2021 00:50:22 -0800 Subject: [PATCH 04/11] Apply suggestions from code review Co-authored-by: David Engel --- .../Data/SqlClient/SNI/SNIMarsConnection.cs | 6 ++--- .../Data/SqlClient/SNI/SNIMarsHandle.cs | 26 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs index 6d36c4282f..57770e712a 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs @@ -135,7 +135,7 @@ public uint SendAsync(SNIPacket packet, SNIAsyncCallback callback) /// SNI error code public uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { if (packet != null) @@ -168,7 +168,7 @@ public uint ReceiveAsync(ref SNIPacket packet) /// SNI error status public uint CheckConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (this) @@ -194,7 +194,7 @@ public void HandleReceiveError(SNIPacket packet) { handle.HandleReceiveError(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveError | SNI | ERR | Trace | MARS Session Id {0}, Packet {1} has Completion Callback ", ConnectionId, packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveError | SNI | ERR | Trace | MARS Session Id {0}, Packet {1} has Completion Callback", ConnectionId, packet?._id); } else { diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs index ea47745fba..b3e0bd1418 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs @@ -189,7 +189,7 @@ public override uint Send(SNIPacket packet) { muxedPacket = SetPacketSMUXHeader(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Send | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SMUX Packet is going to be sent. ", ConnectionId, _sequenceNumber, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Send | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SMUX Packet is going to be sent.", ConnectionId, _sequenceNumber, _sendHighwater); return _connection.Send(muxedPacket); } finally @@ -258,7 +258,7 @@ private uint SendPendingPackets() } _sendPacketQueue.Dequeue(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | MARS Session Id {0}, _sendPacketQueue dequeued, count ", ConnectionId, _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | MARS Session Id {0}, _sendPacketQueue dequeued, count {1}", ConnectionId, _sendPacketQueue?.Count); continue; } else @@ -288,7 +288,7 @@ private uint SendPendingPackets() /// SNI error code public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (this) @@ -297,7 +297,7 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul } SendPendingPackets(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendAsync | SNI | INFO | MARS Session Id {0}, _sendPacketQueue enqueued, count {1}, ", ConnectionId, _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendAsync | SNI | INFO | MARS Session Id {0}, _sendPacketQueue enqueued, count {1}", ConnectionId, _sendPacketQueue?.Count); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -314,7 +314,7 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul /// SNI error code public override uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (_receivedPacketQueue) @@ -323,14 +323,14 @@ public override uint ReceiveAsync(ref SNIPacket packet) if (_connectionError != null) { - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | ERR | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, _connectionError {5} ", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, _connectionError); + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | ERR | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, _connectionError {5}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, _connectionError); return SNICommon.ReportSNIError(_connectionError); } if (queueCount == 0) { _asyncReceives++; - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | Info | MARS Session Id {0}, queueCount 0, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} ", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck); + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | Info | MARS Session Id {0}, queueCount 0, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -340,7 +340,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) if (queueCount == 1) { #if DEBUG - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | Info | MARS Session Id {0}, packet dequeued {1}, packet Owner {2}, packet refCount {3}, received Packet Queue count {4} ", ConnectionId, packet?._id, packet?._owner, packet?._refCount, _receivedPacketQueue?.Count); + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | Info | MARS Session Id {0}, packet dequeued {1}, packet Owner {2}, packet refCount {3}, received Packet Queue count {4}", ConnectionId, packet?._id, packet?._owner, packet?._refCount, _receivedPacketQueue?.Count); #endif _packetEvent.Reset(); } @@ -351,7 +351,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) _receiveHighwater++; } - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, queueCount {5} ", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, _receivedPacketQueue?.Count); + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, queueCount {5}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, _receivedPacketQueue?.Count); SendAckIfNecessary(); return TdsEnums.SNI_SUCCESS; } @@ -366,7 +366,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) /// public void HandleReceiveError(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleReceiveError | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleReceiveError | SNI | INFO | SCOPE | Entering Scope {0}"); try { // SNIMarsHandle should only receive calls to this function from the SNIMarsConnection aggregator class @@ -421,7 +421,7 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) /// Send highwater mark public void HandleAck(uint highwater) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleAck | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleAck | SNI | INFO | SCOPE | Entering Scope {0}"); try { lock (this) @@ -482,7 +482,7 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { _receiveHighwater++; } - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.HandleReceiveComplete | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} ", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck); + SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.HandleReceiveComplete | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck); SendAckIfNecessary(); } finally @@ -520,7 +520,7 @@ private void SendAckIfNecessary() /// SNI error code public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0}"); try { packet = null; From 0e85661552cf3ad24855dbd2319929c7918619ba Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 3 Feb 2021 19:05:45 -0800 Subject: [PATCH 05/11] Apply suggestions from code review Co-authored-by: David Engel --- .../src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs | 12 ++++++------ .../src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs | 2 -- .../src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs | 2 +- .../Microsoft/Data/SqlClient/TdsParserStateObject.cs | 12 ++++++------ 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs index a8ca34b6f9..9a1cbde035 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs @@ -134,7 +134,7 @@ public override int ProtocolVersion public override uint CheckConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0}"); try { if (!_stream.CanWrite || !_stream.CanRead) @@ -184,7 +184,7 @@ public override void Dispose() public override uint Receive(out SNIPacket packet, int timeout) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0}"); try { SNIPacket errorPacket; @@ -231,7 +231,7 @@ public override uint Receive(out SNIPacket packet, int timeout) public override uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0}"); try { SNIPacket errorPacket; @@ -266,7 +266,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) public override uint Send(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.Send | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.Send | SNI | INFO | SCOPE | Entering Scope {0}"); try { bool releaseLock = false; @@ -429,7 +429,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, Exception sniExceptio { ReturnPacket(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1} ", ConnectionId, sniException.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1}", ConnectionId, sniException.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, sniException); } @@ -439,7 +439,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, uint nativeError, uin { ReturnPacket(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1} ", ConnectionId, errorMessage); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1}", ConnectionId, errorMessage); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, nativeError, sniError, errorMessage); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs index 4b7526203e..ccaefb5958 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs @@ -36,7 +36,6 @@ internal sealed class SNIPacket internal readonly SNIHandle _owner; // used in debug builds to check that packets are being returned to the correct pool internal string _traceTag; // used in debug builds to assist tracing what steps the packet has been through - #if TRACE_HISTORY [DebuggerDisplay("{Action.ToString(),nq}")] internal struct History @@ -80,7 +79,6 @@ public SNIPacket(SNIHandle owner, int id) if (_data != null) { SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.dtor | SNI | ERR | Finalizer called for unreleased SNIPacket, Connection Id {0}, Packet Id {1}, _refCount {2}, DataLeft {3}, tag {4}", _owner?.ConnectionId, _id, _refCount, DataLeft, _traceTag); - //Debug.Fail($@"finalizer called for unreleased SNIPacket, tag: {_traceTag}"); } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs index aede8fd3d8..7996e17ed3 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs @@ -47,7 +47,7 @@ internal uint EnableSsl(SNIHandle handle, uint options) } catch (Exception e) { - SqlClientEventSource.Log.TryTraceEvent("SNIProxy.EnableSsl | Err | Session Id {0}, SNI Handshake failed with exception: {1} ", handle?.ConnectionId, e?.Message); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.EnableSsl | Err | Session Id {0}, SNI Handshake failed with exception: {1}", handle?.ConnectionId, e?.Message); return SNICommon.ReportSNIError(SNIProviders.SSL_PROV, SNICommon.HandshakeFailureError, e); } } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs index b490a85ac4..88ea4dc3d8 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs @@ -564,8 +564,8 @@ internal bool TryInitialize(TdsParserStateObject stateObj, int columnsCount) { return false; } - SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, NBCROW bitmap received, column count = {1}", stateObj.ObjectID, columnsCount); - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" State Object Id {0}, Null Bitmap {1}, NBCROW bitmap data: {2}", stateObj.ObjectID, _nullBitmap, (ushort)_nullBitmap.Length); + SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap received, column count = {1}", stateObj.ObjectID, columnsCount); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap data. Null Bitmap {1}, Null bitmap length: {2}", stateObj.ObjectID, _nullBitmap, (ushort)_nullBitmap.Length); return true; } @@ -2811,7 +2811,7 @@ public void ProcessSniPacket(IntPtr packet, UInt32 error) } } SniReadStatisticsAndTracing(); - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" State Object Id {0}, In Buffer {1}, Packet read: {2}", ObjectID, _inBuff, (ushort)_inBytesRead); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParser.ReadNetworkPacketAsyncCallback | INFO | ADV | State Object Id {0}, Packet read. In Buffer {1}, In Bytes Read: {2}", ObjectID, _inBuff, (ushort)_inBytesRead); AssertValidState(); } else @@ -3557,8 +3557,8 @@ internal void SendAttention(bool mustTakeWriteLock = false) _attentionSending = false; } - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" State Object Id {0}, Out Buffer {1}, Packet sent {2}", ObjectID, _outBuff, (ushort)_outBytesUsed); - SqlClientEventSource.Log.TryTraceEvent(" Attention sent to the server."); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParser.WritePacket | INFO | ADV | State Object Id {0}, Packet sent. Out Buffer {1}, Out Bytes Used {2}", ObjectID, _outBuff, (ushort)_outBytesUsed); + SqlClientEventSource.Log.TryTraceEvent("TdsParser.SendAttention | INFO | Attention sent to the server."); AssertValidState(); } @@ -3744,7 +3744,7 @@ private void SniWriteStatisticsAndTracing() _traceChangePasswordLength = 0; } } - SqlClientEventSource.Log.TryAdvancedTraceBinEvent(" State Object Id {0}, Out buffer: {1}, Packet sent: {2}", ObjectID, _outBuff, (ushort)_outBytesUsed); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParser.WritePacket | INFO | ADV | State Object Id {0}, Packet sent. Out buffer: {1}, Out Bytes Used: {2}", ObjectID, _outBuff, (ushort)_outBytesUsed); } [Conditional("DEBUG")] From d37f465cd41cf70cdae45dea03739444875c43a7 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 3 Feb 2021 19:08:03 -0800 Subject: [PATCH 06/11] Apply suggestions from code review Co-authored-by: David Engel --- .../src/Microsoft/Data/SqlClient/TdsParserStateObject.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs index a7b963ab2e..1179ae65dd 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs @@ -543,7 +543,7 @@ internal bool TryInitialize(TdsParserStateObject stateObj, int columnsCount) } SqlClientEventSource.Log.TryAdvancedTraceEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap received, column count = {1}", stateObj._objectID, columnsCount); - SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, NBCROW bitmap data: ", stateObj._objectID, _nullBitmap, (ushort)_nullBitmap.Length); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.NullBitmap.Initialize | INFO | ADV | State Object Id {0}, Null Bitmap length {1}, NBCROW bitmap data: {2}", stateObj._objectID, (ushort)_nullBitmap.Length, _nullBitmap); return true; } @@ -3502,7 +3502,7 @@ internal void SendAttention(bool mustTakeWriteLock = false) _attentionSending = false; } - SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.SendAttention | INFO | ADV | State Object Id {0}, Out Buffer {1}, Packet sent: {2}", _objectID, _outBuff, (ushort)_outBytesUsed); + SqlClientEventSource.Log.TryAdvancedTraceBinEvent("TdsParserStateObject.SendAttention | INFO | ADV | State Object Id {0}, Packet sent. Out Buffer {1}, Out Bytes Used: {2}", _objectID, _outBuff, (ushort)_outBytesUsed); SqlClientEventSource.Log.TryTraceEvent("TdsParserStateObject.SendAttention | Info | State Object Id {0}, Attention sent to the server.", _objectID); AssertValidState(); From d237e679606809e4f449830467ca20c709dc3091 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 3 Feb 2021 19:25:39 -0800 Subject: [PATCH 07/11] Small fix --- .../Data/SqlClient/SNI/SNIMarsHandle.cs | 4 ++-- .../Data/SqlClient/SNI/SNINpHandle.cs | 2 +- .../Microsoft/Data/SqlClient/SNI/SNIProxy.cs | 4 ++-- .../Data/SqlClient/SNI/SNITcpHandle.cs | 24 +++++++++---------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs index b3e0bd1418..e0c39a293f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs @@ -454,7 +454,7 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { if (_sendHighwater != header.highwater) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, header.highwater {1}, _sendHighwater {2}, Handle Ack with header.highwater", ConnectionId, header.highwater, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, header.highwater {1}, _sendHighwater {2}, Handle Ack with header.highwater", ConnectionId, header?.highwater, _sendHighwater); HandleAck(header.highwater); } @@ -464,7 +464,7 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { _receivedPacketQueue.Enqueue(packet); _packetEvent.Set(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _receivedPacketQueue count {3}, packet event set", ConnectionId, _sequenceNumber, _sendHighwater, _receivedPacketQueue.Count); + SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _receivedPacketQueue count {3}, packet event set", ConnectionId, _sequenceNumber, _sendHighwater, _receivedPacketQueue?.Count); return; } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs index 9a1cbde035..009366d6f0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs @@ -429,7 +429,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, Exception sniExceptio { ReturnPacket(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1}", ConnectionId, sniException.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1}", ConnectionId, sniException?.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, sniException); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs index 7996e17ed3..1204b129d0 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs @@ -303,7 +303,7 @@ internal SNIHandle CreateConnectionHandle(string fullServerName, bool ignoreSniO } } - SqlClientEventSource.Log.TryTraceEvent("SNIProxy.CreateConnectionHandle | Info | Session Id {0}, SNI Handle Type: {1}", sniHandle?.ConnectionId, sniHandle.GetType()); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.CreateConnectionHandle | Info | Session Id {0}, SNI Handle Type: {1}", sniHandle?.ConnectionId, sniHandle?.GetType()); return sniHandle; } @@ -327,7 +327,7 @@ private static byte[] GetSqlServerSPN(DataSource dataSource) postfix = DefaultSqlServerPort.ToString(); } - SqlClientEventSource.Log.TryTraceEvent("SNIProxy.GetSqlServerSPN | Info | ServerName {0}, InstanceName {1}, Port {2}, postfix {3}", dataSource.ServerName, dataSource.InstanceName, dataSource.Port, postfix); + SqlClientEventSource.Log.TryTraceEvent("SNIProxy.GetSqlServerSPN | Info | ServerName {0}, InstanceName {1}, Port {2}, postfix {3}", dataSource?.ServerName, dataSource?.InstanceName, dataSource?.Port, postfix); return GetSqlServerSPN(hostName, postfix); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs index 9c6cdcc630..1e36db4199 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs @@ -239,13 +239,13 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket exception occurred: Error Code {1}, Message {2}", _connectionId, se.SocketErrorCode, se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket exception occurred: Error Code {1}, Message {2}", _connectionId, se?.SocketErrorCode, se?.Message); ReportTcpSNIError(se); return; } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Exception occurred: {1}", _connectionId, e.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Exception occurred: {1}", _connectionId, e?.Message); ReportTcpSNIError(e); return; } @@ -537,12 +537,12 @@ public override uint EnableSsl(uint options) } catch (AuthenticationException aue) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Authentication exception occurred: {1}", _connectionId, aue.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Authentication exception occurred: {1}", _connectionId, aue?.Message); return ReportTcpSNIError(aue); } catch (InvalidOperationException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Invalid Operation Exception occurred: {1}", _connectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Invalid Operation Exception occurred: {1}", _connectionId, ioe?.Message); return ReportTcpSNIError(ioe); } @@ -693,7 +693,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) errorPacket = packet; packet = null; var e = new Win32Exception(); - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Win32 exception occurred: {1}", _connectionId, e.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Win32 exception occurred: {1}", _connectionId, e?.Message); return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message); } @@ -704,14 +704,14 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode?.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (SocketException se) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Socket exception occurred: {1}", _connectionId, se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Socket exception occurred: {1}", _connectionId, se?.Message); return ReportErrorAndReleasePacket(errorPacket, se); } catch (IOException ioe) @@ -721,11 +721,11 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) uint errorCode = ReportErrorAndReleasePacket(errorPacket, ioe); if (ioe.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred with Wait Timeout (error 258): {1}", _connectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred with Wait Timeout (error 258): {1}", _connectionId, ioe?.Message); errorCode = TdsEnums.SNI_WAIT_TIMEOUT; } - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred: {1}", _connectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred: {1}", _connectionId, ioe?.Message); return errorCode; } finally @@ -812,18 +812,18 @@ public override uint CheckConnection() // return true we can safely determine that the connection is no longer active. if (!_socket.Connected || (_socket.Poll(100, SelectMode.SelectRead) && _socket.Available == 0)) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket not usable.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.CheckConnection | SNI | INFO | Connection Id {0}, Socket not usable.", _connectionId); return TdsEnums.SNI_ERROR; } } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket Exception occurred: {1}", _connectionId, se.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket Exception occurred: {1}", _connectionId, se?.Message); return ReportTcpSNIError(se); } catch (ObjectDisposedException ode) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode?.Message); return ReportTcpSNIError(ode); } From e202e108743dbb471ec93d27871a4bc8b63eeeba Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Tue, 16 Feb 2021 13:56:12 -0800 Subject: [PATCH 08/11] SslOverTdsStream old constructor re-added for tests --- .../src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs index a1eb4125ad..10fdd7fd28 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -24,6 +24,12 @@ internal sealed partial class SslOverTdsStream : Stream private const int PACKET_SIZE_WITHOUT_HEADER = TdsEnums.DEFAULT_LOGIN_PACKET_SIZE - TdsEnums.HEADER_LEN; private const int PRELOGIN_PACKET_TYPE = 0x12; + /// + /// Constructor + /// + /// Underlying stream + public SslOverTdsStream(Stream stream) : this(stream, default) { } + /// /// Constructor /// From 483c87a1f56d927b1ff662b0021ab7767639c684 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 17 Feb 2021 16:53:33 -0800 Subject: [PATCH 09/11] Method Name and Class Name --- .../Data/SqlClient/SNI/LocalDB.Windows.cs | 5 +- .../Data/SqlClient/SNI/SNIMarsConnection.cs | 64 +++++++------- .../Data/SqlClient/SNI/SNIMarsHandle.cs | 88 ++++++++++--------- .../Data/SqlClient/SNI/SNINpHandle.cs | 1 + .../Data/SqlClient/SqlClientEventSource.cs | 67 ++++++++------ 5 files changed, 124 insertions(+), 101 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs index aa17caa49a..e6801e05b1 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs @@ -13,6 +13,7 @@ namespace Microsoft.Data.SqlClient.SNI internal sealed class LocalDB { private static readonly LocalDB Instance = new LocalDB(); + private const string s_className = nameof(LocalDB); //HKEY_LOCAL_MACHINE private const string LocalDBInstalledVersionRegistryKey = "SOFTWARE\\Microsoft\\Microsoft SQL Server Local DB\\Installed Versions\\"; @@ -102,7 +103,7 @@ internal static string MapLocalDBErrorStateToErrorMessage(LocalDBErrorState erro /// private bool LoadUserInstanceDll() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { // Check in a non thread-safe way if the handle is already set for performance. @@ -190,7 +191,7 @@ private bool LoadUserInstanceDll() /// private string GetUserInstanceDllPath(out LocalDBErrorState errorState) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | SCOPE | INFO | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { string dllPath = null; diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs index 57770e712a..badae43200 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs @@ -14,6 +14,8 @@ namespace Microsoft.Data.SqlClient.SNI /// internal class SNIMarsConnection { + private const string s_className = nameof(SNIMarsConnection); + private readonly Guid _connectionId = Guid.NewGuid(); private readonly Dictionary _sessions = new Dictionary(); private readonly byte[] _headerBytes = new byte[SNISMUXHeader.HEADER_LENGTH]; @@ -44,7 +46,7 @@ public Guid ConnectionId public SNIMarsConnection(SNIHandle lowerHandle) { _lowerHandle = lowerHandle; - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.ctor | SNI | INFO | Trace | Created MARS Session Id {0}", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "Created MARS Session Id {0}", args0: ConnectionId); _lowerHandle.SetAsyncCallbacks(HandleReceiveComplete, HandleSendComplete); } @@ -55,7 +57,7 @@ public SNIMarsHandle CreateMarsSession(object callbackObject, bool async) ushort sessionId = _nextSessionId++; SNIMarsHandle handle = new SNIMarsHandle(this, sessionId, callbackObject, async); _sessions.Add(sessionId, handle); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.CreateMarsSession | SNI | INFO | Trace | MARS Session Id {0}, SNI MARS Handle Id {1}, created new MARS Session {2}", ConnectionId, handle?.ConnectionId, sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, SNI MARS Handle Id {1}, created new MARS Session {2}", args0: ConnectionId, args1: handle?.ConnectionId, args2: sessionId); return handle; } } @@ -66,17 +68,17 @@ public SNIMarsHandle CreateMarsSession(object callbackObject, bool async) /// public uint StartReceive() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.StartReceive | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { SNIPacket packet = null; if (ReceiveAsync(ref packet) == TdsEnums.SNI_SUCCESS_IO_PENDING) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.StartReceive | SNI | INFO | Trace | MARS Session Id {0}, Success IO pending.", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Success IO pending.", args0: ConnectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.StartReceive | SNI | ERR | MARS Session Id {0}, Connection not usable.", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Connection not usable.", args0: ConnectionId); return SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnNotUsableError, Strings.SNI_ERROR_19); } finally @@ -92,7 +94,7 @@ public uint StartReceive() /// SNI error code public uint Send(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.Send | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { lock (this) @@ -114,7 +116,7 @@ public uint Send(SNIPacket packet) /// SNI error code public uint SendAsync(SNIPacket packet, SNIAsyncCallback callback) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { lock (this) @@ -135,14 +137,14 @@ public uint SendAsync(SNIPacket packet, SNIAsyncCallback callback) /// SNI error code public uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (packet != null) { ReturnPacket(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.ReceiveAsync | SNI | INFO | Trace | MARS Session Id {0}, Packet {1} returned", ConnectionId, packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Packet {1} returned", args0: ConnectionId, args1: packet?._id); #endif packet = null; } @@ -151,7 +153,7 @@ public uint ReceiveAsync(ref SNIPacket packet) { var response = _lowerHandle.ReceiveAsync(ref packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.ReceiveAsync | SNI | INFO | Trace | MARS Session Id {0}, Received new packet {1}", ConnectionId, packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Received new packet {1}", args0: ConnectionId, args1: packet?._id); #endif return response; } @@ -168,7 +170,7 @@ public uint ReceiveAsync(ref SNIPacket packet) /// SNI error status public uint CheckConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { lock (this) @@ -194,11 +196,11 @@ public void HandleReceiveError(SNIPacket packet) { handle.HandleReceiveError(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveError | SNI | ERR | Trace | MARS Session Id {0}, Packet {1} has Completion Callback", ConnectionId, packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Packet {1} has Completion Callback", args0: ConnectionId, args1: packet?._id); } else { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveError | SNI | ERR | Trace | MARS Session Id {0}, Packet {1} does not have Completion Callback, error not handled.", ConnectionId, packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Packet {1} does not have Completion Callback, error not handled.", args0: ConnectionId, args1: packet?._id); #endif } } @@ -223,7 +225,7 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) /// SNI error code public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | SCOPE | Entering Scope {0} "); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { SNISMUXHeader currentHeader = null; @@ -235,7 +237,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) lock (this) { HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, Handled receive error code: {1}", _lowerHandle?.ConnectionId, sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); return; } } @@ -258,7 +260,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (bytesTaken == 0) { sniErrorCode = ReceiveAsync(ref packet); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | Trace | MARS Session Id {0}, Non-SMUX Header SNI Packet received with code {1}", ConnectionId, sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Non-SMUX Header SNI Packet received with code {1}", args0: ConnectionId, args1: sniErrorCode); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { @@ -266,7 +268,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, Handled receive error code: {1}", _lowerHandle?.ConnectionId, sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); return; } } @@ -275,7 +277,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) _dataBytesLeft = (int)_currentHeader.length; _currentPacket = _lowerHandle.RentPacket(headerSize: 0, dataSize: (int)_currentHeader.length); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, _dataBytesLeft {1}, _currentPacket {2}, Reading data of length: _currentHeader.length {3}", _lowerHandle?.ConnectionId, _dataBytesLeft, currentPacket?._id, _currentHeader?.length); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _dataBytesLeft {1}, _currentPacket {2}, Reading data of length: _currentHeader.length {3}", args0: _lowerHandle?.ConnectionId, args1: _dataBytesLeft, args2: currentPacket?._id, args3: _currentHeader?.length); #endif } @@ -292,7 +294,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (_dataBytesLeft > 0) { sniErrorCode = ReceiveAsync(ref packet); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | Trace | MARS Session Id {0}, SMUX DATA Header SNI Packet received with code {1}, _dataBytesLeft {2}", ConnectionId, sniErrorCode, _dataBytesLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, SMUX DATA Header SNI Packet received with code {1}, _dataBytesLeft {2}", args0: ConnectionId, args1: sniErrorCode, args2: _dataBytesLeft); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { @@ -300,7 +302,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, Handled receive error code: {1}", _lowerHandle?.ConnectionId, sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); return; } } @@ -312,7 +314,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.InvalidParameterError, Strings.SNI_ERROR_5); HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | Current Header Session Id {0} not found, MARS Session Id {1} will be destroyed, New SNI error created: {2}", _currentHeader?.sessionId, _lowerHandle?.ConnectionId, sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "Current Header Session Id {0} not found, MARS Session Id {1} will be destroyed, New SNI error created: {2}", args0: _currentHeader?.sessionId, args1: _lowerHandle?.ConnectionId, args2: sniErrorCode); _lowerHandle.Dispose(); _lowerHandle = null; return; @@ -321,19 +323,19 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_FIN) { _sessions.Remove(_currentHeader.sessionId); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | FIN | MARS Session Id {0}, SMUX_FIN flag received, Current Header Session Id {1} removed", _lowerHandle?.ConnectionId, _currentHeader?.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | FIN | ", "MARS Session Id {0}, SMUX_FIN flag received, Current Header Session Id {1} removed", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); } else { currentSession = _sessions[_currentHeader.sessionId]; - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, Current Session assigned to Session Id {1}", _lowerHandle?.ConnectionId, _currentHeader?.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Current Session assigned to Session Id {1}", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); } } if (currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { currentSession.HandleReceiveComplete(currentPacket, currentHeader); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | SMUX_DATA | MARS Session Id {0}, Current Session {1} completed receiving Data", _lowerHandle?.ConnectionId, _currentHeader?.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | SMUX_DATA | ", "MARS Session Id {0}, Current Session {1} completed receiving Data", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); } if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_ACK) @@ -341,16 +343,16 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) try { currentSession.HandleAck(currentHeader.highwater); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | SMUX_ACK | MARS Session Id {0}, Current Session {1} handled ack", _lowerHandle?.ConnectionId, _currentHeader?.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | SMUX_ACK | ", "MARS Session Id {0}, Current Session {1} handled ack", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, Exception occurred: {2}", _currentHeader?.sessionId, e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Exception occurred: {2}", args0: _currentHeader?.sessionId, args1: e?.Message); SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } #if DEBUG Debug.Assert(_currentPacket == currentPacket, "current and _current are not the same"); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | INFO | SMUX_ACK | MARS Session Id {0}, Current Packet {1} returned", _lowerHandle?.ConnectionId, currentPacket?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | SMUX_ACK | ", "MARS Session Id {0}, Current Packet {1} returned", args0: _lowerHandle?.ConnectionId, args1: currentPacket?._id); #endif ReturnPacket(currentPacket); currentPacket = null; @@ -369,7 +371,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsConnection.HandleReceiveComplete | SNI | ERR | MARS Session Id {0}, packet.DataLeft 0, SNI error {2}", _lowerHandle?.ConnectionId, sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, packet.DataLeft 0, SNI error {2}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); return; } } @@ -386,7 +388,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) /// public uint EnableSsl(uint options) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.EnableSsl | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { return _lowerHandle.EnableSsl(options); @@ -402,7 +404,7 @@ public uint EnableSsl(uint options) /// public void DisableSsl() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.DisableSsl | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { _lowerHandle.DisableSsl(); @@ -429,7 +431,7 @@ public void ReturnPacket(SNIPacket packet) /// public void KillConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsConnection.KillConnection | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { _lowerHandle.KillConnection(); diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs index e0c39a293f..7671cb33c1 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs @@ -15,6 +15,7 @@ namespace Microsoft.Data.SqlClient.SNI internal sealed class SNIMarsHandle : SNIHandle { private const uint ACK_THRESHOLD = 2; + private const string s_className = nameof(SNIMarsHandle); private readonly SNIMarsConnection _connection; private readonly uint _status = TdsEnums.SNI_UNINITIALIZED; @@ -54,15 +55,15 @@ internal sealed class SNIMarsHandle : SNIHandle /// public override void Dispose() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.Dispose | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { SendControlPacket(SNISMUXFlags.SMUX_FIN); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Dispose | SNI | INFO | MARS Session Id {0}, Sent SMUX_FIN packet to terminate session.", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Sent SMUX_FIN packet to terminate session.", args0: ConnectionId); } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Dispose | SNI | ERR | MARS Session Id {0}, Internal exception error = {1}, Member Name={2}", ConnectionId, e?.Message, e?.GetType()?.Name); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Internal exception error = {1}, Member Name={2}", args0: ConnectionId, args1: e?.Message, args2: e?.GetType()?.Name); SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } finally @@ -86,7 +87,7 @@ public SNIMarsHandle(SNIMarsConnection connection, ushort sessionId, object call _callbackObject = callbackObject; _handleSendCompleteCallback = HandleSendComplete; SendControlPacket(SNISMUXFlags.SMUX_SYN); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.ctor | SNI | INFO | MARS Session Id {0}, Sent SMUX_SYN packet to start a new session, session Id {1}", ConnectionId, _sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Sent SMUX_SYN packet to start a new session, session Id {1}", args0: ConnectionId, args1: _sessionId); _status = TdsEnums.SNI_SUCCESS; } @@ -101,7 +102,7 @@ private void SendControlPacket(SNISMUXFlags flags) { SNIPacket packet = RentPacket(headerSize: SNISMUXHeader.HEADER_LENGTH, dataSize: 0); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendControlPacket | SNI | INFO | MARS Session Id {0}, Packet rented {1}, packet dataLeft {2}", ConnectionId, packet?._id, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Packet rented {1}, packet dataLeft {2}", args0: ConnectionId, args1: packet?._id, args2: packet?.DataLeft); #endif lock (this) { @@ -113,7 +114,8 @@ private void SendControlPacket(SNISMUXFlags flags) _connection.Send(packet); ReturnPacket(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendControlPacket | SNI | INFO | MARS Session Id {0}, Packet returned {1}, packet dataLeft {2}", ConnectionId, packet?._id, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Packet returned {1}, packet dataLeft {2}", args0: ConnectionId, args1: packet?._id, args2: packet?.DataLeft); + ; #endif } finally @@ -148,7 +150,7 @@ private SNIPacket SetPacketSMUXHeader(SNIPacket packet) _currentHeader.Write(packet.GetHeaderBuffer(SNISMUXHeader.HEADER_LENGTH)); packet.SetHeaderActive(); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SetPacketSMUXHeader | SNI | INFO | MARS Session Id {0}, Setting SMUX_DATA header in current header for packet {1}", ConnectionId, packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Setting SMUX_DATA header in current header for packet {1}", args0: ConnectionId, args1: packet?._id); #endif return packet; } @@ -161,7 +163,7 @@ private SNIPacket SetPacketSMUXHeader(SNIPacket packet) public override uint Send(SNIPacket packet) { Debug.Assert(packet.ReservedHeaderSize == SNISMUXHeader.HEADER_LENGTH, "mars handle attempting to send muxed packet without smux reservation in Send"); - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.Send | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { while (true) @@ -174,12 +176,12 @@ public override uint Send(SNIPacket packet) } } - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Send | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for Acknowledgment event.", ConnectionId, _sequenceNumber, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for Acknowledgment event.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); _ackEvent.Wait(); lock (this) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | MARS Session Id {0}, _sendPacketQueue count found {1}, Acknowledgment event Reset", ConnectionId, _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sendPacketQueue count found {1}, Acknowledgment event Reset", args0: ConnectionId, args1: _sendPacketQueue?.Count); _ackEvent.Reset(); } } @@ -189,7 +191,7 @@ public override uint Send(SNIPacket packet) { muxedPacket = SetPacketSMUXHeader(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Send | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SMUX Packet is going to be sent.", ConnectionId, _sequenceNumber, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SMUX Packet is going to be sent.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); return _connection.Send(muxedPacket); } finally @@ -214,13 +216,13 @@ private uint InternalSendAsync(SNIPacket packet, SNIAsyncCallback callback) { if (_sequenceNumber >= _sendHighwater) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.InternalSendAsync | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SNI Queue is full", ConnectionId, _sequenceNumber, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SNI Queue is full", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); return TdsEnums.SNI_QUEUE_FULL; } SNIPacket muxedPacket = SetPacketSMUXHeader(packet); muxedPacket.SetCompletionCallback(callback ?? HandleSendComplete); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.InternalSendAsync | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Sending packet", ConnectionId, _sequenceNumber, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Sending packet", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); return _connection.SendAsync(muxedPacket, callback); } } @@ -236,7 +238,7 @@ private uint InternalSendAsync(SNIPacket packet, SNIAsyncCallback callback) /// SNI error code private uint SendPendingPackets() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); SNIMarsQueuedPacket packet = null; try { @@ -253,18 +255,18 @@ private uint SendPendingPackets() if (result != TdsEnums.SNI_SUCCESS && result != TdsEnums.SNI_SUCCESS_IO_PENDING) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | ERR | MARS Session Id {0}, InternalSendAsync result is not SNI_SUCCESS and is not SNI_SUCCESS_IO_PENDING", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, InternalSendAsync result is not SNI_SUCCESS and is not SNI_SUCCESS_IO_PENDING", args0: ConnectionId); return result; } _sendPacketQueue.Dequeue(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | MARS Session Id {0}, _sendPacketQueue dequeued, count {1}", ConnectionId, _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sendPacketQueue dequeued, count {1}", args0: ConnectionId, args1: _sendPacketQueue?.Count); continue; } else { _ackEvent.Set(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendPendingPackets | SNI | INFO | MARS Session Id {0}, _sendPacketQueue count found {1}, acknowledgment set", ConnectionId, _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sendPacketQueue count found {1}, acknowledgment set", args0: ConnectionId, args1: _sendPacketQueue?.Count); } } @@ -288,7 +290,7 @@ private uint SendPendingPackets() /// SNI error code public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { lock (this) @@ -297,7 +299,7 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul } SendPendingPackets(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.SendAsync | SNI | INFO | MARS Session Id {0}, _sendPacketQueue enqueued, count {1}", ConnectionId, _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sendPacketQueue enqueued, count {1}", args0: ConnectionId, args1: _sendPacketQueue?.Count); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -314,7 +316,7 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul /// SNI error code public override uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { lock (_receivedPacketQueue) @@ -323,14 +325,14 @@ public override uint ReceiveAsync(ref SNIPacket packet) if (_connectionError != null) { - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | ERR | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, _connectionError {5}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, _connectionError); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, _connectionError {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: _connectionError); return SNICommon.ReportSNIError(_connectionError); } if (queueCount == 0) { _asyncReceives++; - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | Info | MARS Session Id {0}, queueCount 0, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, queueCount 0, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -340,7 +342,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) if (queueCount == 1) { #if DEBUG - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | SNI | Info | MARS Session Id {0}, packet dequeued {1}, packet Owner {2}, packet refCount {3}, received Packet Queue count {4}", ConnectionId, packet?._id, packet?._owner, packet?._refCount, _receivedPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, packet dequeued {1}, packet Owner {2}, packet refCount {3}, received Packet Queue count {4}", args0: ConnectionId, args1: packet?._id, args2: packet?._owner, args3: packet?._refCount, args4: _receivedPacketQueue?.Count); #endif _packetEvent.Reset(); } @@ -351,7 +353,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) _receiveHighwater++; } - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.ReceiveAsync | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, queueCount {5}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, _receivedPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, queueCount {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: _receivedPacketQueue?.Count); SendAckIfNecessary(); return TdsEnums.SNI_SUCCESS; } @@ -366,7 +368,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) /// public void HandleReceiveError(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleReceiveError | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { // SNIMarsHandle should only receive calls to this function from the SNIMarsConnection aggregator class @@ -376,7 +378,7 @@ public void HandleReceiveError(SNIPacket packet) lock (_receivedPacketQueue) { _connectionError = SNILoadHandle.SingletonInstance.LastError; - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveError | SNI | ERR | MARS Session Id {0}, _connectionError to be handled: {1}", ConnectionId, _connectionError); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, _connectionError to be handled: {1}", args0: ConnectionId, args1: _connectionError); _packetEvent.Set(); } @@ -395,7 +397,7 @@ public void HandleReceiveError(SNIPacket packet) /// SNI error code public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleSendComplete | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { lock (this) @@ -406,7 +408,7 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) } _connection.ReturnPacket(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleSendComplete | SNI | INFO | MARS Session Id {0}, Returned Packet: {1}", ConnectionId, packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Returned Packet: {1}", args0: ConnectionId, args1: packet?._id); #endif } finally @@ -421,14 +423,14 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) /// Send highwater mark public void HandleAck(uint highwater) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleAck | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { lock (this) { if (_sendHighwater != highwater) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleAck | SNI | INFO | MARS Session Id {0}, Setting _sendHighwater {1} to highwater {2} and send pending packets.", ConnectionId, _sendHighwater, highwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Setting _sendHighwater {1} to highwater {2} and send pending packets.", args0: ConnectionId, args1: _sendHighwater, args2: highwater); _sendHighwater = highwater; SendPendingPackets(); } @@ -447,14 +449,14 @@ public void HandleAck(uint highwater) /// SMUX header public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { lock (this) { if (_sendHighwater != header.highwater) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, header.highwater {1}, _sendHighwater {2}, Handle Ack with header.highwater", ConnectionId, header?.highwater, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, header.highwater {1}, _sendHighwater {2}, Handle Ack with header.highwater", args0: ConnectionId, args1: header?.highwater, args2: _sendHighwater); HandleAck(header.highwater); } @@ -464,13 +466,13 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { _receivedPacketQueue.Enqueue(packet); _packetEvent.Set(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _receivedPacketQueue count {3}, packet event set", ConnectionId, _sequenceNumber, _sendHighwater, _receivedPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _receivedPacketQueue count {3}, packet event set", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _receivedPacketQueue?.Count); return; } _asyncReceives--; Debug.Assert(_callbackObject != null); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.HandleReceiveComplete | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _asyncReceives {3}", ConnectionId, _sequenceNumber, _sendHighwater, _asyncReceives); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _asyncReceives {3}", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _asyncReceives); ((TdsParserStateObject)_callbackObject).ReadAsyncCallback(PacketHandle.FromManagedPacket(packet), 0); } @@ -482,7 +484,7 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { _receiveHighwater++; } - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.HandleReceiveComplete | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck); SendAckIfNecessary(); } finally @@ -508,7 +510,7 @@ private void SendAckIfNecessary() if (receiveHighwater - receiveHighwaterLastAck > ACK_THRESHOLD) { SendControlPacket(SNISMUXFlags.SMUX_ACK); - SqlClientEventSource.Log.TryTraceEvent("SNIMarsHandle.SendAckIfNecessary | Info | MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} Sending acknowledgment > ACK_THRESHOLD {5}", ConnectionId, _asyncReceives, _receiveHighwater, _sendHighwater, _receiveHighwaterLastAck, ACK_THRESHOLD); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} Sending acknowledgment ACK_THRESHOLD {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: ACK_THRESHOLD); } } @@ -520,7 +522,7 @@ private void SendAckIfNecessary() /// SNI error code public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNIMarsHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { packet = null; @@ -533,12 +535,12 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) { if (_connectionError != null) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | ERR | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _connectionError found: {3}.", ConnectionId, _sequenceNumber, _sendHighwater, _connectionError); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _connectionError found: {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _connectionError); return SNICommon.ReportSNIError(_connectionError); } queueCount = _receivedPacketQueue.Count; - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, W_receivedPacketQueue count {3}.", ConnectionId, _sequenceNumber, _sendHighwater, queueCount); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, W_receivedPacketQueue count {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: queueCount); if (queueCount > 0) { @@ -547,7 +549,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) if (queueCount == 1) { _packetEvent.Reset(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, packet event reset, _receivedPacketQueue count 1.", ConnectionId, _sequenceNumber, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, packet event reset, _receivedPacketQueue count 1.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); } result = TdsEnums.SNI_SUCCESS; @@ -562,15 +564,15 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) } SendAckIfNecessary(); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, returning with result {3}.", ConnectionId, _sequenceNumber, _sendHighwater, result); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, returning with result {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: result); return result; } - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for packet event.", ConnectionId, _sequenceNumber, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for packet event.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); if (!_packetEvent.Wait(timeoutInMilliseconds)) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11); - SqlClientEventSource.Log.TrySNITraceEvent("SNIMarsHandle.Receive | SNI | INFO | MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _packetEvent wait timed out.", ConnectionId, _sequenceNumber, _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _packetEvent wait timed out.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); return TdsEnums.SNI_WAIT_TIMEOUT; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs index 009366d6f0..ce3f5e698c 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs @@ -19,6 +19,7 @@ namespace Microsoft.Data.SqlClient.SNI internal sealed class SNINpHandle : SNIPhysicalHandle { internal const string DefaultPipePath = @"sql\query"; // e.g. \\HOSTNAME\pipe\sql\query + // private const int MAX_PIPE_INSTANCES = 255; // TODO: Investigate pipe instance limit. private readonly string _targetServer; private readonly object _sendSync; diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs index 69bcc553e9..1e9252d0e8 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics.Tracing; +using System.Text; using System.Threading; namespace Microsoft.Data.SqlClient @@ -267,6 +268,9 @@ public static class Tasks internal bool IsSNIScopeEnabled() => Log.IsEnabled(EventLevel.Informational, Keywords.SNIScope); #endregion + private string GetFormattedMessage(string className, string memberName, string infoType, string message) => + new StringBuilder(className).Append(".").Append(memberName).Append(infoType).Append(message).ToString(); + #region overloads //Never use event writer directly as they are not checking for enabled/disabled situations. Always use overloads. @@ -274,7 +278,7 @@ public static class Tasks #region Traces without if statements [NonEvent] - internal void TraceEvent(string message, T0 args0, T1 args1) + internal void TraceEvent(string message, T0 args0, T1 args1, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { Trace(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr)); } @@ -361,11 +365,13 @@ internal void TryTraceEvent(string message, T0 args0, T1 #region Scope [NonEvent] - internal long TryScopeEnterEvent(string message) + internal long TryScopeEnterEvent(string className, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsScopeEnabled()) { - return ScopeEnter(message); + StringBuilder sb = new StringBuilder(className); + sb.Append(".").Append(memberName).Append(" | INFO | SCOPE | Entering Scope {0}"); + return SNIScopeEnter(sb.ToString()); } return 0; } @@ -415,7 +421,7 @@ internal void TryScopeLeaveEvent(long scopeId) { if (Log.IsScopeEnabled()) { - ScopeLeave(string.Format("Exiting Scope {0}", scopeId)); + ScopeLeave(string.Format("Exit Scope {0}", scopeId)); } } #endregion @@ -525,7 +531,7 @@ internal void TryNotificationScopeLeaveEvent(long scopeId) { if (Log.IsNotificationScopeEnabled()) { - NotificationScopeLeave(string.Format("Exiting Notification Scope {0}", scopeId)); + NotificationScopeLeave(string.Format("Exit Notification Scope {0}", scopeId)); } } #endregion @@ -584,7 +590,7 @@ internal void TryPoolerScopeLeaveEvent(long scopeId) { if (Log.IsPoolerScopeEnabled()) { - PoolerScopeLeave(string.Format("Exiting Pooler Scope {0}", scopeId)); + PoolerScopeLeave(string.Format("Exit Pooler Scope {0}", scopeId)); } } #endregion @@ -705,7 +711,7 @@ internal void TryAdvanceScopeLeave(long scopeId) { if (Log.IsAdvancedTraceOn()) { - AdvancedScopeLeave(string.Format("Exiting Advanced Scope {0}", scopeId)); + AdvancedScopeLeave(string.Format("Exit Advanced Scope {0}", scopeId)); } } @@ -796,67 +802,78 @@ internal void StateDumpEvent(string message, T0 args0, T1 args1) #region SNI Trace [NonEvent] - internal void TrySNITraceEvent(string message) + internal void TrySNITraceEvent(string className, string infoType, string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + { + if (Log.IsSNITraceEnabled()) + { + SNITrace(GetFormattedMessage(className, memberName, infoType, message)); + } + } + + [NonEvent] + internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(string.Format(message)); + SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string message, T0 args0) + internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(string.Format(message, args0?.ToString() ?? NullStr)); + SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string message, T0 args0, T1 args1) + internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, T2 args2, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr)); + SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string message, T0 args0, T1 args1, T2 args2) + internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, T2 args2, T3 args3, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr)); + SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string message, T0 args0, T1 args1, T2 args2, T3 args3) + internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr)); + SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr, args4?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4) + internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4, T5 args5, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr, args4?.ToString() ?? NullStr)); + SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr, args4?.ToString() ?? NullStr, args5?.ToString() ?? NullStr))); } } #endregion #region SNI Scope [NonEvent] - internal long TrySNIScopeEnterEvent(string message) + internal long TrySNIScopeEnterEvent(string className, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNIScopeEnabled()) { - return SNIScopeEnter(message); + StringBuilder sb = new StringBuilder(className); + sb.Append(".").Append(memberName).Append(" | SNI | INFO | SCOPE | Entering Scope {0}"); + return SNIScopeEnter(sb.ToString()); } return 0; } @@ -866,7 +883,7 @@ internal void TrySNIScopeLeaveEvent(long scopeId) { if (Log.IsSNIScopeEnabled()) { - SNIScopeLeave(string.Format("Exiting SNI Scope {0}", scopeId)); + SNIScopeLeave(string.Format("Exit SNI Scope {0}", scopeId)); } } #endregion @@ -992,11 +1009,11 @@ internal void SNIScopeLeave(string message) => internal readonly struct SNIEventScope : IDisposable { - private readonly long scopeId; + private readonly long _scopeId; - public SNIEventScope(long scopeID) => scopeId = scopeID; + public SNIEventScope(long scopeID) => _scopeId = scopeID; public void Dispose() => - SqlClientEventSource.Log.SNIScopeLeave(string.Format("Exiting SNI Scope {0}", scopeId)); + SqlClientEventSource.Log.SNIScopeLeave(string.Format("Exit SNI Scope {0}", _scopeId)); public static SNIEventScope Create(string message) => new SNIEventScope(SqlClientEventSource.Log.SNIScopeEnter(message)); } From de8fc98716f51251d7a1f3e8c8e704d157a6ad64 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Thu, 18 Feb 2021 17:24:39 -0800 Subject: [PATCH 10/11] Tracing updates + fixes --- .../Data/SqlClient/SNI/LocalDB.Windows.cs | 20 +-- .../Microsoft/Data/SqlClient/SNI/SNICommon.cs | 20 +-- .../Data/SqlClient/SNI/SNILoadHandle.cs | 1 - .../Data/SqlClient/SNI/SNIMarsConnection.cs | 44 +++---- .../Data/SqlClient/SNI/SNIMarsHandle.cs | 66 +++++----- .../Data/SqlClient/SNI/SNINpHandle.cs | 65 +++++----- .../Microsoft/Data/SqlClient/SNI/SNIPacket.cs | 31 ++--- .../Data/SqlClient/SNI/SNITcpHandle.cs | 83 +++++++------ .../src/Microsoft/Data/SqlClient/SNI/SSRP.cs | 13 +- .../SNI/SslOverTdsStream.NetCoreApp.cs | 8 +- .../SNI/SslOverTdsStream.NetStandard.cs | 8 +- .../Data/SqlClient/SNI/SslOverTdsStream.cs | 5 +- .../Microsoft/Data/SqlClient/SqlCommand.cs | 15 ++- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 2 +- .../Microsoft/Data/SqlClient/SqlCommand.cs | 7 +- .../Data/SqlClient/SqlClientEventSource.cs | 117 ++++++++++++------ 16 files changed, 278 insertions(+), 227 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs index e6801e05b1..6e1f0e19e8 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/LocalDB.Windows.cs @@ -128,7 +128,7 @@ private bool LoadUserInstanceDll() if (dllPath == null) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, MapLocalDBErrorStateToCode(registryQueryErrorState), MapLocalDBErrorStateToErrorMessage(registryQueryErrorState)); - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | ERR | User instance DLL path is null."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "User instance DLL path is null."); return false; } @@ -136,7 +136,7 @@ private bool LoadUserInstanceDll() if (string.IsNullOrWhiteSpace(dllPath)) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBInvalidSqlUserInstanceDllPath, Strings.SNI_ERROR_55); - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | ERR | User instance DLL path is invalid. DLL path = {0}", dllPath); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "User instance DLL path is invalid. DLL path = {0}", dllPath); return false; } @@ -146,7 +146,7 @@ private bool LoadUserInstanceDll() if (libraryHandle.IsInvalid) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBFailedToLoadDll, Strings.SNI_ERROR_56); - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | ERR | Library Handle is invalid. Could not load the dll."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Library Handle is invalid. Could not load the dll."); libraryHandle.Dispose(); return false; } @@ -157,7 +157,7 @@ private bool LoadUserInstanceDll() if (_startInstanceHandle == IntPtr.Zero) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.LocalDBBadRuntime, Strings.SNI_ERROR_57); - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | ERR | Was not able to load the PROC from DLL. Bad Runtime."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Was not able to load the PROC from DLL. Bad Runtime."); libraryHandle.Dispose(); return false; } @@ -174,7 +174,7 @@ private bool LoadUserInstanceDll() } _sqlUserInstanceLibraryHandle = libraryHandle; - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.LoadUserInstanceDll | SNI | INFO | User Instance DLL was loaded successfully."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "User Instance DLL was loaded successfully."); return true; } } @@ -200,7 +200,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (key == null) { errorState = LocalDBErrorState.NO_INSTALLATION; - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | No installation found."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "No installation found."); return null; } @@ -215,7 +215,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (!Version.TryParse(subKey, out currentKeyVersion)) { errorState = LocalDBErrorState.INVALID_CONFIG; - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | Invalid Configuration."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Invalid Configuration."); return null; } @@ -229,7 +229,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (latestVersion.Equals(zeroVersion)) { errorState = LocalDBErrorState.INVALID_CONFIG; - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | Invalid Configuration."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Invalid Configuration."); return null; } @@ -242,7 +242,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (instanceAPIPathRegistryObject == null) { errorState = LocalDBErrorState.NO_SQLUSERINSTANCEDLL_PATH; - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | No SQL user instance DLL. Instance API Path Registry Object Error."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "No SQL user instance DLL. Instance API Path Registry Object Error."); return null; } @@ -251,7 +251,7 @@ private string GetUserInstanceDllPath(out LocalDBErrorState errorState) if (valueKind != RegistryValueKind.String) { errorState = LocalDBErrorState.INVALID_SQLUSERINSTANCEDLL_PATH; - SqlClientEventSource.Log.TrySNITraceEvent("LocalDB.Windows.GetUserInstanceDllPath | SNI | ERR | Invalid SQL user instance DLL path. Registry value kind mismatch."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Invalid SQL user instance DLL path. Registry value kind mismatch."); return null; } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs index f42ff57c7d..8ae171fc68 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNICommon.cs @@ -101,6 +101,8 @@ internal enum SNISMUXFlags internal class SNICommon { + private const string s_className = nameof(SNICommon); + // Each error number maps to SNI_ERROR_* in String.resx internal const int ConnTerminatedError = 2; internal const int InvalidParameterError = 5; @@ -141,7 +143,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, X509C { if (policyErrors == SslPolicyErrors.None) { - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | INFO | targetServerName {0}, SSL Server certificate not validated as PolicyErrors set to None.", targetServerName); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "targetServerName {0}, SSL Server certificate not validated as PolicyErrors set to None.", args0: targetServerName); return true; } @@ -152,7 +154,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, X509C // Verify that target server name matches subject in the certificate if (targetServerName.Length > certServerName.Length) { - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, Target Server name is of greater length than Subject in Certificate.", targetServerName); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "targetServerName {0}, Target Server name is of greater length than Subject in Certificate.", args0: targetServerName); return false; } else if (targetServerName.Length == certServerName.Length) @@ -160,7 +162,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, X509C // Both strings have the same length, so targetServerName must be a FQDN if (!targetServerName.Equals(certServerName, StringComparison.OrdinalIgnoreCase)) { - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, Target Server name does not match Subject in Certificate.", targetServerName); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "targetServerName {0}, Target Server name does not match Subject in Certificate.", args0: targetServerName); return false; } } @@ -168,7 +170,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, X509C { if (string.Compare(targetServerName, 0, certServerName, 0, targetServerName.Length, StringComparison.OrdinalIgnoreCase) != 0) { - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, Target Server name does not match Subject in Certificate.", targetServerName); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "targetServerName {0}, Target Server name does not match Subject in Certificate.", args0: targetServerName); return false; } @@ -178,7 +180,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, X509C // (Names have different lengths, so the target server can't be a FQDN.) if (certServerName[targetServerName.Length] != '.') { - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, Target Server name does not match Subject in Certificate.", targetServerName); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "targetServerName {0}, Target Server name does not match Subject in Certificate.", args0: targetServerName); return false; } } @@ -186,10 +188,10 @@ internal static bool ValidateSslServerCertificate(string targetServerName, X509C else { // Fail all other SslPolicy cases besides RemoteCertificateNameMismatch - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | ERR | targetServerName {0}, SslPolicyError {1}, SSL Policy invalidated certificate.", targetServerName, policyErrors); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "targetServerName {0}, SslPolicyError {1}, SSL Policy invalidated certificate.", args0: targetServerName, args1: policyErrors); return false; } - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ValidateSslServerCertificate | SNI | INFO | targetServerName {0}, Client certificate validated successfully.", targetServerName); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "targetServerName {0}, Client certificate validated successfully.", args0: targetServerName); return true; } finally @@ -208,7 +210,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, X509C /// internal static uint ReportSNIError(SNIProviders provider, uint nativeError, uint sniError, string errorMessage) { - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ReportSNIError | SNI | ERR | Provider = {0}, native Error = {1}, SNI Error = {2}, Error Message = {3}", provider, nativeError, sniError, errorMessage); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Provider = {0}, native Error = {1}, SNI Error = {2}, Error Message = {3}", args0: provider, args1: nativeError, args2: sniError, args3: errorMessage); return ReportSNIError(new SNIError(provider, nativeError, sniError, errorMessage)); } @@ -221,7 +223,7 @@ internal static uint ReportSNIError(SNIProviders provider, uint nativeError, uin /// internal static uint ReportSNIError(SNIProviders provider, uint sniError, Exception sniException) { - SqlClientEventSource.Log.TrySNITraceEvent("SNICommon.ReportSNIError | SNI | ERR | Provider = {0}, SNI Error = {1}, Exception = {2}", provider, sniError, sniException.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Provider = {0}, SNI Error = {1}, Exception = {2}", args0: provider, args1: sniError, args2: sniException?.Message); return ReportSNIError(new SNIError(provider, sniError, sniException)); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNILoadHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNILoadHandle.cs index bfada20446..1fa6c58a3f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNILoadHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNILoadHandle.cs @@ -30,7 +30,6 @@ public SNIError LastError set { - SqlClientEventSource.Log.TrySNITraceEvent("SNILoadHandle.LastError | SNI | INFO | SETTER | Last Error Value = {0}", value?.errorMessage); _lastError.Value = value; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs index badae43200..597172c37f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs @@ -46,7 +46,7 @@ public Guid ConnectionId public SNIMarsConnection(SNIHandle lowerHandle) { _lowerHandle = lowerHandle; - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "Created MARS Session Id {0}", args0: ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Created MARS Session Id {0}", args0: ConnectionId); _lowerHandle.SetAsyncCallbacks(HandleReceiveComplete, HandleSendComplete); } @@ -57,7 +57,7 @@ public SNIMarsHandle CreateMarsSession(object callbackObject, bool async) ushort sessionId = _nextSessionId++; SNIMarsHandle handle = new SNIMarsHandle(this, sessionId, callbackObject, async); _sessions.Add(sessionId, handle); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, SNI MARS Handle Id {1}, created new MARS Session {2}", args0: ConnectionId, args1: handle?.ConnectionId, args2: sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, SNI MARS Handle Id {1}, created new MARS Session {2}", args0: ConnectionId, args1: handle?.ConnectionId, args2: sessionId); return handle; } } @@ -75,10 +75,10 @@ public uint StartReceive() if (ReceiveAsync(ref packet) == TdsEnums.SNI_SUCCESS_IO_PENDING) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Success IO pending.", args0: ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Success IO pending.", args0: ConnectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Connection not usable.", args0: ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Connection not usable.", args0: ConnectionId); return SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnNotUsableError, Strings.SNI_ERROR_19); } finally @@ -144,7 +144,7 @@ public uint ReceiveAsync(ref SNIPacket packet) { ReturnPacket(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Packet {1} returned", args0: ConnectionId, args1: packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Packet {1} returned", args0: ConnectionId, args1: packet?._id); #endif packet = null; } @@ -153,7 +153,7 @@ public uint ReceiveAsync(ref SNIPacket packet) { var response = _lowerHandle.ReceiveAsync(ref packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Received new packet {1}", args0: ConnectionId, args1: packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Received new packet {1}", args0: ConnectionId, args1: packet?._id); #endif return response; } @@ -196,11 +196,11 @@ public void HandleReceiveError(SNIPacket packet) { handle.HandleReceiveError(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Packet {1} has Completion Callback", args0: ConnectionId, args1: packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Packet {1} has Completion Callback", args0: ConnectionId, args1: packet?._id); } else { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Packet {1} does not have Completion Callback, error not handled.", args0: ConnectionId, args1: packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Packet {1} does not have Completion Callback, error not handled.", args0: ConnectionId, args1: packet?._id); #endif } } @@ -237,7 +237,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) lock (this) { HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); return; } } @@ -260,7 +260,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (bytesTaken == 0) { sniErrorCode = ReceiveAsync(ref packet); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Non-SMUX Header SNI Packet received with code {1}", args0: ConnectionId, args1: sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Non-SMUX Header SNI Packet received with code {1}", args0: ConnectionId, args1: sniErrorCode); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { @@ -268,7 +268,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); return; } } @@ -277,7 +277,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) _dataBytesLeft = (int)_currentHeader.length; _currentPacket = _lowerHandle.RentPacket(headerSize: 0, dataSize: (int)_currentHeader.length); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _dataBytesLeft {1}, _currentPacket {2}, Reading data of length: _currentHeader.length {3}", args0: _lowerHandle?.ConnectionId, args1: _dataBytesLeft, args2: currentPacket?._id, args3: _currentHeader?.length); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _dataBytesLeft {1}, _currentPacket {2}, Reading data of length: _currentHeader.length {3}", args0: _lowerHandle?.ConnectionId, args1: _dataBytesLeft, args2: currentPacket?._id, args3: _currentHeader?.length); #endif } @@ -294,7 +294,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (_dataBytesLeft > 0) { sniErrorCode = ReceiveAsync(ref packet); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, SMUX DATA Header SNI Packet received with code {1}, _dataBytesLeft {2}", args0: ConnectionId, args1: sniErrorCode, args2: _dataBytesLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, SMUX DATA Header SNI Packet received with code {1}, _dataBytesLeft {2}", args0: ConnectionId, args1: sniErrorCode, args2: _dataBytesLeft); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { @@ -302,7 +302,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Handled receive error code: {1}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); return; } } @@ -314,7 +314,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.InvalidParameterError, Strings.SNI_ERROR_5); HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "Current Header Session Id {0} not found, MARS Session Id {1} will be destroyed, New SNI error created: {2}", args0: _currentHeader?.sessionId, args1: _lowerHandle?.ConnectionId, args2: sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Current Header Session Id {0} not found, MARS Session Id {1} will be destroyed, New SNI error created: {2}", args0: _currentHeader?.sessionId, args1: _lowerHandle?.ConnectionId, args2: sniErrorCode); _lowerHandle.Dispose(); _lowerHandle = null; return; @@ -323,19 +323,19 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_FIN) { _sessions.Remove(_currentHeader.sessionId); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | FIN | ", "MARS Session Id {0}, SMUX_FIN flag received, Current Header Session Id {1} removed", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "SMUX_FIN | MARS Session Id {0}, SMUX_FIN flag received, Current Header Session Id {1} removed", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); } else { currentSession = _sessions[_currentHeader.sessionId]; - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Current Session assigned to Session Id {1}", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Current Session assigned to Session Id {1}", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); } } if (currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { currentSession.HandleReceiveComplete(currentPacket, currentHeader); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | SMUX_DATA | ", "MARS Session Id {0}, Current Session {1} completed receiving Data", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "SMUX_DATA | MARS Session Id {0}, Current Session {1} completed receiving Data", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); } if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_ACK) @@ -343,16 +343,16 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) try { currentSession.HandleAck(currentHeader.highwater); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | SMUX_ACK | ", "MARS Session Id {0}, Current Session {1} handled ack", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "SMUX_ACK | MARS Session Id {0}, Current Session {1} handled ack", args0: _lowerHandle?.ConnectionId, args1: _currentHeader?.sessionId); } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Exception occurred: {2}", args0: _currentHeader?.sessionId, args1: e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Exception occurred: {2}", args0: _currentHeader?.sessionId, args1: e?.Message); SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } #if DEBUG Debug.Assert(_currentPacket == currentPacket, "current and _current are not the same"); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | SMUX_ACK | ", "MARS Session Id {0}, Current Packet {1} returned", args0: _lowerHandle?.ConnectionId, args1: currentPacket?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "SMUX_ACK | MARS Session Id {0}, Current Packet {1} returned", args0: _lowerHandle?.ConnectionId, args1: currentPacket?._id); #endif ReturnPacket(currentPacket); currentPacket = null; @@ -371,7 +371,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } HandleReceiveError(packet); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, packet.DataLeft 0, SNI error {2}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, packet.DataLeft 0, SNI error {2}", args0: _lowerHandle?.ConnectionId, args1: sniErrorCode); return; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs index 7671cb33c1..1c1523d73e 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsHandle.cs @@ -59,11 +59,11 @@ public override void Dispose() try { SendControlPacket(SNISMUXFlags.SMUX_FIN); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Sent SMUX_FIN packet to terminate session.", args0: ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Sent SMUX_FIN packet to terminate session.", args0: ConnectionId); } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, Internal exception error = {1}, Member Name={2}", args0: ConnectionId, args1: e?.Message, args2: e?.GetType()?.Name); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Internal exception error = {1}, Member Name={2}", args0: ConnectionId, args1: e?.Message, args2: e?.GetType()?.Name); SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } finally @@ -87,7 +87,7 @@ public SNIMarsHandle(SNIMarsConnection connection, ushort sessionId, object call _callbackObject = callbackObject; _handleSendCompleteCallback = HandleSendComplete; SendControlPacket(SNISMUXFlags.SMUX_SYN); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Sent SMUX_SYN packet to start a new session, session Id {1}", args0: ConnectionId, args1: _sessionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Sent SMUX_SYN packet to start a new session, session Id {1}", args0: ConnectionId, args1: _sessionId); _status = TdsEnums.SNI_SUCCESS; } @@ -102,7 +102,7 @@ private void SendControlPacket(SNISMUXFlags flags) { SNIPacket packet = RentPacket(headerSize: SNISMUXHeader.HEADER_LENGTH, dataSize: 0); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Packet rented {1}, packet dataLeft {2}", args0: ConnectionId, args1: packet?._id, args2: packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Packet rented {1}, packet dataLeft {2}", args0: ConnectionId, args1: packet?._id, args2: packet?.DataLeft); #endif lock (this) { @@ -114,7 +114,7 @@ private void SendControlPacket(SNISMUXFlags flags) _connection.Send(packet); ReturnPacket(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Packet returned {1}, packet dataLeft {2}", args0: ConnectionId, args1: packet?._id, args2: packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Packet returned {1}, packet dataLeft {2}", args0: ConnectionId, args1: packet?._id, args2: packet?.DataLeft); ; #endif } @@ -150,7 +150,7 @@ private SNIPacket SetPacketSMUXHeader(SNIPacket packet) _currentHeader.Write(packet.GetHeaderBuffer(SNISMUXHeader.HEADER_LENGTH)); packet.SetHeaderActive(); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Setting SMUX_DATA header in current header for packet {1}", args0: ConnectionId, args1: packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Setting SMUX_DATA header in current header for packet {1}", args0: ConnectionId, args1: packet?._id); #endif return packet; } @@ -176,12 +176,12 @@ public override uint Send(SNIPacket packet) } } - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for Acknowledgment event.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for Acknowledgment event.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); _ackEvent.Wait(); lock (this) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sendPacketQueue count found {1}, Acknowledgment event Reset", args0: ConnectionId, args1: _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sendPacketQueue count found {1}, Acknowledgment event Reset", args0: ConnectionId, args1: _sendPacketQueue?.Count); _ackEvent.Reset(); } } @@ -191,7 +191,7 @@ public override uint Send(SNIPacket packet) { muxedPacket = SetPacketSMUXHeader(packet); } - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SMUX Packet is going to be sent.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SMUX Packet is going to be sent.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); return _connection.Send(muxedPacket); } finally @@ -216,13 +216,13 @@ private uint InternalSendAsync(SNIPacket packet, SNIAsyncCallback callback) { if (_sequenceNumber >= _sendHighwater) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SNI Queue is full", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, SNI Queue is full", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); return TdsEnums.SNI_QUEUE_FULL; } SNIPacket muxedPacket = SetPacketSMUXHeader(packet); muxedPacket.SetCompletionCallback(callback ?? HandleSendComplete); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Sending packet", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Sending packet", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); return _connection.SendAsync(muxedPacket, callback); } } @@ -255,18 +255,18 @@ private uint SendPendingPackets() if (result != TdsEnums.SNI_SUCCESS && result != TdsEnums.SNI_SUCCESS_IO_PENDING) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, InternalSendAsync result is not SNI_SUCCESS and is not SNI_SUCCESS_IO_PENDING", args0: ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, InternalSendAsync result is not SNI_SUCCESS and is not SNI_SUCCESS_IO_PENDING", args0: ConnectionId); return result; } _sendPacketQueue.Dequeue(); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sendPacketQueue dequeued, count {1}", args0: ConnectionId, args1: _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sendPacketQueue dequeued, count {1}", args0: ConnectionId, args1: _sendPacketQueue?.Count); continue; } else { _ackEvent.Set(); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sendPacketQueue count found {1}, acknowledgment set", args0: ConnectionId, args1: _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sendPacketQueue count found {1}, acknowledgment set", args0: ConnectionId, args1: _sendPacketQueue?.Count); } } @@ -299,7 +299,7 @@ public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = nul } SendPendingPackets(); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sendPacketQueue enqueued, count {1}", args0: ConnectionId, args1: _sendPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sendPacketQueue enqueued, count {1}", args0: ConnectionId, args1: _sendPacketQueue?.Count); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -325,14 +325,14 @@ public override uint ReceiveAsync(ref SNIPacket packet) if (_connectionError != null) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, _connectionError {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: _connectionError); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, _connectionError {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: _connectionError); return SNICommon.ReportSNIError(_connectionError); } if (queueCount == 0) { _asyncReceives++; - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, queueCount 0, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, queueCount 0, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -342,7 +342,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) if (queueCount == 1) { #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, packet dequeued {1}, packet Owner {2}, packet refCount {3}, received Packet Queue count {4}", args0: ConnectionId, args1: packet?._id, args2: packet?._owner, args3: packet?._refCount, args4: _receivedPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, packet dequeued {1}, packet Owner {2}, packet refCount {3}, received Packet Queue count {4}", args0: ConnectionId, args1: packet?._id, args2: packet?._owner, args3: packet?._refCount, args4: _receivedPacketQueue?.Count); #endif _packetEvent.Reset(); } @@ -353,7 +353,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) _receiveHighwater++; } - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, queueCount {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: _receivedPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}, queueCount {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: _receivedPacketQueue?.Count); SendAckIfNecessary(); return TdsEnums.SNI_SUCCESS; } @@ -378,7 +378,7 @@ public void HandleReceiveError(SNIPacket packet) lock (_receivedPacketQueue) { _connectionError = SNILoadHandle.SingletonInstance.LastError; - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, _connectionError to be handled: {1}", args0: ConnectionId, args1: _connectionError); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, _connectionError to be handled: {1}", args0: ConnectionId, args1: _connectionError); _packetEvent.Set(); } @@ -408,7 +408,7 @@ public void HandleSendComplete(SNIPacket packet, uint sniErrorCode) } _connection.ReturnPacket(packet); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Returned Packet: {1}", args0: ConnectionId, args1: packet?._id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Returned Packet: {1}", args0: ConnectionId, args1: packet?._id); #endif } finally @@ -430,7 +430,7 @@ public void HandleAck(uint highwater) { if (_sendHighwater != highwater) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, Setting _sendHighwater {1} to highwater {2} and send pending packets.", args0: ConnectionId, args1: _sendHighwater, args2: highwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, Setting _sendHighwater {1} to highwater {2} and send pending packets.", args0: ConnectionId, args1: _sendHighwater, args2: highwater); _sendHighwater = highwater; SendPendingPackets(); } @@ -456,7 +456,7 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { if (_sendHighwater != header.highwater) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, header.highwater {1}, _sendHighwater {2}, Handle Ack with header.highwater", args0: ConnectionId, args1: header?.highwater, args2: _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, header.highwater {1}, _sendHighwater {2}, Handle Ack with header.highwater", args0: ConnectionId, args1: header?.highwater, args2: _sendHighwater); HandleAck(header.highwater); } @@ -466,13 +466,13 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { _receivedPacketQueue.Enqueue(packet); _packetEvent.Set(); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _receivedPacketQueue count {3}, packet event set", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _receivedPacketQueue?.Count); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _receivedPacketQueue count {3}, packet event set", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _receivedPacketQueue?.Count); return; } _asyncReceives--; Debug.Assert(_callbackObject != null); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _asyncReceives {3}", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _asyncReceives); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _asyncReceives {3}", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _asyncReceives); ((TdsParserStateObject)_callbackObject).ReadAsyncCallback(PacketHandle.FromManagedPacket(packet), 0); } @@ -484,7 +484,7 @@ public void HandleReceiveComplete(SNIPacket packet, SNISMUXHeader header) { _receiveHighwater++; } - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck); SendAckIfNecessary(); } finally @@ -510,7 +510,7 @@ private void SendAckIfNecessary() if (receiveHighwater - receiveHighwaterLastAck > ACK_THRESHOLD) { SendControlPacket(SNISMUXFlags.SMUX_ACK); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} Sending acknowledgment ACK_THRESHOLD {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: ACK_THRESHOLD); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _asyncReceives {1}, _receiveHighwater {2}, _sendHighwater {3}, _receiveHighwaterLastAck {4} Sending acknowledgment ACK_THRESHOLD {5}", args0: ConnectionId, args1: _asyncReceives, args2: _receiveHighwater, args3: _sendHighwater, args4: _receiveHighwaterLastAck, args5: ACK_THRESHOLD); } } @@ -535,12 +535,12 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) { if (_connectionError != null) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | ERR | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _connectionError found: {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _connectionError); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _connectionError found: {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: _connectionError); return SNICommon.ReportSNIError(_connectionError); } queueCount = _receivedPacketQueue.Count; - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, W_receivedPacketQueue count {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: queueCount); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, W_receivedPacketQueue count {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: queueCount); if (queueCount > 0) { @@ -549,7 +549,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) if (queueCount == 1) { _packetEvent.Reset(); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, packet event reset, _receivedPacketQueue count 1.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, packet event reset, _receivedPacketQueue count 1.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); } result = TdsEnums.SNI_SUCCESS; @@ -564,15 +564,15 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) } SendAckIfNecessary(); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, returning with result {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: result); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, returning with result {3}.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater, args3: result); return result; } - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for packet event.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for packet event.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); if (!_packetEvent.Wait(timeoutInMilliseconds)) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11); - SqlClientEventSource.Log.TrySNITraceEvent(s_className, " | SNI | INFO | ", "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _packetEvent wait timed out.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _packetEvent wait timed out.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater); return TdsEnums.SNI_WAIT_TIMEOUT; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs index ce3f5e698c..4571dd470b 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs @@ -18,6 +18,7 @@ namespace Microsoft.Data.SqlClient.SNI /// internal sealed class SNINpHandle : SNIPhysicalHandle { + private const string s_className = nameof(SNINpHandle); internal const string DefaultPipePath = @"sql\query"; // e.g. \\HOSTNAME\pipe\sql\query // private const int MAX_PIPE_INSTANCES = 255; // TODO: Investigate pipe instance limit. @@ -39,8 +40,8 @@ internal sealed class SNINpHandle : SNIPhysicalHandle public SNINpHandle(string serverName, string pipeName, long timerExpire) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.ctor | SNI | INFO | SCOPE | Entering Scope {0}"); - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ctor | SNI | INFO | Connection Id {0}, Setting server name = {1}, pipe name = {2}", _connectionId, serverName, pipeName); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Setting server name = {1}, pipe name = {2}", args0: _connectionId, args1: serverName, args2: pipeName); try { _sendSync = new object(); @@ -71,14 +72,14 @@ public SNINpHandle(string serverName, string pipeName, long timerExpire) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.ConnOpenFailedError, te); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ctor | SNI | ERR | Connection Id {0}, Connection Timed out. Error Code 1 Exception = {1}", _connectionId, te?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Connection Timed out. Error Code 1 Exception = {1}", args0: _connectionId, args1: te?.Message); return; } catch (IOException ioe) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.ConnOpenFailedError, ioe); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ctor | SNI | ERR | Connection Id {0}, IO Exception occurred. Error Code 1 Exception = {1}", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IO Exception occurred. Error Code 1 Exception = {1}", args0: _connectionId, args1: ioe?.Message); return; } @@ -86,7 +87,7 @@ public SNINpHandle(string serverName, string pipeName, long timerExpire) { SNICommon.ReportSNIError(SNIProviders.NP_PROV, 0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); _status = TdsEnums.SNI_ERROR; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ctor | SNI | ERR | Connection Id {0}, Pipe Stream not operational. Error Code 1 Exception = {1}", _connectionId, Strings.SNI_ERROR_1); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Pipe Stream not operational. Error Code 1 Exception = {1}", args0: _connectionId, args1: Strings.SNI_ERROR_1); return; } @@ -135,17 +136,17 @@ public override int ProtocolVersion public override uint CheckConnection() { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.CheckConnection | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (!_stream.CanWrite || !_stream.CanRead) { - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.CheckConnection | SNI | ERR | Connection Id {0}, Cannot write or read to/from the stream", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Cannot write or read to/from the stream", args0: _connectionId); return TdsEnums.SNI_ERROR; } else { - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.CheckConnection | SNI | INFO | Connection Id {0}, Can read and write to/from stream.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Can read and write to/from stream.", args0: _connectionId); return TdsEnums.SNI_SUCCESS; } } @@ -179,13 +180,13 @@ public override void Dispose() //Release any references held by _stream. _stream = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Dispose | SNI | INFO | Connection Id {0}, All streams disposed and references cleared.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, All streams disposed and references cleared.", args0: _connectionId); } } public override uint Receive(out SNIPacket packet, int timeout) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.Receive | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { SNIPacket errorPacket; @@ -196,14 +197,14 @@ public override uint Receive(out SNIPacket packet, int timeout) { packet = RentPacket(headerSize: 0, dataSize: _bufferSize); packet.ReadFromStream(_stream); - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Receive | SNI | INFO | Connection Id {0}, Rented and read packet, dataLeft {1}", _connectionId, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Rented and read packet, dataLeft {1}", args0: _connectionId, args1: packet?.DataLeft); if (packet.Length == 0) { errorPacket = packet; packet = null; var e = new Win32Exception(); - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Receive | SNI | ERR | Connection Id {0}, Packet length found 0, Win32 exception raised: {1}", _connectionId, e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Packet length found 0, Win32 exception raised: {1}", args0: _connectionId, args1: e?.Message); return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message); } } @@ -211,14 +212,14 @@ public override uint Receive(out SNIPacket packet, int timeout) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, ObjectDisposedException occurred: {1}.", args0: _connectionId, args1: ode?.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (IOException ioe) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Receive | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IOException occurred: {1}.", args0: _connectionId, args1: ioe?.Message); return ReportErrorAndReleasePacket(errorPacket, ioe); } return TdsEnums.SNI_SUCCESS; @@ -232,7 +233,7 @@ public override uint Receive(out SNIPacket packet, int timeout) public override uint ReceiveAsync(ref SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.ReceiveAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { SNIPacket errorPacket; @@ -241,21 +242,21 @@ public override uint ReceiveAsync(ref SNIPacket packet) try { packet.ReadFromStreamAsync(_stream, _receiveCallback); - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Rented and read packet asynchronously, dataLeft {1}", _connectionId, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Rented and read packet asynchronously, dataLeft {1}", args0: _connectionId, args1: packet?.DataLeft); return TdsEnums.SNI_SUCCESS_IO_PENDING; } catch (ObjectDisposedException ode) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReceiveAsync | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, ObjectDisposedException occurred: {1}.", args0: _connectionId, args1: ode?.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (IOException ioe) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReceiveAsync | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IOException occurred: {1}.", args0: _connectionId, args1: ioe?.Message); return ReportErrorAndReleasePacket(errorPacket, ioe); } } @@ -267,7 +268,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) public override uint Send(SNIPacket packet) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.Send | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { bool releaseLock = false; @@ -294,18 +295,18 @@ public override uint Send(SNIPacket packet) { try { - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Send | SNI | INFO | Connection Id {0}, Packet writing to stream, dataLeft {1}", _connectionId, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet writing to stream, dataLeft {1}", args0: _connectionId, args1: packet?.DataLeft); packet.WriteToStream(_stream); return TdsEnums.SNI_SUCCESS; } catch (ObjectDisposedException ode) { - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Send | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}.", _connectionId, ode?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, ObjectDisposedException occurred: {1}.", args0: _connectionId, args1: ode?.Message); return ReportErrorAndReleasePacket(packet, ode); } catch (IOException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.Send | SNI | ERR | Connection Id {0}, IOException occurred: {1}.", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IOException occurred: {1}.", args0: _connectionId, args1: ioe?.Message); return ReportErrorAndReleasePacket(packet, ioe); } } @@ -326,11 +327,11 @@ public override uint Send(SNIPacket packet) public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { SNIAsyncCallback cb = callback ?? _sendCallback; - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.SendAsync | SNI | INFO | Connection Id {0}, Packet writing to stream, dataLeft {1}", _connectionId, packet?.DataLeft); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet writing to stream, dataLeft {1}", args0: _connectionId, args1: packet?.DataLeft); packet.WriteToStreamAsync(_stream, cb, SNIProviders.NP_PROV); return TdsEnums.SNI_SUCCESS_IO_PENDING; } @@ -348,7 +349,7 @@ public override void SetAsyncCallbacks(SNIAsyncCallback receiveCallback, SNIAsyn public override uint EnableSsl(uint options) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.EnableSsl | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { _validateCert = (options & TdsEnums.SNI_SSL_VALIDATE_CERTIFICATE) != 0; @@ -360,12 +361,12 @@ public override uint EnableSsl(uint options) } catch (AuthenticationException aue) { - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.EnableSsl | SNI | ERR | Connection Id {0}, AuthenticationException message = {1}.", ConnectionId, aue.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, AuthenticationException message = {1}.", args0: ConnectionId, args1: aue?.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, aue); } catch (InvalidOperationException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.EnableSsl | SNI | ERR | Connection Id {0}, InvalidOperationException message = {1}.", ConnectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, InvalidOperationException message = {1}.", args0: ConnectionId, args1: ioe?.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, ioe); } _stream = _sslStream; @@ -397,16 +398,16 @@ public override void DisableSsl() /// true if valid private bool ValidateServerCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNINpHandle.ValidateServerCertificate | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (!_validateCert) { - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate validation not requested.", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Certificate validation not requested.", args0: ConnectionId); return true; } - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Proceeding to SSL certificate validation.", ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Proceeding to SSL certificate validation.", args0: ConnectionId); return SNICommon.ValidateSslServerCertificate(_targetServer, cert, policyErrors); } finally @@ -430,7 +431,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, Exception sniExceptio { ReturnPacket(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1}", ConnectionId, sniException?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet returned, error occurred: {1}", args0: ConnectionId, args1: sniException?.Message); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, SNICommon.InternalExceptionError, sniException); } @@ -440,7 +441,7 @@ private uint ReportErrorAndReleasePacket(SNIPacket packet, uint nativeError, uin { ReturnPacket(packet); } - SqlClientEventSource.Log.TrySNITraceEvent("SNINpHandle.ReportErrorAndReleasePacket | SNI | INFO | Connection Id {0}, Packet returned, error occurred: {1}", ConnectionId, errorMessage); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet returned, error occurred: {1}", args0: ConnectionId, args1: errorMessage); return SNICommon.ReportSNIError(SNIProviders.NP_PROV, nativeError, sniError, errorMessage); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs index ccaefb5958..7fe953d8d8 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs @@ -19,6 +19,7 @@ namespace Microsoft.Data.SqlClient.SNI /// internal sealed class SNIPacket { + private const string s_className = nameof(SNIPacket); private int _dataLength; // the length of the data in the data segment, advanced by Append-ing data, does not include smux header length private int _dataCapacity; // the total capacity requested, if the array is rented this may be less than the _data.Length, does not include smux header length private int _dataOffset; // the start point of the data in the data segment, advanced by Take-ing data @@ -68,7 +69,7 @@ public SNIPacket(SNIHandle owner, int id) #endif _id = id; _owner = owner; - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ctor | SNI | INFO | Connection Id {0}, Packet Id {1} instantiated,", _owner?.ConnectionId, _id); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} instantiated,", args0: _owner?.ConnectionId, args1: _id); } // the finalizer is only included in debug builds and is used to ensure that all packets are correctly recycled @@ -78,7 +79,7 @@ public SNIPacket(SNIHandle owner, int id) { if (_data != null) { - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.dtor | SNI | ERR | Finalizer called for unreleased SNIPacket, Connection Id {0}, Packet Id {1}, _refCount {2}, DataLeft {3}, tag {4}", _owner?.ConnectionId, _id, _refCount, DataLeft, _traceTag); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Finalizer called for unreleased SNIPacket, Connection Id {0}, Packet Id {1}, _refCount {2}, DataLeft {3}, tag {4}", args0: _owner?.ConnectionId, args1: _id, args2: _refCount, args3: DataLeft, args4: _traceTag); } } @@ -143,7 +144,7 @@ public void Allocate(int headerLength, int dataLength) _dataOffset = 0; _headerLength = headerLength; #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.Allocate | SNI | INFO | Connection Id {0}, Packet Id {1} allocated with _headerLength {2}, _dataCapacity {3}", _owner?.ConnectionId, _id, _headerLength, _dataCapacity); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} allocated with _headerLength {2}, _dataCapacity {3}", args0: _owner?.ConnectionId, args1: _id, args2: _headerLength, args3: _dataCapacity); #endif } @@ -169,7 +170,7 @@ public int TakeData(SNIPacket packet, int size) int dataSize = TakeData(packet._data, packet._headerLength + packet._dataLength, size); packet._dataLength += dataSize; #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.TakeData | SNI | INFO | Connection Id {0}, Packet Id {1} took data from Packet Id {2} dataSize {3}, _dataLength {4}", _owner?.ConnectionId, _id, packet?._id, dataSize, packet._dataLength); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} took data from Packet Id {2} dataSize {3}, _dataLength {4}", args0: _owner?.ConnectionId, args1: _id, args2: packet?._id, args3: dataSize, args4: packet._dataLength); #endif return dataSize; } @@ -184,7 +185,7 @@ public void AppendData(byte[] data, int size) Buffer.BlockCopy(data, 0, _data, _headerLength + _dataLength, size); _dataLength += size; #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.AppendData | SNI | INFO | Connection Id {0}, Packet Id {1} was appended with size {2}, _dataLength {3}", _owner?.ConnectionId, _id, size, _dataLength); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} was appended with size {2}, _dataLength {3}", args0: _owner?.ConnectionId, args1: _id, args2: size, args3: _dataLength); #endif } @@ -210,7 +211,7 @@ public int TakeData(byte[] buffer, int dataOffset, int size) Buffer.BlockCopy(_data, _headerLength + _dataOffset, buffer, dataOffset, size); _dataOffset += size; #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.TakeData | SNI | INFO | Connection Id {0}, Packet Id {1} took data size {2}, _dataLength {3}, _dataOffset {4}", _owner?.ConnectionId, _id, size, _dataLength, _dataOffset); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} took data size {2}, _dataLength {3}, _dataOffset {4}", args0: _owner?.ConnectionId, args1: _id, args2: size, args3: _dataLength, args4: _dataOffset); #endif return size; } @@ -230,7 +231,7 @@ public void SetHeaderActive() _dataLength += _headerLength; _headerLength = 0; #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.SetHeaderActive | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} header set to active.", _owner?.ConnectionId, _id, _dataLength); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} _dataLength {2} header set to active.", args0: _owner?.ConnectionId, args1: _id, args2: _dataLength); #endif } @@ -248,7 +249,7 @@ public void Release() _dataCapacity = 0; } #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.Release | SNI | INFO | Connection Id {0}, Packet Id {1} _headerLength {2} and _dataLength {3} released.", _owner?.ConnectionId, _id, _headerLength, _dataLength); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} _headerLength {2} and _dataLength {3} released.", args0: _owner?.ConnectionId, args1: _id, args2: _headerLength, args3: _dataLength); #endif _dataLength = 0; _dataOffset = 0; @@ -265,7 +266,7 @@ public void ReadFromStream(Stream stream) { _dataLength = stream.Read(_data, _headerLength, _dataCapacity); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ReadFromStream | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} read from stream.", _owner?.ConnectionId, _id, _dataLength); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} _dataLength {2} read from stream.", args0: _owner?.ConnectionId, args1: _id, args2: _dataLength); #endif } @@ -295,7 +296,7 @@ private void ReadFromStreamAsyncContinuation(Task t, object state) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, SNICommon.InternalExceptionError, e); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ReadFromStreamAsyncContinuation | SNI | ERR | Connection Id {0}, Internal Exception occurred while reading data: {1}", _owner?.ConnectionId, e.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Internal Exception occurred while reading data: {1}", args0: _owner?.ConnectionId, args1: e?.Message); #endif error = true; } @@ -303,13 +304,13 @@ private void ReadFromStreamAsyncContinuation(Task t, object state) { _dataLength = t.Result; #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ReadFromStreamAsyncContinuation | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} read from stream.", _owner?.ConnectionId, _id, _dataLength); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} _dataLength {2} read from stream.", args0: _owner?.ConnectionId, args1: _id, args2: _dataLength); #endif if (_dataLength == 0) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, SNICommon.ConnTerminatedError, Strings.SNI_ERROR_2); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.ReadFromStreamAsyncContinuation | SNI | ERR | Connection Id {0}, No data read from stream, connection was terminated.", _owner?.ConnectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, No data read from stream, connection was terminated.", args0: _owner?.ConnectionId); #endif error = true; } @@ -326,7 +327,7 @@ public void WriteToStream(Stream stream) { stream.Write(_data, _headerLength, _dataLength); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.WriteToStream | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} written to stream.", _owner?.ConnectionId, _id, _dataLength); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} _dataLength {2} written to stream.", args0: _owner?.ConnectionId, args1: _id, args2: _dataLength); #endif } @@ -343,14 +344,14 @@ public async void WriteToStreamAsync(Stream stream, SNIAsyncCallback callback, S { await stream.WriteAsync(_data, 0, _dataLength, CancellationToken.None).ConfigureAwait(false); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.WriteToStreamAsync | SNI | INFO | Connection Id {0}, Packet Id {1} _dataLength {2} written to stream.", _owner?.ConnectionId, _id, _dataLength); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Packet Id {1} _dataLength {2} written to stream.", args0: _owner?.ConnectionId, args1: _id, args2: _dataLength); #endif } catch (Exception e) { SNILoadHandle.SingletonInstance.LastError = new SNIError(provider, SNICommon.InternalExceptionError, e); #if DEBUG - SqlClientEventSource.Log.TrySNITraceEvent("SNIPacket.WriteToStreamAsync | SNI | ERR | Connection Id {0}, Internal Exception occurred while writing data: {1}", _owner?.ConnectionId, e.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Internal Exception occurred while writing data: {1}", args0: _owner?.ConnectionId, args1: e?.Message); #endif status = TdsEnums.SNI_ERROR; } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs index 1e36db4199..98ed3f222b 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs @@ -22,6 +22,7 @@ namespace Microsoft.Data.SqlClient.SNI /// internal sealed class SNITCPHandle : SNIPhysicalHandle { + private static string s_className = nameof(SNITCPHandle); private readonly string _targetServer; private readonly object _sendSync; private readonly Socket _socket; @@ -67,7 +68,7 @@ public override void Dispose() //Release any references held by _stream. _stream = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Dispose | SNI | INFO | Connection Id {0}, All streams disposed.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, All streams disposed.", args0: _connectionId); } } @@ -119,8 +120,8 @@ public override int ProtocolVersion /// Used for DNS Cache public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel, string cachedFQDN, ref SQLDNSInfo pendingDNSInfo) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNITCPHandle.ctor | SNI | INFO | SCOPE | Entering Scope {0}"); - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Setting server name = {1}", _connectionId, serverName); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Setting server name = {1}", args0: _connectionId, args1: serverName); try { _targetServer = serverName; @@ -144,7 +145,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel bool reportError = true; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Connecting to serverName {1} and port {2}", _connectionId, serverName, port); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Connecting to serverName {1} and port {2}", args0: _connectionId, args1: serverName, args2: port); // We will always first try to connect with serverName as before and let the DNS server to resolve the serverName. // If the DSN resolution fails, we will try with IPs in the DNS cache if existed. We try with IPv4 first and followed by IPv6 if // IPv4 fails. The exceptions will be throw to upper level and be handled as before. @@ -166,13 +167,13 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel { if (hasCachedDNSInfo == false) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0}, Cached DNS Info not found, exception occurred thrown: {1}", _connectionId, ex?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Cached DNS Info not found, exception occurred thrown: {1}", args0: _connectionId, args1: ex?.Message); throw; } else { int portRetry = string.IsNullOrEmpty(cachedDNSInfo.Port) ? port : int.Parse(cachedDNSInfo.Port); - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Retrying with cached DNS IP Address {1} and port {2}", _connectionId, cachedDNSInfo.AddrIPv4, cachedDNSInfo.Port); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Retrying with cached DNS IP Address {1} and port {2}", args0: _connectionId, args1: cachedDNSInfo.AddrIPv4, args2: cachedDNSInfo.Port); try { @@ -190,7 +191,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel if (exRetry is SocketException || exRetry is ArgumentNullException || exRetry is ArgumentException || exRetry is ArgumentOutOfRangeException || exRetry is AggregateException) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0}, Retrying exception {1}", _connectionId, exRetry?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Retrying exception {1}", args0: _connectionId, args1: exRetry?.Message); if (parallel) { _socket = TryConnectParallel(cachedDNSInfo.AddrIPv6, portRetry, ts, isInfiniteTimeOut, ref reportError, cachedFQDN, ref pendingDNSInfo); @@ -202,7 +203,7 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel } else { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0}, Retry failed, exception occurred: {1}", _connectionId, exRetry?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Retry failed, exception occurred: {1}", args0: _connectionId, args1: exRetry?.Message); throw; } } @@ -224,10 +225,10 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel if (reportError) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} could not be opened, exception occurred: {1}", _connectionId, Strings.SNI_ERROR_40); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0} could not be opened, exception occurred: {1}", args0: _connectionId, args1: Strings.SNI_ERROR_40); ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); } - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket could not be opened.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0} Socket could not be opened.", args0: _connectionId); return; } @@ -239,20 +240,20 @@ public SNITCPHandle(string serverName, int port, long timerExpire, bool parallel } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Socket exception occurred: Error Code {1}, Message {2}", _connectionId, se?.SocketErrorCode, se?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0} Socket exception occurred: Error Code {1}, Message {2}", args0: _connectionId, args1: se?.SocketErrorCode, args2: se?.Message); ReportTcpSNIError(se); return; } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | ERR | Connection Id {0} Exception occurred: {1}", _connectionId, e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0} Exception occurred: {1}", args0: _connectionId, args1: e?.Message); ReportTcpSNIError(e); return; } _stream = _tcpStream; _status = TdsEnums.SNI_SUCCESS; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ctor | SNI | INFO | Connection Id {0} Socket opened successfully, TCP stream ready.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0} Socket opened successfully, TCP stream ready.", args0: _connectionId); } finally { @@ -276,7 +277,7 @@ private Socket TryConnectParallel(string hostName, int port, TimeSpan ts, bool i { // Fail if above 64 to match legacy behavior callerReportError = false; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.TryConnectParallel | SNI | ERR | Connection Id {0} serverAddresses.Length {1} Exception: {2}", _connectionId, serverAddresses.Length, Strings.SNI_ERROR_47); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0} serverAddresses.Length {1} Exception: {2}", args0: _connectionId, args1: serverAddresses.Length, args2: Strings.SNI_ERROR_47); ReportTcpSNIError(0, SNICommon.MultiSubnetFailoverWithMoreThan64IPs, Strings.SNI_ERROR_47); return availableSocket; } @@ -306,7 +307,7 @@ private Socket TryConnectParallel(string hostName, int port, TimeSpan ts, bool i if (!(isInfiniteTimeOut ? connectTask.Wait(-1) : connectTask.Wait(ts))) { callerReportError = false; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.TryConnectParallel | SNI | ERR | Connection Id {0} Connection timed out, Exception: {1}", _connectionId, Strings.SNI_ERROR_40); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0} Connection timed out, Exception: {1}", args0: _connectionId, args1: Strings.SNI_ERROR_40); ReportTcpSNIError(0, SNICommon.ConnOpenFailedError, Strings.SNI_ERROR_40); return availableSocket; } @@ -365,7 +366,7 @@ void Cancel() } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Connect | SNI | ERR | THIS EXCEPTION IS BEING SWALLOWED: {0}", e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "THIS EXCEPTION IS BEING SWALLOWED: {0}", args0: e?.Message); } } } @@ -390,7 +391,7 @@ void Cancel() // enable keep-alive on socket SetKeepAliveValues(ref sockets[i]); - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Connect | SNI | INFO | Connecting to IP address {0} and port {1}", ipAddresses[i], port); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connecting to IP address {0} and port {1}", args0: ipAddresses[i], args1: port); sockets[i].Connect(ipAddresses[i], port); if (sockets[i] != null) // sockets[i] can be null if cancel callback is executed during connect() { @@ -409,7 +410,7 @@ void Cancel() } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Connect | SNI | ERR | THIS EXCEPTION IS BEING SWALLOWED: {0}", e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "THIS EXCEPTION IS BEING SWALLOWED: {0}", args0: e?.Message); } } } @@ -537,17 +538,17 @@ public override uint EnableSsl(uint options) } catch (AuthenticationException aue) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Authentication exception occurred: {1}", _connectionId, aue?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Authentication exception occurred: {1}", args0: _connectionId, args1: aue?.Message); return ReportTcpSNIError(aue); } catch (InvalidOperationException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | ERR | Connection Id {0}, Invalid Operation Exception occurred: {1}", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Invalid Operation Exception occurred: {1}", args0: _connectionId, args1: ioe?.Message); return ReportTcpSNIError(ioe); } _stream = _sslStream; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.EnableSsl | SNI | INFO | Connection Id {0}, SSL enabled successfully.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, SSL enabled successfully.", args0: _connectionId); return TdsEnums.SNI_SUCCESS; } @@ -561,7 +562,7 @@ public override void DisableSsl() _sslOverTdsStream.Dispose(); _sslOverTdsStream = null; _stream = _tcpStream; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.DisableSsl | SNI | INFO | Connection Id {0}, SSL Disabled. Communication will continue on TCP Stream.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, SSL Disabled. Communication will continue on TCP Stream.", args0: _connectionId); } /// @@ -576,11 +577,11 @@ private bool ValidateServerCertificate(object sender, X509Certificate cert, X509 { if (!_validateCert) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate will not be validated.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Certificate will not be validated.", args0: _connectionId); return true; } - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ValidateServerCertificate | SNI | INFO | Connection Id {0}, Certificate will be validated for Target Server name", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Certificate will be validated for Target Server name", args0: _connectionId); return SNICommon.ValidateSslServerCertificate(_targetServer, cert, policyErrors); } @@ -625,22 +626,22 @@ public override uint Send(SNIPacket packet) try { packet.WriteToStream(_stream); - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Send | SNI | INFO | Connection Id {0}, Data sent to stream synchronously", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Data sent to stream synchronously", args0: _connectionId); return TdsEnums.SNI_SUCCESS; } catch (ObjectDisposedException ode) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Send | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, ObjectDisposedException occurred: {1}", args0: _connectionId, args1: ode?.Message); return ReportTcpSNIError(ode); } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Send | SNI | ERR | Connection Id {0}, SocketException occurred: {1}", _connectionId, se.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, SocketException occurred: {1}", args0: _connectionId, args1: se?.Message); return ReportTcpSNIError(se); } catch (IOException ioe) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Send | SNI | ERR | Connection Id {0}, IOException occurred: {1}", _connectionId, ioe.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IOException occurred: {1}", args0: _connectionId, args1: ioe?.Message); return ReportTcpSNIError(ioe); } } @@ -680,7 +681,7 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) else { // otherwise it is timeout for 0 or less than -1 - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Error 258, Timeout error occurred.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Error 258, Timeout error occurred.", args0: _connectionId); ReportTcpSNIError(0, SNICommon.ConnTimeoutError, Strings.SNI_ERROR_11); return TdsEnums.SNI_WAIT_TIMEOUT; } @@ -693,25 +694,25 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) errorPacket = packet; packet = null; var e = new Win32Exception(); - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Win32 exception occurred: {1}", _connectionId, e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Win32 exception occurred: {1}", args0: _connectionId, args1: e?.Message); return ReportErrorAndReleasePacket(errorPacket, (uint)e.NativeErrorCode, 0, e.Message); } - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | INFO | Connection Id {0}, Data read from stream synchronously", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Data read from stream synchronously", args0: _connectionId); return TdsEnums.SNI_SUCCESS; } catch (ObjectDisposedException ode) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, ObjectDisposedException occurred: {1}", args0: _connectionId, args1: ode?.Message); return ReportErrorAndReleasePacket(errorPacket, ode); } catch (SocketException se) { errorPacket = packet; packet = null; - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, Socket exception occurred: {1}", _connectionId, se?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, Socket exception occurred: {1}", args0: _connectionId, args1: se?.Message); return ReportErrorAndReleasePacket(errorPacket, se); } catch (IOException ioe) @@ -721,11 +722,11 @@ public override uint Receive(out SNIPacket packet, int timeoutInMilliseconds) uint errorCode = ReportErrorAndReleasePacket(errorPacket, ioe); if (ioe.InnerException is SocketException socketException && socketException.SocketErrorCode == SocketError.TimedOut) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred with Wait Timeout (error 258): {1}", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IO exception occurred with Wait Timeout (error 258): {1}", args0: _connectionId, args1: ioe?.Message); errorCode = TdsEnums.SNI_WAIT_TIMEOUT; } - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.Receive | SNI | ERR | Connection Id {0}, IO exception occurred: {1}", _connectionId, ioe?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "Connection Id {0}, IO exception occurred: {1}", args0: _connectionId, args1: ioe?.Message); return errorCode; } finally @@ -754,12 +755,12 @@ public override void SetAsyncCallbacks(SNIAsyncCallback receiveCallback, SNIAsyn /// SNI error code public override uint SendAsync(SNIPacket packet, SNIAsyncCallback callback = null) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SNITcpHandle.SendAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { SNIAsyncCallback cb = callback ?? _sendCallback; packet.WriteToStreamAsync(_stream, cb, SNIProviders.TCP_PROV); - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.SendAsync | SNI | INFO | Connection Id {0}, Data sent to stream asynchronously", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Data sent to stream asynchronously", args0: _connectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } finally @@ -781,7 +782,7 @@ public override uint ReceiveAsync(ref SNIPacket packet) try { packet.ReadFromStreamAsync(_stream, _receiveCallback); - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Data received from stream asynchronously", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Data received from stream asynchronously", args0: _connectionId); return TdsEnums.SNI_SUCCESS_IO_PENDING; } catch (Exception e) when (e is ObjectDisposedException || e is SocketException || e is IOException) @@ -812,18 +813,18 @@ public override uint CheckConnection() // return true we can safely determine that the connection is no longer active. if (!_socket.Connected || (_socket.Poll(100, SelectMode.SelectRead) && _socket.Available == 0)) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.CheckConnection | SNI | INFO | Connection Id {0}, Socket not usable.", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Socket not usable.", args0: _connectionId); return TdsEnums.SNI_ERROR; } } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, Socket Exception occurred: {1}", _connectionId, se?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Socket Exception occurred: {1}", args0: _connectionId, args1: se?.Message); return ReportTcpSNIError(se); } catch (ObjectDisposedException ode) { - SqlClientEventSource.Log.TrySNITraceEvent("SNITCPHandle.ReceiveAsync | SNI | INFO | Connection Id {0}, ObjectDisposedException occurred: {1}", _connectionId, ode?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, ObjectDisposedException occurred: {1}", args0: _connectionId, args1: ode?.Message); return ReportTcpSNIError(ode); } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs index 72e34e5803..e49fc4c89a 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SSRP.cs @@ -13,6 +13,7 @@ namespace Microsoft.Data.SqlClient.SNI { internal class SSRP { + private const string s_className = nameof(SSRP); private const char SemicolonSeparator = ';'; private const int SqlServerBrowserPort = 1434; @@ -26,7 +27,7 @@ internal static int GetPortByInstanceName(string browserHostName, string instanc { Debug.Assert(!string.IsNullOrWhiteSpace(browserHostName), "browserHostName should not be null, empty, or whitespace"); Debug.Assert(!string.IsNullOrWhiteSpace(instanceName), "instanceName should not be null, empty, or whitespace"); - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SSRP.GetPortByInstanceName | SNI | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { byte[] instanceInfoRequest = CreateInstanceInfoRequest(instanceName); @@ -37,7 +38,7 @@ internal static int GetPortByInstanceName(string browserHostName, string instanc } catch (SocketException se) { - SqlClientEventSource.Log.TrySNITraceEvent("SSRP.GetPortByInstanceName | SNI | ERR | SocketException Message = {0}", se.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "SocketException Message = {0}", args0: se?.Message); throw new Exception(SQLMessage.SqlServerBrowserNotAccessible(), se); } @@ -73,7 +74,7 @@ internal static int GetPortByInstanceName(string browserHostName, string instanc private static byte[] CreateInstanceInfoRequest(string instanceName) { Debug.Assert(!string.IsNullOrWhiteSpace(instanceName), "instanceName should not be null, empty, or whitespace"); - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SSRP.CreateInstanceInfoRequest | SNI | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { const byte ClntUcastInst = 0x04; @@ -150,7 +151,7 @@ private static byte[] CreateDacPortInfoRequest(string instanceName) /// response packet from UDP server private static byte[] SendUDPRequest(string browserHostname, int port, byte[] requestPacket) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SSRP.SendUDPRequest | SNI | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { Debug.Assert(!string.IsNullOrWhiteSpace(browserHostname), "browserhostname should not be null, empty, or whitespace"); @@ -169,10 +170,10 @@ private static byte[] SendUDPRequest(string browserHostname, int port, byte[] re Task sendTask = client.SendAsync(requestPacket, requestPacket.Length, browserHostname, port); Task receiveTask = null; - SqlClientEventSource.Log.TrySNITraceEvent("SSRP.SendUDPRequest | SNI | INFO | Waiting for UDP Client to fetch Port info."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Waiting for UDP Client to fetch Port info."); if (sendTask.Wait(sendTimeOutMs) && (receiveTask = client.ReceiveAsync()).Wait(receiveTimeOutMs)) { - SqlClientEventSource.Log.TrySNITraceEvent("SSRP.SendUDPRequest | SNI | INFO | Received Port info from UDP Client."); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Received Port info from UDP Client."); responsePacket = receiveTask.Result.Buffer; } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetCoreApp.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetCoreApp.cs index ab1ef65a68..5f676ec457 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetCoreApp.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetCoreApp.cs @@ -25,7 +25,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati public override int Read(Span buffer) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.Read | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (!_encapsulate) @@ -80,7 +80,7 @@ public override int Read(Span buffer) public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.ReadAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (!_encapsulate) @@ -184,7 +184,7 @@ public override async ValueTask ReadAsync(Memory buffer, Cancellation public override void Write(ReadOnlySpan buffer) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.Write | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { // During the SSL negotiation phase, SSL is tunnelled over TDS packet type 0x12. After @@ -242,7 +242,7 @@ public override void Write(ReadOnlySpan buffer) public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.WriteAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (!_encapsulate) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetStandard.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetStandard.cs index 3ee7f77c93..ec7bdac54b 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetStandard.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.NetStandard.cs @@ -14,7 +14,7 @@ internal sealed partial class SslOverTdsStream : Stream { public override int Read(byte[] buffer, int offset, int count) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.Read | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (!_encapsulate) @@ -74,7 +74,7 @@ public override int Read(byte[] buffer, int offset, int count) public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.ReadAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (!_encapsulate) @@ -134,7 +134,7 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, public override void Write(byte[] buffer, int offset, int count) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.Write | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { // During the SSL negotiation phase, SSL is tunnelled over TDS packet type 0x12. After @@ -187,7 +187,7 @@ public override void Write(byte[] buffer, int offset, int count) public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { - long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent("SslOverTdsStream.WriteAsync | SNI | INFO | SCOPE | Entering Scope {0}"); + long scopeID = SqlClientEventSource.Log.TrySNIScopeEnterEvent(s_className); try { if (!_encapsulate) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs index 10fdd7fd28..3696b1323f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -15,6 +15,7 @@ namespace Microsoft.Data.SqlClient.SNI /// internal sealed partial class SslOverTdsStream : Stream { + private const string s_className = nameof(SslOverTdsStream); private readonly Stream _stream; private Guid _connectionId; @@ -48,7 +49,7 @@ public SslOverTdsStream(Stream stream, Guid connectionId = default) public void FinishHandshake() { _encapsulate = false; - SqlClientEventSource.Log.TrySNITraceEvent("SslOverTdsStream.FinishHandshake | SNI | INFO | Connection Id {0}, Switched from encapsulation to passthrough mode", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Switched from encapsulation to passthrough mode", args0: _connectionId); } /// @@ -67,7 +68,7 @@ public override void Flush() if (!(_stream is PipeStream)) { _stream.Flush(); - SqlClientEventSource.Log.TrySNITraceEvent("SslOverTdsStream.Flush | SNI | INFO | Connection Id {0}, Flushed stream", _connectionId); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.INFO, "Connection Id {0}, Flushed stream", args0: _connectionId); } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs index e529110f7b..78da5056f2 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -1001,6 +1001,7 @@ public override object ExecuteScalar() try { statistics = SqlStatistics.StartTimer(Statistics); + WriteBeginExecuteEvent(); SqlDataReader ds; ds = RunExecuteReader(0, RunBehavior.ReturnImmediately, returnStream: true); success = true; @@ -1081,6 +1082,7 @@ public override int ExecuteNonQuery() try { statistics = SqlStatistics.StartTimer(Statistics); + WriteBeginExecuteEvent(); InternalExecuteNonQuery(completion: null, sendToPipe: false, timeout: CommandTimeout, out _, methodName: nameof(ExecuteNonQuery)); return _rowsAffected; } @@ -1571,7 +1573,7 @@ public XmlReader ExecuteXmlReader() try { statistics = SqlStatistics.StartTimer(Statistics); - + WriteBeginExecuteEvent(); // use the reader to consume metadata SqlDataReader ds; ds = RunExecuteReader(CommandBehavior.SequentialAccess, RunBehavior.ReturnImmediately, returnStream: true); @@ -1646,6 +1648,7 @@ private IAsyncResult BeginExecuteXmlReaderInternal(CommandBehavior behavior, Asy if (!inRetry) { statistics = SqlStatistics.StartTimer(Statistics); + WriteBeginExecuteEvent(); } bool usedCache; @@ -1893,6 +1896,7 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) Exception e = null; try { + WriteBeginExecuteEvent(); statistics = SqlStatistics.StartTimer(Statistics); return RunExecuteReader(behavior, RunBehavior.ReturnImmediately, returnStream: true); } @@ -2071,7 +2075,7 @@ private IAsyncResult BeginExecuteReaderInternal(CommandBehavior behavior, AsyncC if (!inRetry) { statistics = SqlStatistics.StartTimer(Statistics); - + WriteBeginExecuteEvent(); ValidateAsyncCommand(); // Special case - done outside of try/catches to prevent putting a stateObj // back into pool when we should not. } @@ -6272,6 +6276,11 @@ public SqlCommand Clone() return clone; } + private void WriteBeginExecuteEvent() + { + SqlClientEventSource.Log.TryBeginExecuteEvent(ObjectID, Connection?.ClientConnectionId, CommandText); + } + private void WriteEndExecuteEvent(bool success, int? sqlExceptionNumber, bool synchronous) { if (SqlClientEventSource.Log.IsExecutionTraceEnabled()) @@ -6290,7 +6299,7 @@ private void WriteEndExecuteEvent(bool success, int? sqlExceptionNumber, bool sy int compositeState = successFlag | isSqlExceptionFlag | synchronousFlag; - SqlClientEventSource.Log.EndExecute(GetHashCode(), compositeState, sqlExceptionNumber.GetValueOrDefault()); + SqlClientEventSource.Log.TryEndExecuteEvent(ObjectID, Connection?.ClientConnectionId, compositeState, sqlExceptionNumber.GetValueOrDefault()); } } } diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index a3fa23baca..7b5a2df68e 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -1301,7 +1301,7 @@ internal SqlError ProcessSNIError(TdsParserStateObject stateObj) #if DEBUG // There is an exception here for MARS as its possible that another thread has closed the connection just as we see an error Debug.Assert(SniContext.Undefined != stateObj.DebugOnlyCopyOfSniContext || ((_fMARS) && ((_state == TdsParserState.Closed) || (_state == TdsParserState.Broken))), "SniContext must not be None"); - SqlClientEventSource.Log.TrySNITraceEvent(" SNIContext must not be None = {0}, _fMARS = {1}, TDS Parser State = {2}", stateObj.DebugOnlyCopyOfSniContext, _fMARS, _state); + SqlClientEventSource.Log.TryTraceEvent(" SNIContext must not be None = {0}, _fMARS = {1}, TDS Parser State = {2}", stateObj.DebugOnlyCopyOfSniContext, _fMARS, _state); #endif SNIErrorDetails details = GetSniErrorDetails(); diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs index d813e0dc8a..d9252a5be0 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -7372,10 +7372,7 @@ private SmiRequestExecutor SetUpSmiRequest(SqlInternalConnectionSmi innerConnect private void WriteBeginExecuteEvent() { - if (Connection != null) - { - SqlClientEventSource.Log.BeginExecute(GetHashCode(), Connection.DataSource, Connection.Database, CommandText); - } + SqlClientEventSource.Log.TryBeginExecuteEvent(ObjectID, Connection?.ClientConnectionId, CommandText); } /// @@ -7402,7 +7399,7 @@ private void WriteEndExecuteEvent(bool success, int? sqlExceptionNumber, bool sy int compositeState = successFlag | isSqlExceptionFlag | synchronousFlag; - SqlClientEventSource.Log.EndExecute(GetHashCode(), compositeState, sqlExceptionNumber.GetValueOrDefault()); + SqlClientEventSource.Log.TryEndExecuteEvent(ObjectID, Connection?.ClientConnectionId, compositeState, sqlExceptionNumber.GetValueOrDefault()); } } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs index 1e9252d0e8..045b8c7eba 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs @@ -16,6 +16,7 @@ internal partial class SqlClientEventSource : EventSource internal static readonly SqlClientEventSource Log = new SqlClientEventSource(); private const string NullStr = "null"; + private const string SqlCommand_ClassName = nameof(SqlCommand); #region Event IDs // Initialized static Scope IDs @@ -227,6 +228,26 @@ public static class Tasks /// Task that tracks SqlCommand execution. /// public const EventTask ExecuteCommand = (EventTask)1; + + /// + /// Task that tracks trace scope. + /// + public const EventTask Scope = (EventTask)2; + + /// + /// Task that tracks trace scope. + /// + public const EventTask PoolerScope = (EventTask)3; + + /// + /// Task that tracks trace scope. + /// + public const EventTask NotificationScope = (EventTask)4; + + /// + /// Task that tracks trace scope. + /// + public const EventTask SNIScope = (EventTask)5; } #endregion @@ -268,8 +289,8 @@ public static class Tasks internal bool IsSNIScopeEnabled() => Log.IsEnabled(EventLevel.Informational, Keywords.SNIScope); #endregion - private string GetFormattedMessage(string className, string memberName, string infoType, string message) => - new StringBuilder(className).Append(".").Append(memberName).Append(infoType).Append(message).ToString(); + private string GetFormattedMessage(string className, string memberName, string eventType, string message) => + new StringBuilder(className).Append(".").Append(memberName).Append(eventType).Append(message).ToString(); #region overloads //Never use event writer directly as they are not checking for enabled/disabled situations. Always use overloads. @@ -426,6 +447,28 @@ internal void TryScopeLeaveEvent(long scopeId) } #endregion + #region Execution Trace + [NonEvent] + internal void TryBeginExecuteEvent(int objectId, object connectionId, string commandText, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + { + if (Log.IsExecutionTraceEnabled()) + { + BeginExecute(GetFormattedMessage(SqlCommand_ClassName, memberName, EventType.INFO, + string.Format("Object Id {0}, Client Connection Id {1}, Command Text {2}", objectId, connectionId, commandText))); + } + } + + [NonEvent] + internal void TryEndExecuteEvent(int objectId, object connectionId, int compositeState, int sqlExceptionNumber, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + { + if (Log.IsExecutionTraceEnabled()) + { + EndExecute(GetFormattedMessage(SqlCommand_ClassName, memberName, EventType.INFO, + string.Format("Object Id {0}, Client Connection Id {1}, Composite State {2}, Sql Exception Number {3}", objectId, connectionId, compositeState, sqlExceptionNumber))); + } + } + #endregion + #region Notification Trace #region Notification Traces without if statements @@ -802,65 +845,65 @@ internal void StateDumpEvent(string message, T0 args0, T1 args1) #region SNI Trace [NonEvent] - internal void TrySNITraceEvent(string className, string infoType, string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + internal void TrySNITraceEvent(string className, string eventType, string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(GetFormattedMessage(className, memberName, infoType, message)); + SNITrace(GetFormattedMessage(className, memberName, eventType, message)); } } [NonEvent] - internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + internal void TrySNITraceEvent(string className, string eventType, string message, T0 args0, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr))); + SNITrace(GetFormattedMessage(className, memberName, eventType, string.Format(message, args0?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + internal void TrySNITraceEvent(string className, string eventType, string message, T0 args0, T1 args1, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr))); + SNITrace(GetFormattedMessage(className, memberName, eventType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, T2 args2, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + internal void TrySNITraceEvent(string className, string eventType, string message, T0 args0, T1 args1, T2 args2, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr))); + SNITrace(GetFormattedMessage(className, memberName, eventType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, T2 args2, T3 args3, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + internal void TrySNITraceEvent(string className, string eventType, string message, T0 args0, T1 args1, T2 args2, T3 args3, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr))); + SNITrace(GetFormattedMessage(className, memberName, eventType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + internal void TrySNITraceEvent(string className, string eventType, string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr, args4?.ToString() ?? NullStr))); + SNITrace(GetFormattedMessage(className, memberName, eventType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr, args4?.ToString() ?? NullStr))); } } [NonEvent] - internal void TrySNITraceEvent(string className, string infoType, string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4, T5 args5, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") + internal void TrySNITraceEvent(string className, string eventType, string message, T0 args0, T1 args1, T2 args2, T3 args3, T4 args4, T5 args5, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "") { if (Log.IsSNITraceEnabled()) { - SNITrace(GetFormattedMessage(className, memberName, infoType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr, args4?.ToString() ?? NullStr, args5?.ToString() ?? NullStr))); + SNITrace(GetFormattedMessage(className, memberName, eventType, string.Format(message, args0?.ToString() ?? NullStr, args1?.ToString() ?? NullStr, args2?.ToString() ?? NullStr, args3?.ToString() ?? NullStr, args4?.ToString() ?? NullStr, args5?.ToString() ?? NullStr))); } } #endregion @@ -892,28 +935,18 @@ internal void TrySNIScopeLeaveEvent(long scopeId) #region Write Events [Event(BeginExecuteEventId, Keywords = Keywords.ExecutionTrace, Task = Tasks.ExecuteCommand, Opcode = EventOpcode.Start)] - internal void BeginExecute(int objectId, string dataSource, string database, string commandText) - { - if (Log.IsExecutionTraceEnabled()) - { - WriteEvent(BeginExecuteEventId, objectId, dataSource, database, commandText); - } - } + internal void BeginExecute(string message) => + WriteEvent(BeginExecuteEventId, message); [Event(EndExecuteEventId, Keywords = Keywords.ExecutionTrace, Task = Tasks.ExecuteCommand, Opcode = EventOpcode.Stop)] - internal void EndExecute(int objectId, int compositeState, int sqlExceptionNumber) - { - if (Log.IsExecutionTraceEnabled()) - { - WriteEvent(EndExecuteEventId, objectId, compositeState, sqlExceptionNumber); - } - } + internal void EndExecute(string message) => + WriteEvent(EndExecuteEventId, message); [Event(TraceEventId, Level = EventLevel.Informational, Keywords = Keywords.Trace)] internal void Trace(string message) => WriteEvent(TraceEventId, message); - [Event(ScopeEnterId, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Keywords = Keywords.Scope)] + [Event(ScopeEnterId, Level = EventLevel.Informational, Task = Tasks.Scope, Opcode = EventOpcode.Start, Keywords = Keywords.Scope)] internal long ScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextScopeId); @@ -921,7 +954,7 @@ internal long ScopeEnter(string message) return scopeId; } - [Event(ScopeExitId, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Keywords = Keywords.Scope)] + [Event(ScopeExitId, Level = EventLevel.Informational, Task = Tasks.Scope, Opcode = EventOpcode.Stop, Keywords = Keywords.Scope)] internal void ScopeLeave(string message) => WriteEvent(ScopeExitId, message); @@ -929,7 +962,7 @@ internal void ScopeLeave(string message) => internal void NotificationTrace(string message) => WriteEvent(NotificationTraceId, message); - [Event(NotificationScopeEnterId, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Keywords = Keywords.NotificationScope)] + [Event(NotificationScopeEnterId, Level = EventLevel.Informational, Task = Tasks.NotificationScope, Opcode = EventOpcode.Start, Keywords = Keywords.NotificationScope)] internal long NotificationScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextNotificationScopeId); @@ -937,7 +970,7 @@ internal long NotificationScopeEnter(string message) return scopeId; } - [Event(NotificationScopeExitId, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Keywords = Keywords.NotificationScope)] + [Event(NotificationScopeExitId, Level = EventLevel.Informational, Task = Tasks.NotificationScope, Opcode = EventOpcode.Stop, Keywords = Keywords.NotificationScope)] internal void NotificationScopeLeave(string message) => WriteEvent(NotificationScopeExitId, message); @@ -945,7 +978,7 @@ internal void NotificationScopeLeave(string message) => internal void PoolerTrace(string message) => WriteEvent(PoolerTraceId, message); - [Event(PoolerScopeEnterId, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Keywords = Keywords.PoolerScope)] + [Event(PoolerScopeEnterId, Level = EventLevel.Informational, Task = Tasks.PoolerScope, Opcode = EventOpcode.Start, Keywords = Keywords.PoolerScope)] internal long PoolerScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextPoolerScopeId); @@ -953,7 +986,7 @@ internal long PoolerScopeEnter(string message) return scopeId; } - [Event(PoolerScopeExitId, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Keywords = Keywords.PoolerScope)] + [Event(PoolerScopeExitId, Level = EventLevel.Informational, Task = Tasks.PoolerScope, Opcode = EventOpcode.Stop, Keywords = Keywords.PoolerScope)] internal void PoolerScopeLeave(string message) => WriteEvent(PoolerScopeExitId, message); @@ -981,7 +1014,7 @@ internal void AdvancedTraceBin(string message) => internal void AdvancedTraceError(string message) => WriteEvent(AdvancedTraceErrorId, message); - [Event(CorrelationTraceId, Level = EventLevel.Informational, Keywords = Keywords.CorrelationTrace, Opcode = EventOpcode.Start)] + [Event(CorrelationTraceId, Level = EventLevel.Informational, Keywords = Keywords.CorrelationTrace)] internal void CorrelationTrace(string message) => WriteEvent(CorrelationTraceId, message); @@ -993,7 +1026,7 @@ internal void StateDump(string message) => internal void SNITrace(string message) => WriteEvent(SNITraceEventId, message); - [Event(SNIScopeEnterId, Level = EventLevel.Informational, Opcode = EventOpcode.Start, Keywords = Keywords.SNIScope)] + [Event(SNIScopeEnterId, Level = EventLevel.Informational, Task = Tasks.SNIScope, Opcode = EventOpcode.Start, Keywords = Keywords.SNIScope)] internal long SNIScopeEnter(string message) { long scopeId = Interlocked.Increment(ref s_nextSNIScopeId); @@ -1001,12 +1034,18 @@ internal long SNIScopeEnter(string message) return scopeId; } - [Event(SNIScopeExitId, Level = EventLevel.Informational, Opcode = EventOpcode.Stop, Keywords = Keywords.SNIScope)] + [Event(SNIScopeExitId, Level = EventLevel.Informational, Task = Tasks.SNIScope, Opcode = EventOpcode.Stop, Keywords = Keywords.SNIScope)] internal void SNIScopeLeave(string message) => WriteEvent(SNIScopeExitId, message); #endregion } + internal static class EventType + { + public const string INFO = " | INFO | "; + public const string ERR = " | ERR | "; + } + internal readonly struct SNIEventScope : IDisposable { private readonly long _scopeId; From e3c428f2f16b95e6fe8b711eda9414c999c20e05 Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Wed, 24 Feb 2021 23:17:43 -0800 Subject: [PATCH 11/11] Review feedback applied --- .../src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs index 597172c37f..395cfed4be 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIMarsConnection.cs @@ -347,7 +347,7 @@ public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) } catch (Exception e) { - SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "MARS Session Id {0}, Exception occurred: {2}", args0: _currentHeader?.sessionId, args1: e?.Message); + SqlClientEventSource.Log.TrySNITraceEvent(s_className, EventType.ERR, "SMUX_ACK | MARS Session Id {0}, Exception occurred: {2}", args0: _currentHeader?.sessionId, args1: e?.Message); SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } #if DEBUG