@@ -73,8 +73,8 @@ protected string PSVersion
7373 get
7474 {
7575 if ( string . IsNullOrEmpty ( _psVersion ) )
76- {
77- if ( this . Host != null )
76+ {
77+ if ( this . Host != null )
7878 {
7979 _psVersion = this . Host . Version . ToString ( ) ;
8080 }
@@ -153,19 +153,33 @@ protected static void InitializeDataCollectionProfile()
153153 // If it exists, remove the old AzureDataCollectionProfile.json file
154154 string oldFileFullPath = Path . Combine ( AzurePowerShell . ProfileDirectory ,
155155 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
157164 {
158- AzureSession . Instance . DataStore . DeleteFile ( oldFileFullPath ) ;
165+ // do not throw if the old file cannot be deleted
159166 }
160167
161168 // Try and read from the new AzurePSDataCollectionProfile.json file
162169 string fileFullPath = Path . Combine ( AzurePowerShell . ProfileDirectory ,
163170 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
165181 {
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
169183 }
170184 }
171185
@@ -284,7 +298,7 @@ protected virtual void TearDownDebuggingTraces()
284298
285299 protected virtual void SetupHttpClientPipeline ( )
286300 {
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 ) ) ) ;
288302 AzureSession . Instance . ClientFactory . UserAgents . Add ( new ProductInfoHeaderValue ( PSVERSION , string . Format ( "v{0}" , PSVersion ) ) ) ;
289303
290304 AzureSession . Instance . ClientFactory . AddHandler (
@@ -472,41 +486,48 @@ private void FlushDebugMessages(bool record = false)
472486
473487 private void RecordDebugMessages ( )
474488 {
475- // Create 'ErrorRecords' folder under profile directory, if not exists
476- if ( string . IsNullOrEmpty ( _errorRecordFolderPath )
477- || ! Directory . Exists ( _errorRecordFolderPath ) )
489+ try
478490 {
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+ }
483499
484- CommandInfo cmd = this . MyInvocation . MyCommand ;
500+ CommandInfo cmd = this . MyInvocation . MyCommand ;
485501
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 ) ;
490506
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 ) ;
494510
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 ( ) ;
501519
502- sb . AppendLine ( ) ;
520+ foreach ( var content in DebugMessages )
521+ {
522+ sb . AppendLine ( content ) ;
523+ }
503524
504- foreach ( var content in DebugMessages )
525+ AzureSession . Instance . DataStore . WriteFile ( filePath , sb . ToString ( ) ) ;
526+ }
527+ catch
505528 {
506- sb . AppendLine ( content ) ;
529+ // do not throw an error if recording debug messages fails
507530 }
508-
509- AzureSession . Instance . DataStore . WriteFile ( filePath , sb . ToString ( ) ) ;
510531 }
511532
512533 /// <summary>
0 commit comments