mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:59:48 -05:00
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
169 lines
6.6 KiB
C#
169 lines
6.6 KiB
C#
//
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
//
|
|
|
|
using Microsoft.SqlTools.Utility;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.Test.Common
|
|
{
|
|
public class TestLogger
|
|
{
|
|
private string logFilePath;
|
|
private string logMessage;
|
|
private string logContents;
|
|
private string logFileName;
|
|
private string topFrame;
|
|
|
|
public bool ShouldVerifyCallstack { get; set; } = false;
|
|
public string TestName { get => testName ?? TraceSource; set => testName = value; }
|
|
public string TraceSource { get; set; } = "sqltoolsTest";
|
|
public string LogMessage { get => logMessage ?? $"{TestName} test message"; set => logMessage = value; }
|
|
public string LogFilePath { get => logFilePath ?? Logger.GenerateLogFilePath(Path.Combine(Directory.GetCurrentDirectory(), TraceSource)); set => logFilePath = value; }
|
|
public TraceEventType EventType { get; set; } = TraceEventType.Information;
|
|
public SourceLevels TracingLevel { get; set; } = SourceLevels.Critical;
|
|
public bool DoNotUseTraceSource { get; set; } = false;
|
|
|
|
private List<Action> pendingVerifications;
|
|
private string testName;
|
|
|
|
public string CallstackMessage { get => $"Callstack=\\s*{TopFrame}"; }
|
|
|
|
public string LogFileName { get => logFileName ?? Logger.LogFileFullPath; set => logFileName = value; }
|
|
public void Initialize() =>
|
|
Logger.Initialize(TracingLevel, LogFilePath, TraceSource); // initialize the logger
|
|
public string LogContents
|
|
{
|
|
get
|
|
{
|
|
if (logContents == null)
|
|
{
|
|
Logger.Close();
|
|
Assert.True(!string.IsNullOrWhiteSpace(LogFileName));
|
|
Assert.True(LogFileName.Length > "{TraceSource}_.log".Length);
|
|
Assert.True(File.Exists(LogFileName));
|
|
logContents = File.ReadAllText(LogFileName);
|
|
}
|
|
return logContents;
|
|
}
|
|
set => logContents = value;
|
|
}
|
|
|
|
public string TopFrame { get => topFrame ?? "at System.Environment.get_StackTrace()"; set => topFrame = value; }
|
|
|
|
public List<Action> PendingVerifications
|
|
{
|
|
get
|
|
{
|
|
if (pendingVerifications == null)
|
|
{
|
|
pendingVerifications = new List<Action>();
|
|
}
|
|
return pendingVerifications;
|
|
}
|
|
set => pendingVerifications = value;
|
|
}
|
|
|
|
public void Write()
|
|
{
|
|
// write test log
|
|
if (DoNotUseTraceSource)
|
|
{
|
|
TraceSource savedTraceSource = Logger.TraceSource;
|
|
Logger.TraceSource = null;
|
|
Logger.Write(EventType, LogMessage);
|
|
Logger.TraceSource = savedTraceSource;
|
|
}
|
|
else
|
|
Logger.Write(EventType, LogMessage);
|
|
}
|
|
|
|
public void WriteWithCallstack()
|
|
{
|
|
// write test log with callstack
|
|
Logger.WriteWithCallstack(EventType, LogMessage);
|
|
ShouldVerifyCallstack = true;
|
|
}
|
|
|
|
public void Verify(bool expectLogMessage = true) => Verify(ShouldVerifyCallstack, expectLogMessage);
|
|
|
|
public void Verify(bool shouldVerifyCallstack, bool expectLogMessage = true) => Verify(EventType, LogMessage, CallstackMessage, shouldVerifyCallstack, expectLogMessage);
|
|
|
|
public void Verify(TraceEventType eventType, string message, string callstackMessage, bool shouldVerifyCallstack = false, bool expectLogMessage = true)
|
|
{
|
|
if (expectLogMessage)
|
|
{
|
|
Assert.True(File.Exists(Logger.LogFileFullPath) && Regex.IsMatch(LogContents, $@"\b{eventType}:\s+\d+\s+:\s+{message}", RegexOptions.Compiled));
|
|
}
|
|
else
|
|
{
|
|
Assert.False(File.Exists(Logger.LogFileFullPath) && Regex.IsMatch(LogContents, $@"\b{eventType}:\s+\d+\s+:\s+{message}", RegexOptions.Compiled));
|
|
}
|
|
if (shouldVerifyCallstack)
|
|
{
|
|
VerifyCallstack(callstackMessage, expectLogMessage);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Perform all the pending verifications
|
|
/// </summary>
|
|
public void VerifyPending()
|
|
{
|
|
foreach (var pv in PendingVerifications)
|
|
{
|
|
pv.Invoke();
|
|
}
|
|
}
|
|
|
|
public void VerifyCallstack(bool expectLogMessage = true) => VerifyCallstack(CallstackMessage, expectLogMessage);
|
|
|
|
public void VerifyCallstack(string message, bool expectLogMessage = true)
|
|
{
|
|
if (expectLogMessage)
|
|
{
|
|
Assert.True(File.Exists(Logger.LogFileFullPath) && Regex.IsMatch(LogContents, $"{message}", RegexOptions.Compiled));
|
|
}
|
|
else
|
|
{
|
|
Assert.False(File.Exists(Logger.LogFileFullPath) && Regex.IsMatch(LogContents, $"{message}", RegexOptions.Compiled));
|
|
}
|
|
}
|
|
|
|
|
|
public static void VerifyInitialization(SourceLevels expectedTracingLevel, string expectedTraceSource, string logFilePath, bool isLogFileExpectedToExist, int? testNo = null)
|
|
{
|
|
string FailureMessagePrefix = testNo.HasValue ? $"For Test No:{testNo.Value.ToString()}," : string.Empty;
|
|
Assert.False(string.IsNullOrWhiteSpace(logFilePath), $"{FailureMessagePrefix} LogFilePath should not be null or whitespaces");
|
|
Assert.True(expectedTracingLevel == Logger.TracingLevel, $"{FailureMessagePrefix} expected Tracing Level did not match the configured value");
|
|
if (isLogFileExpectedToExist)
|
|
{
|
|
Assert.True(File.Exists(logFilePath), $"{FailureMessagePrefix} logFilePath:{logFilePath} must exist");
|
|
}
|
|
else
|
|
{
|
|
Assert.False(File.Exists(logFilePath), $"{FailureMessagePrefix} {logFilePath} must not exist");
|
|
}
|
|
Assert.True(string.Compare(expectedTraceSource, Logger.TraceSource.Name, ignoreCase: true) == 0, $"{FailureMessagePrefix} expected Trace Source Name did not match the configured value");
|
|
}
|
|
|
|
public void Cleanup() => Cleanup(Logger.LogFileFullPath);
|
|
|
|
public static void Cleanup(string logFileName)
|
|
{
|
|
if (File.Exists(logFileName))
|
|
{
|
|
Logger.Close();
|
|
File.Delete(logFileName);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|