Files
sqltoolsservice/test/ScriptGenerator/Program.cs
ranasaria 09652cccd1 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
2018-09-24 23:55:59 -07:00

58 lines
3.5 KiB
C#

using ScriptGenerator.Properties;
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace ScriptGenerator
{
class Program
{
internal const string DefaultFileExtension = "Sql";
static readonly Regex ExtractDbName = new Regex(@"CREATE\s+DATABASE\s+\[(?<dbName>\w+)\]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static readonly Regex CreateTableRegex = new Regex(@"(?<begin>CREATE\s+TABLE\s+.*)(?<middle>\](?![\.])[\s\S]*?CONSTRAINT\s+\[\w+?)(?<end>\](?![\.]))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static readonly Regex AlterTableConstraintRegex = new Regex(@"(?<begin>ALTER\s+TABLE\s+.*?)(?<middle>\](?![\.])\s*\b(?:ADD|WITH|CHECK)\b[\s\S]*?CONSTRAINT\s+\[\w+?)(?<end>\](?![\.]))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static readonly Regex CreateViewRegex = new Regex(@"(?<begin>CREATE\s+VIEW\s+.*?)(?<end>\](?![\.])[\s\S]*?\bAS\b)", RegexOptions.Compiled | RegexOptions.IgnoreCase );
static readonly Regex CreateProcedureRegex = new Regex(@"(?<begin>CREATE\s+PROCEDURE\s+.*?)(?<end>\](?![\.])[\s\S]*?(?:@|WITH|AS))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static void Main(string[] args)
{
CommandOptions options = new CommandOptions(args);
for (int d = 1; d <= options.Databases; d++)
{
using (StreamWriter writer = new StreamWriter(Path.ChangeExtension($@"{options.FilePathPrefix}{d}", DefaultFileExtension)))
{
string oldDbName = ExtractDbName.Match(Resources.AdventureWorks).Groups["dbName"].Value;
string newDbName = $@"{oldDbName}{d}";
//Turn Off Strict Clr Mode
writer.WriteLine(Resources.TurnOffClrStrictSecurityMode);
//Put all original objects in the 'newDbName' database
writer.WriteLine(Resources.AdventureWorks.Replace(oldDbName, newDbName));
// Multiple copies of Create Table statements
for (int t = 1; t <= options.TablesMultiplier; t++)
{
string tablesScript = Resources.AdventureWorksTablesCreate.Replace(oldDbName, newDbName);
tablesScript = CreateTableRegex.Replace(tablesScript, "${begin}" + t + "${middle}" + t + "${end}");
writer.WriteLine(AlterTableConstraintRegex.Replace(tablesScript, "${begin}"+ t + "${middle}" + t + "${end}"));
}
// Multiple copies of Create View statements
for (int v = 1; v <= options.ViewsMultiplier; v++)
{
string viewScript = Resources.AdventureWorksViewsCreate.Replace(oldDbName, newDbName);
writer.WriteLine(CreateViewRegex.Replace(viewScript, "${begin}" + v + "${end}"));
}
// Multiple copies of Create Procedure statements
for (int s = 1; s <= options.StoredProceduresMultiplier; s++)
{
string spScript = Resources.AdventureWorksStoredProceduresCreate.Replace(oldDbName, newDbName);
writer.WriteLine(CreateProcedureRegex.Replace(spScript, "${begin}" + s + "${end}"));
}
//Turn On Strict Clr Mode
writer.WriteLine(Resources.TurnOnClrStrictSecurityMode);
}
}
}
}
}