Skip to content

Commit

Permalink
Add operation error log (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
jviau authored Nov 8, 2023
1 parent d61b4c0 commit a75b62b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Worker/Core/Logs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.DurableTask.Entities;
using Microsoft.Extensions.Logging;

namespace Microsoft.DurableTask
Expand All @@ -13,6 +14,9 @@ namespace Microsoft.DurableTask
/// </remarks>
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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Worker/Core/Shims/DurableTaskShimFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 6 additions & 1 deletion src/Worker/Core/Shims/TaskEntityShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ class TaskEntityShim : DTCore.Entities.TaskEntity
readonly StateShim state;
readonly ContextShim context;
readonly OperationShim operation;
readonly ILogger logger;

/// <summary>
/// Initializes a new instance of the <see cref="TaskEntityShim"/> class.
/// </summary>
/// <param name="dataConverter">The data converter.</param>
/// <param name="taskEntity">The task entity.</param>
/// <param name="entityId">The entity ID.</param>
public TaskEntityShim(DataConverter dataConverter, ITaskEntity taskEntity, EntityId entityId)
/// <param name="logger">The logger.</param>
public TaskEntityShim(
DataConverter dataConverter, ITaskEntity taskEntity, EntityId entityId, ILogger logger)
{
this.dataConverter = Check.NotNull(dataConverter);
this.taskEntity = Check.NotNull(taskEntity);
this.entityId = new EntityInstanceId(entityId.Name, entityId.Key);
this.state = new StateShim(dataConverter);
this.context = new ContextShim(this.entityId, dataConverter);
this.operation = new OperationShim(this);
this.logger = logger;
}

/// <inheritdoc />
Expand Down Expand Up @@ -68,6 +72,7 @@ public override async Task<EntityBatchResult> ExecuteOperationBatchAsync(EntityB
}
catch (Exception applicationException)
{
this.logger.OperationError(applicationException, this.entityId, current.Operation!);
results.Add(new OperationResult()
{
FailureDetails = new FailureDetails(applicationException),
Expand Down

0 comments on commit a75b62b

Please sign in to comment.