Skip to content

Commit

Permalink
Tracing updates + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed Feb 19, 2021
1 parent 483c87a commit c89c7e0
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ 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;
}

// In case the registry had an empty path for dll
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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -152,23 +154,23 @@ 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)
{
// 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;
}
}
else
{
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;
}

Expand All @@ -178,18 +180,18 @@ 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;
}
}
}
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
Expand All @@ -208,7 +210,7 @@ internal static bool ValidateSslServerCertificate(string targetServerName, X509C
/// <returns></returns>
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));
}

Expand All @@ -221,7 +223,7 @@ internal static uint ReportSNIError(SNIProviders provider, uint nativeError, uin
/// <returns></returns>
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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Loading

0 comments on commit c89c7e0

Please sign in to comment.