1111using Microsoft . CLU . Help ;
1212using Microsoft . CLU . Metadata ;
1313using Microsoft . CLU . Common . Properties ;
14+ using Microsoft . ApplicationInsights ;
15+ using System . Management . Automation . Host ;
1416
1517namespace Microsoft . CLU . CommandBinder
1618{
@@ -63,6 +65,7 @@ public CmdletBinderAndCommand(ConfigurationDictionary commandConfiguration, ICom
6365 _runtime = runtime ;
6466 _commandConfiguration = commandConfiguration ;
6567 _staticParameterBindInProgress = true ;
68+ InitTelemetry ( ) ;
6669 InitCmdlet ( cmdletValue . LoadCmdlet ( ) , cmdletValue . PackageAssembly . FullPath ) ;
6770 Action < Type , uint , string > discriminatorBindFinished = ( Type cmdletType , uint seekBackOffset , string fullPath ) =>
6871 {
@@ -83,6 +86,7 @@ public CmdletBinderAndCommand(ConfigurationDictionary commandConfiguration, ICom
8386 Debug . Assert ( runtime != null ) ;
8487 _runtime = runtime ;
8588 _commandConfiguration = commandConfiguration ;
89+ InitTelemetry ( ) ;
8690 Action < Type , uint , string > discriminatorBindFinished = ( Type cmdletType , uint seekBackOffset , string fullPath ) =>
8791 {
8892 _staticParameterBindInProgress = true ;
@@ -253,12 +257,18 @@ public void Invoke()
253257 }
254258 catch ( CmdletTerminateException terminateException )
255259 {
260+ _telemetryClient . TrackException ( terminateException , GetErrorTelemetryProperties ( ) ) ;
256261 _cmdlet . CommandRuntime . WriteError ( terminateException . ErrorRecord ) ;
257262 }
258263 catch ( Exception exception )
259264 {
265+ _telemetryClient . TrackException ( exception , GetErrorTelemetryProperties ( ) ) ;
260266 _cmdlet . CommandRuntime . WriteError ( new ErrorRecord ( exception , "" , ErrorCategory . InvalidResult , _cmdlet ) ) ;
261267 }
268+ finally
269+ {
270+ _telemetryClient . Flush ( ) ;
271+ }
262272 }
263273
264274 /// <summary>
@@ -341,6 +351,56 @@ private void InitCmdlet(Type cmdletType, string assemblyLocation)
341351 _staticParametersBindHandler = new BindHandler ( _cmdlet , _staticParametersBindState ) ;
342352 }
343353
354+ private IDictionary < string , string > GetErrorTelemetryProperties ( )
355+ {
356+ Dictionary < string , string > eventProperties = new Dictionary < string , string > ( ) ;
357+ eventProperties . Add ( "IsSuccess" , "False" ) ;
358+ if ( _cmdlet != null )
359+ {
360+ eventProperties . Add ( "ModuleName" , _cmdlet . GetType ( ) . GetTypeInfo ( ) . Assembly . GetName ( ) . Name ) ;
361+ eventProperties . Add ( "ModuleVersion" , _cmdlet . GetType ( ) . GetTypeInfo ( ) . Assembly . GetName ( ) . Version . ToString ( ) ) ;
362+ var cmdletAliasAttribute = _cmdlet . GetType ( ) . GetTypeInfo ( ) . GetCustomAttributes ( )
363+ . FirstOrDefault ( ( at ) => at . GetType ( ) . FullName . Equals ( "System.Management.Automation.CliCommandAliasAttribute" ) ) ;
364+
365+ if ( cmdletAliasAttribute != null )
366+ {
367+ var attrType = cmdletAliasAttribute . GetType ( ) ;
368+ eventProperties . Add ( "CommandName" , "az " + ( string ) attrType . GetProperty ( "CommandName" ) . GetValue ( cmdletAliasAttribute ) ) ;
369+ }
370+ }
371+ var _cluHost = _runtime as CLUHost ;
372+ if ( _cluHost != null )
373+ {
374+ eventProperties . Add ( "HostVersion" , _cluHost . Version . ToString ( ) ) ;
375+ eventProperties . Add ( "InputFromPipeline" , _cluHost . IsInputRedirected . ToString ( ) ) ;
376+ eventProperties . Add ( "OutputToPipeline" , _cluHost . IsOutputRedirected . ToString ( ) ) ;
377+ }
378+ if ( CLUEnvironment . Platform . IsMacOSX )
379+ {
380+ eventProperties . Add ( "OS" , "MacOS" ) ;
381+ }
382+ else if ( CLUEnvironment . Platform . IsUnix )
383+ {
384+ eventProperties . Add ( "OS" , "Unix" ) ;
385+ }
386+ else
387+ {
388+ eventProperties . Add ( "OS" , "Windows" ) ;
389+ }
390+ return eventProperties ;
391+ }
392+
393+ /// <summary>
394+ /// Initializes TelemetryClient using default channel.
395+ /// </summary>
396+ private void InitTelemetry ( )
397+ {
398+ _telemetryClient = new TelemetryClient
399+ {
400+ InstrumentationKey = "963c4276-ec20-48ad-b9ab-3968e9da5578"
401+ } ;
402+ }
403+
344404 /// <summary>
345405 /// Bind the dynamic parameters if cmdlet instance supports dynamic parameters.
346406 /// </summary>
@@ -533,7 +593,11 @@ private static bool MatchesParameterSet(ParameterMetadata parameter, string para
533593 return parameterSet == null || parameter . ParameterSets . ContainsKey ( parameterSet ) ;
534594 }
535595
536- #region Private fields
596+ #region Private fields
597+ /// <summary>
598+ /// Telemetry client.
599+ /// </summary>
600+ private TelemetryClient _telemetryClient ;
537601
538602 /// <summary>
539603 /// Configuration of the current command.
0 commit comments