Skip to content

Commit

Permalink
Fix exception logging (#2578)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkbennett authored Apr 10, 2024
1 parent 05ddcd8 commit 79ed0a9
Show file tree
Hide file tree
Showing 98 changed files with 217 additions and 222 deletions.
4 changes: 2 additions & 2 deletions HyperVExtension/src/DevSetupAgent/DevAgentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ protected async override Task ExecuteAsync(CancellationToken stoppingToken)
}
catch (Exception ex)
{
_log.Error($"Exception in DevAgentService.", ex);
_log.Error(ex, $"Exception in DevAgentService.");
}
}
}
catch (Exception ex)
{
_log.Error($"Failed to run DevSetupAgent.", ex);
_log.Error(ex, $"Failed to run DevSetupAgent.");
throw;
}
finally
Expand Down
8 changes: 4 additions & 4 deletions HyperVExtension/src/DevSetupAgent/HostRegistryChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ await Task.Run(
}
catch (Exception ex)
{
_log.Error($"Could not write host message. Response ID: {responseMessage.ResponseId}", ex);
_log.Error(ex, $"Could not write host message. Response ID: {responseMessage.ResponseId}");
}
},
stoppingToken);
Expand All @@ -130,7 +130,7 @@ await Task.Run(
}
catch (Exception ex)
{
_log.Error($"Could not delete host message. Response ID: {responseId}", ex);
_log.Error(ex, $"Could not delete host message. Response ID: {responseId}");
}
},
stoppingToken);
Expand Down Expand Up @@ -207,7 +207,7 @@ private RequestMessage TryReadMessage()
}
catch (Exception ex)
{
_log.Error($"Could not read host message {valueName}", ex);
_log.Error(ex, $"Could not read host message {valueName}");
}

MessageHelper.DeleteAllMessages(_registryHiveKey, _fromHostRegistryKeyPath, s[0]);
Expand All @@ -218,7 +218,7 @@ private RequestMessage TryReadMessage()
}
catch (Exception ex)
{
_log.Error("Could not read host message.", ex);
_log.Error(ex, "Could not read host message.");
}

