mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:35:43 -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
127 lines
4.8 KiB
C#
127 lines
4.8 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 System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SqlTools.ServiceLayer.TestDriver.Driver;
|
|
using Microsoft.SqlTools.Utility;
|
|
using Xunit;
|
|
|
|
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
|
|
|
// turn off entry point since this is xUnit project
|
|
#if false
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Tests
|
|
{
|
|
internal class Program
|
|
{
|
|
internal static int Main(string[] args)
|
|
{
|
|
if (args.Length < 1)
|
|
{
|
|
Console.WriteLine("Microsoft.SqlTools.ServiceLayer.TestDriver.exe [tests]" + Environment.NewLine +
|
|
" [tests] is a space-separated list of tests to run." + Environment.NewLine +
|
|
" They are qualified within the Microsoft.SqlTools.ServiceLayer.TestDriver.Tests namespace" + Environment.NewLine +
|
|
"Be sure to set the environment variable " + ServiceTestDriver.ServiceHostEnvironmentVariable + " to the full path of the sqltoolsservice executable.");
|
|
return 0;
|
|
}
|
|
|
|
Logger.Initialize("testdriver", TraceEventType.Verbose);
|
|
|
|
int returnCode = 0;
|
|
Task.Run(async () =>
|
|
{
|
|
string testNamespace = "Microsoft.SqlTools.ServiceLayer.TestDriver.Tests.";
|
|
foreach (var test in args)
|
|
{
|
|
try
|
|
{
|
|
var testName = test.Contains(testNamespace) ? test.Replace(testNamespace, "") : test;
|
|
bool containsTestName = testName.Contains(".");
|
|
var className = containsTestName ? testName.Substring(0, testName.LastIndexOf('.')) : testName;
|
|
var methodName = containsTestName ? testName.Substring(testName.LastIndexOf('.') + 1) : null;
|
|
|
|
var type = Type.GetType(testNamespace + className);
|
|
if (type == null)
|
|
{
|
|
Console.WriteLine("Invalid class name");
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(methodName))
|
|
{
|
|
var methods = type.GetMethods().Where(x => x.CustomAttributes.Any(a => a.AttributeType == typeof(FactAttribute)));
|
|
foreach (var method in methods)
|
|
{
|
|
await RunTest(type, method, method.Name);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MethodInfo methodInfo = type.GetMethod(methodName);
|
|
await RunTest(type, methodInfo, test);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
RunTestCleanup(type);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.ToString());
|
|
returnCode = -1;
|
|
}
|
|
}
|
|
}).Wait();
|
|
|
|
return returnCode;
|
|
}
|
|
|
|
private static async Task RunTest(Type type, MethodInfo methodInfo, string testName)
|
|
{
|
|
if (methodInfo == null)
|
|
{
|
|
Console.WriteLine("Invalid method name");
|
|
}
|
|
else
|
|
{
|
|
using (var typeInstance = (IDisposable)Activator.CreateInstance(type))
|
|
{
|
|
Console.WriteLine("Running test " + testName);
|
|
await (Task)methodInfo.Invoke(typeInstance, null);
|
|
Console.WriteLine("Test ran successfully: " + testName);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void RunTestCleanup(Type type)
|
|
{
|
|
try
|
|
{
|
|
MethodInfo cleanupMethod = type.GetMethod("Cleanup");
|
|
if (cleanupMethod != null)
|
|
{
|
|
cleanupMethod.Invoke(null, null);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(
|
|
"An exception occurred running Cleanup for type {0}: {1}",
|
|
type.FullName,
|
|
e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|