Add durable instance-id logging scopes for orchestrations and activities#598
Add durable instance-id logging scopes for orchestrations and activities#598
Conversation
Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #250 by adding automatic instance-id logging scopes to orchestration and activity executions in Durable Task. Previously, users had to manually create logging scopes containing the instance ID in each orchestrator and activity to correlate logs in Application Insights.
Key changes:
- Logging scopes wrapping orchestrator execution with
InstanceIdinTaskOrchestrationShim.Execute - Logging scopes wrapping activity execution with
InstanceIdinTaskActivityShim.RunAsync - Unit tests validating that instance-id scopes are properly applied
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Worker/Core/Shims/TaskOrchestrationShim.cs | Wraps orchestration execution in a logger scope containing the InstanceId, ensuring all logs during orchestration have the instance context |
| src/Worker/Core/Shims/TaskActivityShim.cs | Wraps activity execution in a logger scope containing the InstanceId, ensuring all logs during activity have the instance context |
| test/Worker/Core.Tests/Shims/TaskShimLoggingScopeTests.cs | Adds comprehensive unit tests verifying that both orchestration and activity shims properly create logging scopes with the correct InstanceId |
| } | ||
| } | ||
|
|
||
| class TestOrchestrationContext : OrchestrationContext |
There was a problem hiding this comment.
According to the coding guidelines, all private classes that do not serve as base classes should be sealed. The TestOrchestrationContext class should be declared as sealed since it's a private helper class and doesn't serve as a base class.
| using Microsoft.DurableTask; | ||
| using Microsoft.DurableTask.Converters; | ||
| using Microsoft.DurableTask.Worker; | ||
| using Microsoft.DurableTask.Worker.Shims; |
There was a problem hiding this comment.
Duplicate namespace declaration. The namespace is already declared on line 7 (using Microsoft.DurableTask.Worker.Shims). This duplicate declaration should be removed as it's redundant and could cause confusion.
| using Microsoft.DurableTask.Worker.Shims; |
| scopeState!.Should().ContainKey("InstanceId").WhoseValue.Should().Be(instanceId); | ||
| } | ||
|
|
||
| class TestActivity : TaskActivity<string, string> |
There was a problem hiding this comment.
According to the coding guidelines, all private classes that do not serve as base classes should be sealed. The TestActivity class should be declared as sealed since it's a private helper class and doesn't serve as a base class.
| } | ||
| } | ||
|
|
||
| class TestOrchestrator : TaskOrchestrator<string, string> |
There was a problem hiding this comment.
According to the coding guidelines, all private classes that do not serve as base classes should be sealed. The TestOrchestrator class should be declared as sealed since it's a private helper class and doesn't serve as a base class.
Durable Functions logs lacked automatic instance-id scoping, making correlation across orchestrators and activities cumbersome.
Logging scopes
InstanceId.Tests
InstanceIdfor orchestrations and activities.Example scope usage now applied automatically:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.