private static void CreateErrorLogFile(Exception exception) { string currentDirectory = Directory.GetCurrentDirectory(); Console.WriteLine($"{nameof(currentDirectory)}: {currentDirectory}"); string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string errorLogFileName = $"DevVmPowerShellModule_ErrorLog_{currentDateTime}.txt"; string errorLogFileFullPath = Path.Combine(currentDirectory, errorLogFileName); //Delete File if it already exists if (File.Exists(errorLogFileFullPath)) { File.Delete(errorLogFileFullPath); } //Create File using (StreamWriter streamWriter = File.CreateText(errorLogFileFullPath)) { string errorMessage = exception.Message; string completeErrorMessage = ErrorMessageHelper.FormatErrorMessage(exception); string stackTrace = exception.StackTrace; streamWriter.WriteLine($"ERROR MESSAGE:\n{errorMessage}"); streamWriter.WriteLine(); streamWriter.WriteLine($"COMPLETE ERROR MESSAGE:\n{completeErrorMessage}"); streamWriter.WriteLine(); streamWriter.WriteLine($"STACK TRACE:\n{stackTrace}"); } }
protected override void EndProcessing() { try { WriteVerbose($"Start - {nameof(EndProcessing)} method"); base.EndProcessing(); EndProcessingCode(); } catch (Exception ex) { string formattedExceptionMessage = ErrorMessageHelper.FormatErrorMessage(ex); Exception exception = new Exception($"An error occured in {nameof(EndProcessing)} method. ErrorMessage: {formattedExceptionMessage}. StackTrace: {ex}", ex); string errorId = $"{nameof(EndProcessing)}"; const ErrorCategory errorCategory = ErrorCategory.DeviceError; ErrorRecord errorRecord = new ErrorRecord(exception, errorId, errorCategory, null); WriteError(errorRecord); } finally { WriteVerbose($"End - {nameof(EndProcessing)} method"); } }
protected override void ProcessRecord() { try { WriteVerbose($"Start - {nameof(ProcessRecord)} method"); base.ProcessRecord(); RetryProcessRecordCode(ProcessRecordCode, TimeSpan.FromSeconds(Constants.Module.TimeInSecondsBetweenRetry), Constants.Module.MaxAttemptCount); } catch (Exception ex) { CreateErrorLogFile(ex); string formattedExceptionMessage = ErrorMessageHelper.FormatErrorMessage(ex); Exception exception = new Exception($"An error occured in {nameof(ProcessRecord)} method. ErrorMessage: {formattedExceptionMessage}. StackTrace: {ex}", ex); string errorId = $"{nameof(ProcessRecord)}"; const ErrorCategory errorCategory = ErrorCategory.DeviceError; ErrorRecord errorRecord = new ErrorRecord(exception, errorId, errorCategory, null); WriteError(errorRecord); } finally { WriteVerbose($"End - {nameof(ProcessRecord)} method"); } }