@@ -73,8 +73,8 @@ protected string PSVersion
73
73
get
74
74
{
75
75
if ( string . IsNullOrEmpty ( _psVersion ) )
76
- {
77
- if ( this . Host != null )
76
+ {
77
+ if ( this . Host != null )
78
78
{
79
79
_psVersion = this . Host . Version . ToString ( ) ;
80
80
}
@@ -153,19 +153,33 @@ protected static void InitializeDataCollectionProfile()
153
153
// If it exists, remove the old AzureDataCollectionProfile.json file
154
154
string oldFileFullPath = Path . Combine ( AzurePowerShell . ProfileDirectory ,
155
155
AzurePSDataCollectionProfile . OldDefaultFileName ) ;
156
- if ( AzureSession . Instance . DataStore . FileExists ( oldFileFullPath ) )
156
+ try
157
+ {
158
+ if ( AzureSession . Instance . DataStore . FileExists ( oldFileFullPath ) )
159
+ {
160
+ AzureSession . Instance . DataStore . DeleteFile ( oldFileFullPath ) ;
161
+ }
162
+ }
163
+ catch
157
164
{
158
- AzureSession . Instance . DataStore . DeleteFile ( oldFileFullPath ) ;
165
+ // do not throw if the old file cannot be deleted
159
166
}
160
167
161
168
// Try and read from the new AzurePSDataCollectionProfile.json file
162
169
string fileFullPath = Path . Combine ( AzurePowerShell . ProfileDirectory ,
163
170
AzurePSDataCollectionProfile . DefaultFileName ) ;
164
- if ( AzureSession . Instance . DataStore . FileExists ( fileFullPath ) )
171
+ try
172
+ {
173
+ if ( AzureSession . Instance . DataStore . FileExists ( fileFullPath ) )
174
+ {
175
+ string contents = AzureSession . Instance . DataStore . ReadFileAsText ( fileFullPath ) ;
176
+ _dataCollectionProfile =
177
+ JsonConvert . DeserializeObject < AzurePSDataCollectionProfile > ( contents ) ;
178
+ }
179
+ }
180
+ catch
165
181
{
166
- string contents = AzureSession . Instance . DataStore . ReadFileAsText ( fileFullPath ) ;
167
- _dataCollectionProfile =
168
- JsonConvert . DeserializeObject < AzurePSDataCollectionProfile > ( contents ) ;
182
+ // do not throw if the data collection profile cannot be serialized
169
183
}
170
184
}
171
185
@@ -284,7 +298,7 @@ protected virtual void TearDownDebuggingTraces()
284
298
285
299
protected virtual void SetupHttpClientPipeline ( )
286
300
{
287
- AzureSession . Instance . ClientFactory . UserAgents . Add ( new ProductInfoHeaderValue ( ModuleName , string . Format ( "v{0}" , ModuleVersion ) ) ) ;
301
+ AzureSession . Instance . ClientFactory . UserAgents . Add ( new ProductInfoHeaderValue ( ModuleName , string . Format ( "v{0}" , ModuleVersion ) ) ) ;
288
302
AzureSession . Instance . ClientFactory . UserAgents . Add ( new ProductInfoHeaderValue ( PSVERSION , string . Format ( "v{0}" , PSVersion ) ) ) ;
289
303
290
304
AzureSession . Instance . ClientFactory . AddHandler (
@@ -472,41 +486,48 @@ private void FlushDebugMessages(bool record = false)
472
486
473
487
private void RecordDebugMessages ( )
474
488
{
475
- // Create 'ErrorRecords' folder under profile directory, if not exists
476
- if ( string . IsNullOrEmpty ( _errorRecordFolderPath )
477
- || ! Directory . Exists ( _errorRecordFolderPath ) )
489
+ try
478
490
{
479
- _errorRecordFolderPath = Path . Combine ( AzurePowerShell . ProfileDirectory ,
480
- "ErrorRecords" ) ;
481
- Directory . CreateDirectory ( _errorRecordFolderPath ) ;
482
- }
491
+ // Create 'ErrorRecords' folder under profile directory, if not exists
492
+ if ( string . IsNullOrEmpty ( _errorRecordFolderPath )
493
+ || ! Directory . Exists ( _errorRecordFolderPath ) )
494
+ {
495
+ _errorRecordFolderPath = Path . Combine ( AzurePowerShell . ProfileDirectory ,
496
+ "ErrorRecords" ) ;
497
+ Directory . CreateDirectory ( _errorRecordFolderPath ) ;
498
+ }
483
499
484
- CommandInfo cmd = this . MyInvocation . MyCommand ;
500
+ CommandInfo cmd = this . MyInvocation . MyCommand ;
485
501
486
- string filePrefix = cmd . Name ;
487
- string timeSampSuffix = DateTime . Now . ToString ( _fileTimeStampSuffixFormat ) ;
488
- string fileName = filePrefix + "_" + timeSampSuffix + ".log" ;
489
- string filePath = Path . Combine ( _errorRecordFolderPath , fileName ) ;
502
+ string filePrefix = cmd . Name ;
503
+ string timeSampSuffix = DateTime . Now . ToString ( _fileTimeStampSuffixFormat ) ;
504
+ string fileName = filePrefix + "_" + timeSampSuffix + ".log" ;
505
+ string filePath = Path . Combine ( _errorRecordFolderPath , fileName ) ;
490
506
491
- StringBuilder sb = new StringBuilder ( ) ;
492
- sb . Append ( "Module : " ) . AppendLine ( cmd . ModuleName ) ;
493
- sb . Append ( "Cmdlet : " ) . AppendLine ( cmd . Name ) ;
507
+ StringBuilder sb = new StringBuilder ( ) ;
508
+ sb . Append ( "Module : " ) . AppendLine ( cmd . ModuleName ) ;
509
+ sb . Append ( "Cmdlet : " ) . AppendLine ( cmd . Name ) ;
494
510
495
- sb . AppendLine ( "Parameters" ) ;
496
- foreach ( var item in this . MyInvocation . BoundParameters )
497
- {
498
- sb . Append ( " -" ) . Append ( item . Key ) . Append ( " : " ) ;
499
- sb . AppendLine ( item . Value == null ? "null" : item . Value . ToString ( ) ) ;
500
- }
511
+ sb . AppendLine ( "Parameters" ) ;
512
+ foreach ( var item in this . MyInvocation . BoundParameters )
513
+ {
514
+ sb . Append ( " -" ) . Append ( item . Key ) . Append ( " : " ) ;
515
+ sb . AppendLine ( item . Value == null ? "null" : item . Value . ToString ( ) ) ;
516
+ }
517
+
518
+ sb . AppendLine ( ) ;
501
519
502
- sb . AppendLine ( ) ;
520
+ foreach ( var content in DebugMessages )
521
+ {
522
+ sb . AppendLine ( content ) ;
523
+ }
503
524
504
- foreach ( var content in DebugMessages )
525
+ AzureSession . Instance . DataStore . WriteFile ( filePath , sb . ToString ( ) ) ;
526
+ }
527
+ catch
505
528
{
506
- sb . AppendLine ( content ) ;
529
+ // do not throw an error if recording debug messages fails
507
530
}
508
-
509
- AzureSession . Instance . DataStore . WriteFile ( filePath , sb . ToString ( ) ) ;
510
531
}
511
532
512
533
/// <summary>
0 commit comments