return requestMessage;
Expand Down
4 changes: 2 additions & 2 deletions HyperVExtension/src/DevSetupAgent/RegistryWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ public void Start()
}
catch (Exception ex)
{
_log.Error("RegistryChanged delegate failed.", ex);
_log.Error(ex, "RegistryChanged delegate failed.");
}
}
}
}
catch (Exception ex)
{
_log.Error("Registry Watcher thread failed.", ex);
_log.Error(ex, "Registry Watcher thread failed.");
}
});
_log.Information("Registry Watcher thread started.");
Expand Down
2 changes: 1 addition & 1 deletion HyperVExtension/src/DevSetupAgent/RequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void ProcessRequestQueue(CancellationToken stoppingToken)
}
catch (Exception ex)
{
_log.Error($"Failed to execute request.", ex);
_log.Error(ex, $"Failed to execute request.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public IHostRequest CreateRequest(IRequestContext requestContext)
{
var messageId = requestContext.RequestMessage.RequestId ?? "<unknown>";
var requestData = requestContext.RequestMessage.RequestData ?? "<unknown>";
_log.Error($"Error processing message. Message ID: {messageId}. Request data: {requestData}", ex);
_log.Error(ex, $"Error processing message. Message ID: {messageId}. Request data: {requestData}");
return new ErrorRequest(requestContext.RequestMessage);
}
}
Expand Down
9 changes: 4 additions & 5 deletions HyperVExtension/src/DevSetupEngine/ConfigurationFileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,21 @@ private void LogConfigurationDiagnostics(WinGet.IDiagnosticInformation diagnosti
{
_log.Information($"WinGet: {diagnosticInformation.Message}");

var sourceComponent = nameof(WinGet.ConfigurationProcessor);
switch (diagnosticInformation.Level)
{
case WinGet.DiagnosticLevel.Warning:
_log.Warning(sourceComponent, diagnosticInformation.Message);
_log.Warning(diagnosticInformation.Message);
return;
case WinGet.DiagnosticLevel.Error:
_log.Error(sourceComponent, diagnosticInformation.Message);
_log.Error(diagnosticInformation.Message);
return;
case WinGet.DiagnosticLevel.Critical:
_log.Fatal(sourceComponent, diagnosticInformation.Message);
_log.Fatal(diagnosticInformation.Message);
return;
case WinGet.DiagnosticLevel.Verbose:
case WinGet.DiagnosticLevel.Informational:
default:
_log.Information(sourceComponent, diagnosticInformation.Message);
_log.Information(diagnosticInformation.Message);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public PackageOperationException(ErrorCode errorCode, string message)
{
HResult = (int)errorCode;
var log = Log.ForContext("SourceContext", nameof(PackageOperationException));
log.Error(message, this);
log.Error(this, message);
}
}
2 changes: 1 addition & 1 deletion HyperVExtension/src/DevSetupEngine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static int Main([System.Runtime.InteropServices.WindowsRuntime.ReadOnlyAr
}
catch (Exception ex)
{
Log.Error($"Exception: {ex}", ex);
Log.Error(ex, $"Exception: {ex}");
Log.CloseAndFlush();
return ex.HResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static Dictionary<string, string> MergeMessageParts(Dictionary<string, st
catch (Exception ex)
{
var log = Serilog.Log.ForContext("SourceContext", nameof(MessageHelper));
log.Error($"Could not read guest message {valueName}", ex);
log.Error(ex, $"Could not read guest message {valueName}");
}

messages.Add(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public IGuestResponse CreateResponse(IResponseMessage message)
{
var messageId = message?.ResponseId ?? "<unknown>";
var responseData = message?.ResponseData ?? "<unknown>";
_log.Error($"Error processing message. Message ID: {messageId}. Request data: {responseData}", ex);
_log.Error(ex, $"Error processing message. Message ID: {messageId}. Request data: {responseData}");
return new ErrorResponse(message!);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public PsObjectHelper(in PSObject pSObject)
catch (Exception ex)
{
var log = Log.ForContext("SourceContext", nameof(PsObjectHelper));
log.Error($"Failed to get property value with name {propertyName} from object with type {type}.", ex);
log.Error(ex, $"Failed to get property value with name {propertyName} from object with type {type}.");
}

return default(T);
Expand Down
2 changes: 1 addition & 1 deletion HyperVExtension/src/HyperVExtension/Helpers/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static string GetResource(string identifier, ILogger? log = null)
}
catch (Exception ex)
{
log?.Error($"Failed loading resource: {identifier}", ex);
log?.Error(ex, $"Failed loading resource: {identifier}");

// If we fail, load the original identifier so it is obvious which resource is missing.
return identifier;
Expand Down
2 changes: 1 addition & 1 deletion HyperVExtension/src/HyperVExtension/HyperVExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public HyperVExtension(IHost host)
}
catch (Exception ex)
{
log.Error($"Failed to get provider for provider type {providerType}", ex);
log.Error(ex, $"Failed to get provider for provider type {providerType}");
}

return provider;
Expand Down
28 changes: 14 additions & 14 deletions HyperVExtension/src/HyperVExtension/Models/HyperVVirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private ComputeSystemOperationResult Start(string options)
catch (Exception ex)
{
StateChanged(this, ComputeSystemState.Unknown);
_log.Error(OperationErrorString(ComputeSystemOperations.Start), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.Start));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public IAsyncOperation<ComputeSystemOperationResult> TerminateAsync(string optio
catch (Exception ex)
{
StateChanged(this, ComputeSystemState.Unknown);
_log.Error(OperationErrorString(ComputeSystemOperations.Terminate), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.Terminate));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand All @@ -278,7 +278,7 @@ public IAsyncOperation<ComputeSystemOperationResult> DeleteAsync(string options)
catch (Exception ex)
{
StateChanged(this, ComputeSystemState.Unknown);
_log.Error(OperationErrorString(ComputeSystemOperations.Delete), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.Delete));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand Down Expand Up @@ -309,7 +309,7 @@ public IAsyncOperation<ComputeSystemOperationResult> SaveAsync(string options)
catch (Exception ex)
{
StateChanged(this, ComputeSystemState.Unknown);
_log.Error(OperationErrorString(ComputeSystemOperations.Save), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.Save));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand Down Expand Up @@ -340,7 +340,7 @@ public IAsyncOperation<ComputeSystemOperationResult> PauseAsync(string options)
catch (Exception ex)
{
StateChanged(this, ComputeSystemState.Unknown);
_log.Error(OperationErrorString(ComputeSystemOperations.Pause), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.Pause));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand Down Expand Up @@ -371,7 +371,7 @@ public IAsyncOperation<ComputeSystemOperationResult> ResumeAsync(string options)
catch (Exception ex)
{
StateChanged(this, ComputeSystemState.Unknown);
_log.Error(OperationErrorString(ComputeSystemOperations.Resume), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.Resume));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand All @@ -393,7 +393,7 @@ public IAsyncOperation<ComputeSystemOperationResult> CreateSnapshotAsync(string
}
catch (Exception ex)
{
_log.Error(OperationErrorString(ComputeSystemOperations.CreateSnapshot), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.CreateSnapshot));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand All @@ -416,7 +416,7 @@ public IAsyncOperation<ComputeSystemOperationResult> RevertSnapshotAsync(string
}
catch (Exception ex)
{
_log.Error(OperationErrorString(ComputeSystemOperations.RevertSnapshot), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.RevertSnapshot));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand All @@ -439,7 +439,7 @@ public IAsyncOperation<ComputeSystemOperationResult> DeleteSnapshotAsync(string
}
catch (Exception ex)
{
_log.Error(OperationErrorString(ComputeSystemOperations.DeleteSnapshot), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.DeleteSnapshot));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand All @@ -457,7 +457,7 @@ public IAsyncOperation<ComputeSystemOperationResult> ConnectAsync(string options
}
catch (Exception ex)
{
_log.Error($"Failed to launch vmconnect on {DateTime.Now}: VM details: {this}", ex);
_log.Error(ex, $"Failed to launch vmconnect on {DateTime.Now}: VM details: {this}");
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand Down Expand Up @@ -487,7 +487,7 @@ public IAsyncOperation<ComputeSystemOperationResult> RestartAsync(string options
catch (Exception ex)
{
StateChanged(this, ComputeSystemState.Unknown);
_log.Error(OperationErrorString(ComputeSystemOperations.Restart), ex);
_log.Error(ex, OperationErrorString(ComputeSystemOperations.Restart));
return new ComputeSystemOperationResult(ex, OperationErrorUnknownString, ex.Message);
}
}).AsAsyncOperation();
Expand Down Expand Up @@ -536,7 +536,7 @@ public IAsyncOperation<IEnumerable<ComputeSystemProperty>> GetComputeSystemPrope
}
catch (Exception ex)
{
_log.Error($"Failed to GetComputeSystemPropertiesAsync on {DateTime.Now}: VM details: {this}", ex);
_log.Error(ex, $"Failed to GetComputeSystemPropertiesAsync on {DateTime.Now}: VM details: {this}");
return new List<ComputeSystemProperty>();
}
}).AsAsyncOperation();
Expand Down Expand Up @@ -708,7 +708,7 @@ public SDK.ApplyConfigurationResult ApplyConfiguration(ApplyConfigurationOperati
}
catch (Exception ex)
{
_log.Error($"Failed to apply configuration on {DateTime.Now}: VM details: {this}", ex);
_log.Error(ex, $"Failed to apply configuration on {DateTime.Now}: VM details: {this}");
return operation.CompleteOperation(new HostGuestCommunication.ApplyConfigurationResult(ex.HResult, ex.Message));
}
}
Expand All @@ -721,7 +721,7 @@ public SDK.ApplyConfigurationResult ApplyConfiguration(ApplyConfigurationOperati
}
catch (Exception ex)
{
_log.Error($"Failed to apply configuration on {DateTime.Now}: VM details: {this}", ex);
_log.Error(ex, $"Failed to apply configuration on {DateTime.Now}: VM details: {this}");
return new ApplyConfigurationOperation(this, ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void Report(IOperationReport value)
}
catch (Exception ex)
{
_log.Error("Operation to create compute system failed", ex);
_log.Error(ex, "Operation to create compute system failed");
ComputeSystemResult = new CreateComputeSystemResult(ex, ex.Message, ex.Message);
}
Expand All @@ -161,7 +161,7 @@ private void UpdateProgress(IOperationReport report, string localizedKey, string
}
catch (Exception ex)
{
_log.Error("Failed to update progress", ex);
_log.Error(ex, "Failed to update progress");
}
}

Expand All @@ -173,7 +173,7 @@ private void UpdateProgress(string localizedString, uint percentage = 0u)
}
catch (Exception ex)
{
_log.Error("Failed to update progress", ex);
_log.Error(ex, "Failed to update progress");
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ private async Task DeleteFileIfExists(StorageFile file)
}
catch (Exception ex)
{
_log.Error($"Failed to delete file {file.Path}", ex);
_log.Error(ex, $"Failed to delete file {file.Path}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public IAsyncOperation<ProviderOperationResult> OnAction(string action, string i
}
catch (Exception ex)
{
_log.Error($"Exception in OnAction: {ex}");
_log.Error(ex, $"Exception in OnAction: {ex}");
operationResult = new ProviderOperationResult(ProviderOperationStatus.Failure, ex, "Something went wrong", ex.Message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public IAsyncOperation<ProviderOperationResult> OnAction(string action, string i
}
catch (Exception ex)
{
_log.Error($"Exception in OnAction: {ex}");
_log.Error(ex, $"Exception in OnAction: {ex}");
operationResult = new ProviderOperationResult(ProviderOperationStatus.Failure, ex, "Something went wrong", ex.Message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public IAsyncOperation<ComputeSystemsResult> GetComputeSystemsAsync(IDeveloperId
}
catch (Exception ex)
{
_log.Error($"Failed to retrieved all virtual machines on: {DateTime.Now}", ex);
_log.Error(ex, $"Failed to retrieved all virtual machines on: {DateTime.Now}");
return new ComputeSystemsResult(ex, OperationErrorString, ex.Message);
}
}).AsAsyncOperation();
Expand Down Expand Up @@ -105,7 +105,7 @@ public ComputeSystemAdaptiveCardResult CreateAdaptiveCardSessionForComputeSystem
}
catch (Exception ex)
{
_log.Error($"Failed to create a new virtual machine on: {DateTime.Now}", ex);
_log.Error(ex, $"Failed to create a new virtual machine on: {DateTime.Now}");

// Dev Home will handle null values as failed operations. We can't throw because this is an out of proc
// COM call, so we'll lose the error information. We'll log the error and return null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public PowerShellResult Execute(IEnumerable<PowerShellCommandlineStatement> comm
catch (Exception ex)
{
var commandStrings = string.Join(Environment.NewLine, commandLineStatements.Select(cmd => cmd.ToString()));
_log.Error($"Error running PowerShell commands: {commandStrings}", ex);
_log.Error(ex, $"Error running PowerShell commands: {commandStrings}");
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public async Task<VMGalleryImageList> GetGalleryImagesAsync()
}
catch (Exception ex)
{
_log.Error($"Unable to retrieve VM gallery images", ex);
_log.Error(ex, $"Unable to retrieve VM gallery images");
}

return _imageList;
Expand Down
Loading

0 comments on commit 79ed0a9

Please sign in to comment.