55using System ;
66using System . CommandLine . Builder ;
77using System . CommandLine . Invocation ;
8+ using System . Diagnostics ;
89using System . IO ;
910using System . Linq ;
10- using Microsoft . DotNet . Cli . Telemetry ;
1111using Microsoft . ML . CLI . Commands ;
1212using Microsoft . ML . CLI . Commands . New ;
1313using Microsoft . ML . CLI . Data ;
14+ using Microsoft . ML . CLI . Telemetry . Events ;
1415using Microsoft . ML . CLI . Utilities ;
1516using NLog ;
1617using NLog . Targets ;
@@ -20,24 +21,33 @@ namespace Microsoft.ML.CLI
2021 public class Program
2122 {
2223 private static Logger _logger = LogManager . GetCurrentClassLogger ( ) ;
24+
2325 public static void Main ( string [ ] args )
2426 {
25- var telemetry = new MlTelemetry ( ) ;
27+ Telemetry . Telemetry . Initialize ( ) ;
2628 int exitCode = 1 ;
29+ Exception ex = null ;
30+ var stopwatch = Stopwatch . StartNew ( ) ;
31+
32+ var mlNetCommandEvent = new MLNetCommandEvent ( ) ;
33+
2734 // Create handler outside so that commandline and the handler is decoupled and testable.
2835 var handler = CommandHandler . Create < NewCommandSettings > (
2936 ( options ) =>
3037 {
3138 try
3239 {
40+ // Send telemetry event for command issued
41+ mlNetCommandEvent . AutoTrainCommandSettings = options ;
42+ mlNetCommandEvent . TrackEvent ( ) ;
43+
3344 // Map the verbosity to internal levels
3445 var verbosity = Utils . GetVerbosity ( options . Verbosity ) ;
3546
3647 // Build the output path
3748 string outputBaseDir = string . Empty ;
3849 if ( options . Name == null )
3950 {
40-
4151 options . Name = "Sample" + Utils . GetTaskKind ( options . MlTask ) . ToString ( ) ;
4252 outputBaseDir = Path . Combine ( options . OutputPath . FullName , options . Name ) ;
4353 }
@@ -50,7 +60,7 @@ public static void Main(string[] args)
5060 options . OutputPath = new DirectoryInfo ( outputBaseDir ) ;
5161
5262 // Instantiate the command
53- var command = new NewCommand ( options , telemetry ) ;
63+ var command = new NewCommand ( options ) ;
5464
5565 // Override the Logger Configuration
5666 var logconsole = LogManager . Configuration . FindTargetByName ( "logconsole" ) ;
@@ -67,6 +77,7 @@ public static void Main(string[] args)
6777 }
6878 catch ( Exception e )
6979 {
80+ ex = e ;
7081 _logger . Log ( LogLevel . Error , e . Message ) ;
7182 _logger . Log ( LogLevel . Debug , e . ToString ( ) ) ;
7283 _logger . Log ( LogLevel . Info , Strings . LookIntoLogFile ) ;
@@ -82,7 +93,8 @@ public static void Main(string[] args)
8293
8394 var parseResult = parser . Parse ( args ) ;
8495
85- if ( parseResult . Errors . Count == 0 )
96+ var commandParseSucceeded = ! parseResult . Errors . Any ( ) ;
97+ if ( commandParseSucceeded )
8698 {
8799 if ( parseResult . RootCommandResult . Children . Count > 0 )
88100 {
@@ -95,13 +107,20 @@ public static void Main(string[] args)
95107
96108 var explicitlySpecifiedOptions = options . Where ( opt => ! opt . IsImplicit ) . Select ( opt => opt . Name ) ;
97109
98- telemetry . SetCommandAndParameters ( command . Name , explicitlySpecifiedOptions ) ;
110+ mlNetCommandEvent . CommandLineParametersUsed = explicitlySpecifiedOptions ;
99111 }
100112 }
101113 }
102114
115+ // Send system info telemetry
116+ SystemInfoEvent . TrackEvent ( ) ;
117+
103118 parser . InvokeAsync ( parseResult ) . Wait ( ) ;
119+ // Send exit telemetry
120+ ApplicationExitEvent . TrackEvent ( exitCode , commandParseSucceeded , stopwatch . Elapsed , ex ) ;
121+ // Flush pending telemetry logs
122+ Telemetry . Telemetry . Flush ( TimeSpan . FromSeconds ( 3 ) ) ;
104123 Environment . Exit ( exitCode ) ;
105124 }
106125 }
107- }
126+ }
0 commit comments