forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
128 lines (108 loc) · 5.02 KB
/
Program.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.ML.CLI.Commands;
using Microsoft.ML.CLI.Commands.New;
using Microsoft.ML.CLI.Data;
using Microsoft.ML.CLI.Telemetry.Events;
using Microsoft.ML.CLI.Utilities;
using NLog;
using NLog.Targets;
namespace Microsoft.ML.CLI
{
class Program
{
private static Logger logger = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
Telemetry.Telemetry.Initialize();
int exitCode = 1;
Exception ex = null;
var stopwatch = Stopwatch.StartNew();
var mlNetCommandEvent = new MLNetCommandEvent();
// Create handler outside so that commandline and the handler is decoupled and testable.
var handler = CommandHandler.Create<NewCommandSettings>(
(options) =>
{
try
{
// Send telemetry event for command issued
mlNetCommandEvent.AutoTrainCommandSettings = options;
mlNetCommandEvent.TrackEvent();
// Map the verbosity to internal levels
var verbosity = Utils.GetVerbosity(options.Verbosity);
// Build the output path
string outputBaseDir = string.Empty;
if (options.Name == null)
{
options.Name = "Sample" + Utils.GetTaskKind(options.MlTask).ToString();
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
}
else
{
outputBaseDir = Path.Combine(options.OutputPath.FullName, options.Name);
}
// Override the output path
options.OutputPath = new DirectoryInfo(outputBaseDir);
// Instantiate the command
var command = new NewCommand(options);
// Override the Logger Configuration
var logconsole = LogManager.Configuration.FindTargetByName("logconsole");
var logfile = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
var logFilePath = Path.Combine(Path.Combine(outputBaseDir, "logs"), "debug_log.txt");
logfile.FileName = logFilePath;
options.LogFilePath = logFilePath;
var config = LogManager.Configuration;
config.AddRule(verbosity, LogLevel.Fatal, logconsole);
// Execute the command
command.Execute();
exitCode = 0;
}
catch (Exception e)
{
ex = e;
logger.Log(LogLevel.Error, e.Message);
logger.Log(LogLevel.Debug, e.ToString());
logger.Log(LogLevel.Info, Strings.LookIntoLogFile);
logger.Log(LogLevel.Error, Strings.Exiting);
}
MLNetCommandEndEvent.TrackEvent(stopwatch.Elapsed, ex);
});
var parser = new CommandLineBuilder()
// parser
.AddCommand(CommandDefinitions.AutoTrain(handler))
.UseDefaults()
.Build();
var parseResult = parser.Parse(args);
var commandParseSucceeded = !parseResult.Errors.Any();
if (commandParseSucceeded)
{
if (parseResult.RootCommandResult.Children.Count > 0)
{
var command = parseResult.RootCommandResult.Children.First();
var parsedArguments = command.Children;
if (parsedArguments.Count > 0)
{
var options = parsedArguments.ToList().Where(sr => sr is System.CommandLine.OptionResult).Cast<System.CommandLine.OptionResult>();
var explicitlySpecifiedOptions = options.Where(opt => !opt.IsImplicit).Select(opt => opt.Name);
mlNetCommandEvent.CommandLineParametersUsed = explicitlySpecifiedOptions;
}
}
}
// Send system info telemetry
SystemInfoEvent.TrackEvent();
parser.InvokeAsync(parseResult).Wait();
// Send exit telemetry
ApplicationExitEvent.TrackEvent(exitCode, commandParseSucceeded, stopwatch.Elapsed, ex);
// Flush pending telemetry logs
Telemetry.Telemetry.Flush(TimeSpan.FromSeconds(5));
Environment.Exit(exitCode);
}
}
}