mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-30 17:24:37 -05:00
Enhanced Logging for sqltoolsservice (#695)
This change modifies the logging framework within sqltoolservice. Moves away from custom Logger object to start using .Net tracing framework. It supports for the static Trace and TraceSource way of logging. For all new code it is recommend that we log the log messages using the existing static Logger class, while the code changes will continue to route the older Trace.Write* calls from the process to same log listeners (and thus the log targets) as used by the Logger class. Thus tracing in SMO code that uses Trace.Write* methods gets routed to the same file as the messages from rest of SQLTools Service code. Make changes to start using .Net Frameworks codebase for all logging to unify our logging story. Allows parameter to set tracingLevel filters that controls what kinds of message make it to the log file. Allows a parameter to set a specific log file name so if these gets set by external code (the UI code using the tools service for example) then the external code is aware of the current log file in use. Adding unittests to test out the existing and improved logging capabilities. Sequences of checkins in development branch: * Saving v1 of logging to prepare for code review. Minor cleanup and some end to end testing still remains * Removing local launchSettings.json files * added support for lazy listener to sqltoolsloglistener and removed incorrect changes to comments across files in previous checkin * Converting time to local time when writing entries to the log * move the hosting.v2 to new .net based logging code * removing *.dgml files and addding them to .gitignore * fixing typo of defaultTraceSource * Addressing pull request feedback * Adding a test to verify logging from SMO codebase * propogating changes to v1 sqltools.hosting commandoptions.cs to the v2 version * Fixing comments on start and stop callstack methods and whitespaces * Commenting a test that got uncommented by mistake * addding .gitattributes file as .sql file was observed to be misconstrued as a binary file
This commit is contained in:
@@ -24,6 +24,7 @@ using Microsoft.SqlTools.CoreServices.LanguageServices.Contracts;
|
||||
using ConnectionType = Microsoft.SqlTools.DataProtocol.Contracts.Connection.ConnectionType;
|
||||
using Microsoft.SqlTools.Hosting.Extensibility;
|
||||
using System.Composition;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Microsoft.SqlTools.CoreServices.Connection
|
||||
{
|
||||
@@ -317,7 +318,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Normal, "Failed to close temporary connections. error: " + ex.Message);
|
||||
Logger.Write(TraceEventType.Information, "Failed to close temporary connections. error: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1175,8 +1176,8 @@ namespace Microsoft.SqlTools.CoreServices.Connection
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Instance.Write(
|
||||
LogLevel.Error,
|
||||
Logger.Write(
|
||||
TraceEventType.Error,
|
||||
string.Format(
|
||||
"Exception caught while trying to change database context to [{0}] for OwnerUri [{1}]. Exception:{2}",
|
||||
newDatabaseName,
|
||||
@@ -1255,7 +1256,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Verbose, "Could not send Connection telemetry event " + ex.ToString());
|
||||
Logger.Write(TraceEventType.Verbose, "Could not send Connection telemetry event " + ex.ToString());
|
||||
}
|
||||
|
||||
});
|
||||
@@ -1301,7 +1302,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection
|
||||
string error = string.Format(CultureInfo.InvariantCulture,
|
||||
"Failed opening a SqlConnection: error:{0} inner:{1} stacktrace:{2}",
|
||||
ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty, ex.StackTrace);
|
||||
Logger.Instance.Write(LogLevel.Error, error);
|
||||
Logger.Write(TraceEventType.Error, error);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Microsoft.SqlTools.Hosting.Utility;
|
||||
|
||||
@@ -350,13 +351,13 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
public void TraceSettings()
|
||||
{
|
||||
// NOTE: logging as warning so we can get this data in the IEService DacFx logs
|
||||
Logger.Instance.Write(LogLevel.Warning, Resources.LoggingAmbientSettings);
|
||||
Logger.Write(TraceEventType.Warning, Resources.LoggingAmbientSettings);
|
||||
|
||||
foreach (KeyValuePair<string, AmbientValue> setting in _configuration)
|
||||
{
|
||||
// Log Ambient Settings
|
||||
Logger.Instance.Write(
|
||||
LogLevel.Warning,
|
||||
Logger.Write(
|
||||
TraceEventType.Warning,
|
||||
string.Format(
|
||||
Resources.AmbientSettingFormat,
|
||||
setting.Key,
|
||||
@@ -406,7 +407,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, string.Format(Resources.UnableToAssignValue, value.GetType().FullName, _type.FullName));
|
||||
Logger.Write(TraceEventType.Error, string.Format(Resources.UnableToAssignValue, value.GetType().FullName, _type.FullName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.Hosting.Utility;
|
||||
|
||||
@@ -270,7 +271,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
}
|
||||
catch
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, String.Format(Resources.FailedToParseConnectionString, connection.ConnectionString));
|
||||
Logger.Write(TraceEventType.Error, String.Format(Resources.FailedToParseConnectionString, connection.ConnectionString));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,7 +429,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
Validate.IsNotNull(nameof(connection), connection);
|
||||
if (!(connection.State == ConnectionState.Open))
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Warning, Resources.ConnectionPassedToIsCloudShouldBeOpen);
|
||||
Logger.Write(TraceEventType.Warning, Resources.ConnectionPassedToIsCloudShouldBeOpen);
|
||||
}
|
||||
|
||||
Func<string, bool> executeCommand = commandText =>
|
||||
@@ -540,7 +540,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
return true;
|
||||
}
|
||||
|
||||
Logger.Instance.Write(LogLevel.Error, ex.ToString());
|
||||
Logger.Write(TraceEventType.Error, ex.ToString());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -668,7 +668,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
},
|
||||
(ex) =>
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, ex.ToString());
|
||||
Logger.Write(TraceEventType.Error, ex.ToString());
|
||||
return StandardExceptionHandler(ex); // handled
|
||||
},
|
||||
useRetry: true);
|
||||
@@ -739,7 +739,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
Validate.IsNotNull(nameof(connection), connection);
|
||||
if (!(connection.State == ConnectionState.Open))
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, "connection passed to GetServerVersion should be open.");
|
||||
Logger.Write(TraceEventType.Error, "connection passed to GetServerVersion should be open.");
|
||||
}
|
||||
|
||||
Func<string, ServerInfo> getServerInfo = commandText =>
|
||||
@@ -788,8 +788,8 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
{
|
||||
//we don't want to fail the normal flow if any unexpected thing happens
|
||||
//during caching although it's unlikely. So we just log the exception and ignore it
|
||||
Logger.Instance.Write(LogLevel.Error, Resources.FailedToCacheIsCloud);
|
||||
Logger.Instance.Write(LogLevel.Error, ex.ToString());
|
||||
Logger.Write(TraceEventType.Error, Resources.FailedToCacheIsCloud);
|
||||
Logger.Write(TraceEventType.Error, ex.ToString());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -892,7 +892,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
|
||||
if (handledEx != null)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, String.Format(Resources.ErrorParsingConnectionString, handledEx));
|
||||
Logger.Write(TraceEventType.Error, String.Format(Resources.ErrorParsingConnectionString, handledEx));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -441,7 +441,7 @@ SET NUMERIC_ROUNDABORT OFF;";
|
||||
}
|
||||
catch (SqlException exception)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, Resources.UnableToRetrieveAzureSessionId + exception.ToString());
|
||||
Logger.Write(TraceEventType.Error, Resources.UnableToRetrieveAzureSessionId + exception.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
//
|
||||
|
||||
using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.SqlTools.Hosting.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
@@ -30,7 +31,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
RetryPolicyUtils.AppendThrottlingDataIfIsThrottlingError(sqlException, err);
|
||||
if (RetryPolicyUtils.IsNonRetryableDataTransferError(err.Number))
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, string.Format(Resources.ExceptionCannotBeRetried, err.Number, err.Message));
|
||||
Logger.Write(TraceEventType.Error, string.Format(Resources.ExceptionCannotBeRetried, err.Number, err.Message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
bool shouldRetry = canRetry
|
||||
&& ShouldRetryImpl(retryState);
|
||||
|
||||
Logger.Instance.Write(LogLevel.Error,
|
||||
Logger.Write(TraceEventType.Error,
|
||||
string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"Retry requested: Retry count = {0}. Delay = {1}, SQL Error Number = {2}, Can retry error = {3}, Will retry = {4}",
|
||||
@@ -267,7 +267,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
{
|
||||
bool shouldIgnoreError = ErrorDetectionStrategy.ShouldIgnoreError(retryState.LastError);
|
||||
|
||||
Logger.Instance.Write(LogLevel.Error,
|
||||
Logger.Write(TraceEventType.Error,
|
||||
string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"Ignore Error requested: Retry count = {0}. Delay = {1}, SQL Error Number = {2}, Should Ignore Error = {3}",
|
||||
|
||||
@@ -392,7 +392,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
|
||||
internal static void DataConnectionFailureRetry(RetryState retryState)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Normal, string.Format(CultureInfo.InvariantCulture,
|
||||
Logger.Write(TraceEventType.Information, string.Format(CultureInfo.InvariantCulture,
|
||||
"Connection retry number {0}. Delaying {1} ms before retry. Exception: {2}",
|
||||
retryState.RetryCount,
|
||||
retryState.Delay.TotalMilliseconds.ToString(CultureInfo.InvariantCulture),
|
||||
@@ -403,7 +403,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
|
||||
internal static void CommandFailureRetry(RetryState retryState, string commandKeyword)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Normal, string.Format(
|
||||
Logger.Write(TraceEventType.Information, string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"{0} retry number {1}. Delaying {2} ms before retry. Exception: {3}",
|
||||
commandKeyword,
|
||||
@@ -416,7 +416,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
|
||||
internal static void CommandFailureIgnore(RetryState retryState, string commandKeyword)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Normal, string.Format(
|
||||
Logger.Write(TraceEventType.Information, string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"{0} retry number {1}. Ignoring failure. Exception: {2}",
|
||||
commandKeyword,
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.SqlTools.Hosting.Utility;
|
||||
|
||||
@@ -344,7 +345,7 @@ namespace Microsoft.SqlTools.CoreServices.Connection.ReliableConnection
|
||||
{
|
||||
if (azureSessionId != Guid.Empty)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Warning, string.Format(
|
||||
Logger.Write(TraceEventType.Warning, string.Format(
|
||||
"Retry occurred: session: {0}; attempt - {1}; delay - {2}; exception - \"{3}\"",
|
||||
azureSessionId,
|
||||
retryState.RetryCount,
|
||||
|
||||
@@ -9,6 +9,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.Hosting.Utility;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Microsoft.SqlTools.CoreServices.LanguageServices
|
||||
{
|
||||
@@ -319,7 +320,7 @@ namespace Microsoft.SqlTools.CoreServices.LanguageServices
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, "Unexpected exception on the binding queue: " + ex.ToString());
|
||||
Logger.Write(TraceEventType.Error, "Unexpected exception on the binding queue: " + ex.ToString());
|
||||
if (queueItem.ErrorHandler != null)
|
||||
{
|
||||
result = queueItem.ErrorHandler(ex);
|
||||
@@ -346,14 +347,14 @@ namespace Microsoft.SqlTools.CoreServices.LanguageServices
|
||||
|
||||
bindTask
|
||||
.ContinueWith((a) => bindingContext.BindingLock.Set())
|
||||
.ContinueWithOnFaulted(t => Logger.Instance.Write(LogLevel.Error, "Binding queue threw exception " + t.Exception.ToString()));
|
||||
.ContinueWithOnFaulted(t => Logger.Write(TraceEventType.Error, "Binding queue threw exception " + t.Exception.ToString()));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// catch and log any exceptions raised in the binding calls
|
||||
// set item processed to avoid deadlocks
|
||||
Logger.Instance.Write(LogLevel.Error, "Binding queue threw exception " + ex.ToString());
|
||||
Logger.Write(TraceEventType.Error, "Binding queue threw exception " + ex.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="../../Common.props" />
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
@@ -29,8 +29,8 @@
|
||||
<PackageReference Include="System.Composition" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Microsoft.SqlTools.Hosting.v2\Microsoft.SqlTools.Hosting.csproj" />
|
||||
<ProjectReference Include="..\Microsoft.SqlTools.DataProtocol.Contracts\Microsoft.SqlTools.DataProtocol.Contracts.csproj" />
|
||||
<ProjectReference Include="..\Microsoft.SqlTools.Hosting.v2\Microsoft.SqlTools.Hosting.v2.csproj" />
|
||||
<ProjectReference Include="..\Microsoft.SqlTools.DataProtocol.Contracts\Microsoft.SqlTools.DataProtocol.Contracts.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Localization\sr.resx" />
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -100,7 +101,7 @@ namespace Microsoft.SqlTools.CoreServices.Workspace
|
||||
{
|
||||
try
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Verbose, "HandleDidChangeConfigurationNotification");
|
||||
Logger.Write(TraceEventType.Verbose, "HandleDidChangeConfigurationNotification");
|
||||
|
||||
// Propagate the changes to the event handlers
|
||||
var configUpdateTasks = ConfigChangeCallbacks.Select(
|
||||
@@ -109,7 +110,7 @@ namespace Microsoft.SqlTools.CoreServices.Workspace
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Instance.Write(LogLevel.Error, "Unknown error " + ex.ToString());
|
||||
Logger.Write(TraceEventType.Error, "Unknown error " + ex.ToString());
|
||||
// Swallow exceptions here to prevent us from crashing
|
||||
// TODO: this probably means the ScriptFile model is in a bad state or out of sync with the actual file; we should recover here
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user