From a75b62b57279073adb4fd12ea63a29767157d427 Mon Sep 17 00:00:00 2001 From: Jacob Viau Date: Wed, 8 Nov 2023 11:11:53 -0800 Subject: [PATCH] Add operation error log (#236) --- src/Worker/Core/Logs.cs | 4 ++++ src/Worker/Core/Shims/DurableTaskShimFactory.cs | 3 ++- src/Worker/Core/Shims/TaskEntityShim.cs | 7 ++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Worker/Core/Logs.cs b/src/Worker/Core/Logs.cs index c76616d4..6b7a6c1e 100644 --- a/src/Worker/Core/Logs.cs +++ b/src/Worker/Core/Logs.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +using Microsoft.DurableTask.Entities; using Microsoft.Extensions.Logging; namespace Microsoft.DurableTask @@ -13,6 +14,9 @@ namespace Microsoft.DurableTask /// static partial class Logs { + [LoggerMessage(EventId = 15, Level = LogLevel.Error, Message = "Unhandled exception in entity operation {entityInstanceId}/{operationName}.")] + public static partial void OperationError(this ILogger logger, Exception ex, EntityInstanceId entityInstanceId, string operationName); + [LoggerMessage(EventId = 55, Level = LogLevel.Information, Message = "{instanceId}: Evaluating custom retry handler for failed '{name}' task. Attempt = {attempt}.")] public static partial void RetryingTask(this ILogger logger, string instanceId, string name, int attempt); } diff --git a/src/Worker/Core/Shims/DurableTaskShimFactory.cs b/src/Worker/Core/Shims/DurableTaskShimFactory.cs index 26e33cb7..c8182be3 100644 --- a/src/Worker/Core/Shims/DurableTaskShimFactory.cs +++ b/src/Worker/Core/Shims/DurableTaskShimFactory.cs @@ -127,6 +127,7 @@ public TaskEntity CreateEntity(TaskName name, ITaskEntity entity, EntityId entit // For now, we simply create a new shim for each entity batch operation. // In the future we may consider caching those shims and reusing them, which can reduce // deserialization and allocation overheads. - return new TaskEntityShim(this.options.DataConverter, entity, entityId); + ILogger logger = this.loggerFactory.CreateLogger(entity.GetType()); + return new TaskEntityShim(this.options.DataConverter, entity, entityId, logger); } } diff --git a/src/Worker/Core/Shims/TaskEntityShim.cs b/src/Worker/Core/Shims/TaskEntityShim.cs index 50d41f95..800b42bb 100644 --- a/src/Worker/Core/Shims/TaskEntityShim.cs +++ b/src/Worker/Core/Shims/TaskEntityShim.cs @@ -22,6 +22,7 @@ class TaskEntityShim : DTCore.Entities.TaskEntity readonly StateShim state; readonly ContextShim context; readonly OperationShim operation; + readonly ILogger logger; /// /// Initializes a new instance of the class. @@ -29,7 +30,9 @@ class TaskEntityShim : DTCore.Entities.TaskEntity /// The data converter. /// The task entity. /// The entity ID. - public TaskEntityShim(DataConverter dataConverter, ITaskEntity taskEntity, EntityId entityId) + /// The logger. + public TaskEntityShim( + DataConverter dataConverter, ITaskEntity taskEntity, EntityId entityId, ILogger logger) { this.dataConverter = Check.NotNull(dataConverter); this.taskEntity = Check.NotNull(taskEntity); @@ -37,6 +40,7 @@ public TaskEntityShim(DataConverter dataConverter, ITaskEntity taskEntity, Entit this.state = new StateShim(dataConverter); this.context = new ContextShim(this.entityId, dataConverter); this.operation = new OperationShim(this); + this.logger = logger; } /// @@ -68,6 +72,7 @@ public override async Task ExecuteOperationBatchAsync(EntityB } catch (Exception applicationException) { + this.logger.OperationError(applicationException, this.entityId, current.Operation!); results.Add(new OperationResult() { FailureDetails = new FailureDetails(applicationException),