-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathProgram.cs
More file actions
192 lines (166 loc) · 6.7 KB
/
Copy pathProgram.cs
File metadata and controls
192 lines (166 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using CommandLine;
using CommandLine.Text;
using NLog;
using NLog.Targets;
using System.Diagnostics;
using System.Runtime;
namespace ConvertWorkload
{
class Program
{
private static Logger logger = LogManager.GetCurrentClassLogger();
private static CancellationTokenSource source;
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(GenericErrorHandler);
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
var version = fvi.FileMajorPart.ToString() + "." + fvi.FileMinorPart.ToString() + "." + fvi.FileBuildPart.ToString();
var name = assembly.FullName;
logger.Info(name + " " + version);
try
{
var options = new Options();
var result = Parser.Default.ParseArguments<Options>(args);
result
.WithParsed(parsedOptions => options = parsedOptions)
.WithNotParsed(errors =>
{
foreach (var error in errors)
{
logger.Error(error.ToString());
}
var helpText = HelpText.AutoBuild(result, h =>
{
h.AdditionalNewLineAfterOption = false;
return h;
}, e => e);
Console.WriteLine(helpText);
Environment.Exit(1);
});
Run(options);
}
catch (Exception e)
{
logger.Error(e);
}
}
private static void Run(Options options)
{
// reconfigure loggers to use a file in the current directory
// or the file specified by the "Log" commandline parameter
if(LogManager.Configuration != null)
{
var target = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
if (target != null)
{
var pathToLog = options.LogFile;
if (pathToLog == null)
{
pathToLog = Path.Combine(Environment.CurrentDirectory, "ConvertWorkload.log");
}
if (!Path.IsPathRooted(pathToLog))
{
pathToLog = Path.Combine(Environment.CurrentDirectory, pathToLog);
}
target.FileName = pathToLog;
LogManager.ReconfigExistingLoggers();
}
}
// check whether localdb is installed
logger.Info("Checking LocalDB...");
var manager = new LocalDBManager();
if (!manager.CanConnectToLocalDB())
{
logger.Info("Installing LocalDB...");
try
{
manager.InstallLocalDB();
}
catch (InvalidOperationException)
{
logger.Error("This operation requires elevation. Restart the application as an administrator.");
return;
}
}
EventReader reader = null;
if (options.InputFile.EndsWith(".trc"))
{
reader = new SqlTraceEventReader(options.InputFile);
}
else
{
reader = new ExtendedEventsEventReader(options.InputFile);
}
EventWriter writer = new WorkloadFileEventWriter(options.OutputFile);
var converter = new WorkloadConverter(reader, writer);
if(options.ApplicationFilter != null)
{
converter.ApplicationFilter = new string[1] { options.ApplicationFilter };
}
if (options.DatabaseFilter != null)
{
converter.DatabaseFilter = new string[1] { options.DatabaseFilter };
}
if (options.HostFilter != null)
{
converter.HostFilter = new string[1] { options.HostFilter };
}
if (options.LoginFilter != null)
{
converter.LoginFilter = new string[1] { options.LoginFilter };
}
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e) {
e.Cancel = true;
logger.Info("Received shutdown signal...");
source.CancelAfter(TimeSpan.FromSeconds(10)); // give a 10 seconds cancellation grace period
converter.Stop();
};
var t = processConverter(converter);
t.Wait();
logger.Info("Converter stopped.");
}
public static void CancelNotification()
{
logger.Info("Shutdown complete.");
}
public static async Task processConverter(WorkloadConverter converter)
{
source = new CancellationTokenSource();
source.Token.Register(CancelNotification);
var completionSource = new TaskCompletionSource<object>();
source.Token.Register(() => completionSource.TrySetCanceled());
var task = Task.Factory.StartNew(() => converter.Convert(), source.Token);
await Task.WhenAny(task, completionSource.Task);
}
static void GenericErrorHandler(object sender, UnhandledExceptionEventArgs e)
{
try
{
logger.Error(e.ToString());
}
finally
{
Console.WriteLine("Caught unhandled exception...");
}
}
}
class Options
{
[Option('L', "Log", HelpText = "Log file")]
public string LogFile { get; set; }
[Option('I', "Input", HelpText = "Input file", Required = true)]
public string InputFile { get; set; }
[Option('O', "Output", HelpText = "Output file", Required = true)]
public string OutputFile { get; set; }
[Option('A', "ApplicationFilter", HelpText = "Application filter")]
public string ApplicationFilter { get; set; }
[Option('D', "DatabaseFilter", HelpText = "Database filter")]
public string DatabaseFilter { get; set; }
[Option('H', "HostFilter", HelpText = "Host Filter")]
public string HostFilter { get; set; }
[Option('U', "LoginFilter", HelpText = "Login Filter")]
public string LoginFilter { get; set; }
}
}