From ec58592b8a3cee29e615320edcb198dab44b5d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vysok=C3=BD?= Date: Mon, 6 Jan 2025 13:54:52 +0100 Subject: [PATCH] Log work item start/end/exception using current's scope logger (#4299) --- .../WorkItemScope.cs | 16 +++++++++++++--- .../WorkItemScopeManager.cs | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ProductConstructionService/ProductConstructionService.WorkItems/WorkItemScope.cs b/src/ProductConstructionService/ProductConstructionService.WorkItems/WorkItemScope.cs index bfb56add63..71dabec87d 100644 --- a/src/ProductConstructionService/ProductConstructionService.WorkItems/WorkItemScope.cs +++ b/src/ProductConstructionService/ProductConstructionService.WorkItems/WorkItemScope.cs @@ -64,10 +64,20 @@ async Task ProcessWorkItemAsync() using (var operation = telemetryClient.StartOperation(type)) using (logger.BeginScope(processor.GetLoggingContextData(workItem))) { - var success = await processor.ProcessWorkItemAsync(workItem, cancellationToken); - if (success) + try { - telemetryScope.SetSuccess(); + logger.LogInformation("Processing work item {type}", type); + var success = await processor.ProcessWorkItemAsync(workItem, cancellationToken); + if (success) + { + telemetryScope.SetSuccess(); + } + } + catch (Exception e) + { + operation.Telemetry.Success = false; + logger.LogError(e, "Failed to process work item {type}", type); + throw; } } } diff --git a/src/ProductConstructionService/ProductConstructionService.WorkItems/WorkItemScopeManager.cs b/src/ProductConstructionService/ProductConstructionService.WorkItems/WorkItemScopeManager.cs index 15632d52ce..8e42416a79 100644 --- a/src/ProductConstructionService/ProductConstructionService.WorkItems/WorkItemScopeManager.cs +++ b/src/ProductConstructionService/ProductConstructionService.WorkItems/WorkItemScopeManager.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Microsoft.ApplicationInsights; using Microsoft.DotNet.DarcLib; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options;