11
11
using Microsoft . CLU . Help ;
12
12
using Microsoft . CLU . Metadata ;
13
13
using Microsoft . CLU . Common . Properties ;
14
+ using Microsoft . ApplicationInsights ;
15
+ using System . Management . Automation . Host ;
14
16
15
17
namespace Microsoft . CLU . CommandBinder
16
18
{
@@ -63,6 +65,7 @@ public CmdletBinderAndCommand(ConfigurationDictionary commandConfiguration, ICom
63
65
_runtime = runtime ;
64
66
_commandConfiguration = commandConfiguration ;
65
67
_staticParameterBindInProgress = true ;
68
+ InitTelemetry ( ) ;
66
69
InitCmdlet ( cmdletValue . LoadCmdlet ( ) , cmdletValue . PackageAssembly . FullPath ) ;
67
70
Action < Type , uint , string > discriminatorBindFinished = ( Type cmdletType , uint seekBackOffset , string fullPath ) =>
68
71
{
@@ -83,6 +86,7 @@ public CmdletBinderAndCommand(ConfigurationDictionary commandConfiguration, ICom
83
86
Debug . Assert ( runtime != null ) ;
84
87
_runtime = runtime ;
85
88
_commandConfiguration = commandConfiguration ;
89
+ InitTelemetry ( ) ;
86
90
Action < Type , uint , string > discriminatorBindFinished = ( Type cmdletType , uint seekBackOffset , string fullPath ) =>
87
91
{
88
92
_staticParameterBindInProgress = true ;
@@ -253,12 +257,18 @@ public void Invoke()
253
257
}
254
258
catch ( CmdletTerminateException terminateException )
255
259
{
260
+ _telemetryClient . TrackException ( terminateException , GetErrorTelemetryProperties ( ) ) ;
256
261
_cmdlet . CommandRuntime . WriteError ( terminateException . ErrorRecord ) ;
257
262
}
258
263
catch ( Exception exception )
259
264
{
265
+ _telemetryClient . TrackException ( exception , GetErrorTelemetryProperties ( ) ) ;
260
266
_cmdlet . CommandRuntime . WriteError ( new ErrorRecord ( exception , "" , ErrorCategory . InvalidResult , _cmdlet ) ) ;
261
267
}
268
+ finally
269
+ {
270
+ _telemetryClient . Flush ( ) ;
271
+ }
262
272
}
263
273
264
274
/// <summary>
@@ -341,6 +351,56 @@ private void InitCmdlet(Type cmdletType, string assemblyLocation)
341
351
_staticParametersBindHandler = new BindHandler ( _cmdlet , _staticParametersBindState ) ;
342
352
}
343
353
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
+
344
404
/// <summary>
345
405
/// Bind the dynamic parameters if cmdlet instance supports dynamic parameters.
346
406
/// </summary>
@@ -533,7 +593,11 @@ private static bool MatchesParameterSet(ParameterMetadata parameter, string para
533
593
return parameterSet == null || parameter . ParameterSets . ContainsKey ( parameterSet ) ;
534
594
}
535
595
536
- #region Private fields
596
+ #region Private fields
597
+ /// <summary>
598
+ /// Telemetry client.
599
+ /// </summary>
600
+ private TelemetryClient _telemetryClient ;
537
601
538
602
/// <summary>
539
603
/// Configuration of the current command.
0 commit comments