Skip to content

Commit 2e02259

Browse files
CopilotYunchuWang
andcommitted
Extend logging categories to TaskOrchestrationContextWrapper (WIP)
Co-authored-by: YunchuWang <[email protected]>
1 parent aa7f0ad commit 2e02259

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/Worker/Grpc/DualCategoryLogger.cs renamed to src/Worker/Core/Logging/DualCategoryLogger.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33

44
using Microsoft.Extensions.Logging;
55

6-
namespace Microsoft.DurableTask.Worker.Grpc;
6+
namespace Microsoft.DurableTask.Worker;
77

88
/// <summary>
99
/// A logger wrapper that emits logs to both a primary (new) category and an optional legacy category.
1010
/// </summary>
1111
/// <remarks>
1212
/// This logger is used to maintain backward compatibility while transitioning to more specific logging categories.
1313
/// When legacy categories are enabled, log messages are written to both the new specific category
14-
/// (e.g., "Microsoft.DurableTask.Worker.Grpc") and the legacy broad category (e.g., "Microsoft.DurableTask").
14+
/// (e.g., "Microsoft.DurableTask.Worker.Grpc" or "Microsoft.DurableTask.Worker.Orchestration")
15+
/// and the legacy broad category (e.g., "Microsoft.DurableTask").
1516
/// </remarks>
16-
sealed class DualCategoryLogger : ILogger
17+
internal sealed class DualCategoryLogger : ILogger
1718
{
1819
readonly ILogger primaryLogger;
1920
readonly ILogger? legacyLogger;

src/Worker/Core/Shims/TaskOrchestrationContextWrapper.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public TaskOrchestrationContextWrapper(
6363
this.invocationContext = Check.NotNull(invocationContext);
6464
this.Properties = Check.NotNull(properties);
6565

66-
this.logger = this.CreateReplaySafeLogger("Microsoft.DurableTask");
66+
this.logger = this.CreateReplaySafeLogger("Microsoft.DurableTask.Worker.Orchestration");
6767
this.deserializedInput = deserializedInput;
6868
}
6969

@@ -541,4 +541,30 @@ string GetDefaultVersion()
541541

542542
return string.Empty;
543543
}
544+
545+
/// <inheritdoc/>
546+
public override ILogger CreateReplaySafeLogger(string categoryName)
547+
{
548+
// For orchestration logs, use dual-category logging if legacy categories are enabled
549+
if (categoryName == "Microsoft.DurableTask.Worker.Orchestration" &&
550+
this.invocationContext.Options.Logging.UseLegacyCategories)
551+
{
552+
ILogger primaryLogger = this.LoggerFactory.CreateLogger(categoryName);
553+
ILogger legacyLogger = this.LoggerFactory.CreateLogger("Microsoft.DurableTask");
554+
ILogger dualLogger = new DualCategoryLogger(primaryLogger, legacyLogger);
555+
556+
// Wrap the dual logger in a ReplaySafeLogger by calling the base implementation
557+
// with a temporary category, then using reflection to replace the inner logger
558+
var tempLogger = base.CreateReplaySafeLogger("temp");
559+
var loggerField = tempLogger.GetType().GetField(
560+
"logger",
561+
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
562+
loggerField?.SetValue(tempLogger, dualLogger);
563+
564+
return tempLogger;
565+
}
566+
567+
// For all other categories, use the default behavior
568+
return base.CreateReplaySafeLogger(categoryName);
569+
}
544570
}

src/Worker/Core/Worker.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ The worker is responsible for processing durable task work items.</PackageDescri
88
<EnableStyleCop>true</EnableStyleCop>
99
</PropertyGroup>
1010

11+
<ItemGroup>
12+
<InternalsVisibleTo Include="Microsoft.DurableTask.Worker.Grpc" Key="$(StrongNamePublicKey)" />
13+
<InternalsVisibleTo Include="Microsoft.DurableTask.Worker.Grpc.Tests" Key="$(StrongNamePublicKey)" />
14+
</ItemGroup>
15+
1116
<ItemGroup>
1217
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />
1318
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />

0 commit comments

Comments
 (0)