Skip to content

Commit b83cbfd

Browse files
committed
PR feedback: more specific categories
1 parent ba337c1 commit b83cbfd

File tree

7 files changed

+28
-33
lines changed

7 files changed

+28
-33
lines changed

src/Worker/Core/Logs.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ namespace Microsoft.DurableTask
1111
/// </summary>
1212
static partial class Logs
1313
{
14-
/// <summary>
15-
/// The common category name for worker logs.
16-
/// </summary>
17-
internal const string WorkerCategoryName = "Microsoft.DurableTask.Worker";
14+
internal static ILogger CreateWorkerLogger(ILoggerFactory loggerFactory, params string[] subcategories)
15+
{
16+
string categoryName = "Microsoft.DurableTask.Worker";
17+
if (subcategories.Length > 0)
18+
{
19+
categoryName += "." + string.Join(".", subcategories);
20+
}
21+
22+
return loggerFactory.CreateLogger(categoryName);
23+
}
1824

1925
[LoggerMessage(EventId = 15, Level = LogLevel.Error, Message = "Unhandled exception in entity operation {entityInstanceId}/{operationName}.")]
2026
public static partial void OperationError(this ILogger logger, Exception ex, EntityInstanceId entityInstanceId, string operationName);

src/Worker/Core/Shims/TaskActivityShim.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public TaskActivityShim(
2929
TaskName name,
3030
ITaskActivity implementation)
3131
{
32-
this.logger = Check.NotNull(loggerFactory).CreateLogger(Logs.WorkerCategoryName);
32+
this.logger = Logs.CreateWorkerLogger(Check.NotNull(loggerFactory), "Activities");
3333
this.dataConverter = Check.NotNull(dataConverter);
3434
this.name = Check.NotDefault(name);
3535
this.implementation = Check.NotNull(implementation);
@@ -50,10 +50,10 @@ public TaskActivityShim(
5050
{
5151
object? output = await this.implementation.RunAsync(contextWrapper, deserializedInput);
5252

53-
this.logger.ActivityCompleted(instanceId, this.name);
54-
5553
// Return the output (if any) as a serialized string.
5654
string? serializedOutput = this.dataConverter.Serialize(output);
55+
this.logger.ActivityCompleted(instanceId, this.name);
56+
5757
return serializedOutput;
5858
}
5959
catch (Exception e)

src/Worker/Core/Shims/TaskOrchestrationShim.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ public TaskOrchestrationShim(
3434
this.invocationContext = Check.NotNull(invocationContext);
3535
this.implementation = Check.NotNull(implementation);
3636

37-
this.logger = this.invocationContext.LoggerFactory.CreateLogger(Logs.WorkerCategoryName);
37+
this.logger = Logs.CreateWorkerLogger(this.invocationContext.LoggerFactory, "Orchestrations");
3838
}
3939

4040
DataConverter DataConverter => this.invocationContext.Options.DataConverter;
4141

42-
ILoggerFactory LoggerFactory => this.invocationContext.LoggerFactory;
43-
4442
/// <inheritdoc/>
4543
public override async Task<string?> Execute(OrchestrationContext innerContext, string rawInput)
4644
{

src/Worker/Grpc/Logs.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ namespace Microsoft.DurableTask.Worker.Grpc
88
/// <summary>
99
/// Log messages.
1010
/// </summary>
11-
/// <remarks>
12-
/// NOTE: Trying to make logs consistent with https://github.com/Azure/durabletask/blob/main/src/DurableTask.Core/Logging/LogEvents.cs.
13-
/// </remarks>
1411
static partial class Logs
1512
{
1613
[LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = "Durable Task gRPC worker starting and connecting to {endpoint}.")]

test/Grpc.IntegrationTests/IntegrationTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ protected IReadOnlyCollection<LogEntry> GetLogs()
9090
return logs;
9191
}
9292

93-
protected IReadOnlyCollection<LogEntry> GetLogs(string categoryName)
93+
protected IReadOnlyCollection<LogEntry> GetLogs(string category)
9494
{
95-
this.logProvider.TryGetLogs(categoryName, out IReadOnlyCollection<LogEntry> logs);
95+
this.logProvider.TryGetLogs(category, out IReadOnlyCollection<LogEntry> logs);
9696
return logs ?? [];
9797
}
9898

test/Grpc.IntegrationTests/OrchestrationErrorHandling.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void MyActivityImpl(TaskActivityContext ctx) =>
7676
Assert.Equal(typeof(CustomException).FullName, failureDetails.InnerFailure.InnerFailure!.ErrorType);
7777
Assert.Equal("Inner!", failureDetails.InnerFailure.InnerFailure.ErrorMessage);
7878

79-
IReadOnlyCollection<LogEntry> workerLogs = this.GetLogs("Microsoft.DurableTask.Worker");
79+
IReadOnlyCollection<LogEntry> workerLogs = this.GetLogs(category: "Microsoft.DurableTask.Worker");
8080
Assert.NotEmpty(workerLogs);
8181

8282
// Check that the orchestrator and activity logs are present

test/TestHelpers/Logging/TestLogProvider.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,21 @@
77

88
namespace Microsoft.DurableTask.Tests.Logging;
99

10-
public sealed class TestLogProvider : ILoggerProvider
10+
public sealed class TestLogProvider(ITestOutputHelper output) : ILoggerProvider
1111
{
12-
readonly ITestOutputHelper output;
13-
readonly ConcurrentDictionary<string, TestLogger> loggers;
14-
15-
public TestLogProvider(ITestOutputHelper output)
16-
{
17-
this.output = output ?? throw new ArgumentNullException(nameof(output));
18-
this.loggers = new ConcurrentDictionary<string, TestLogger>(StringComparer.OrdinalIgnoreCase);
19-
}
12+
readonly ITestOutputHelper output = output ?? throw new ArgumentNullException(nameof(output));
13+
readonly ConcurrentDictionary<string, TestLogger> loggers = new(StringComparer.OrdinalIgnoreCase);
2014

2115
public bool TryGetLogs(string category, out IReadOnlyCollection<LogEntry> logs)
2216
{
23-
if (this.loggers.TryGetValue(category, out TestLogger? logger))
24-
{
25-
logs = logger.GetLogs();
26-
return true;
27-
}
28-
29-
logs = Array.Empty<LogEntry>();
30-
return false;
17+
// Get all logs for loggers that are prefixed with the category name
18+
// (e.g. "Microsoft.DurableTask.Worker" will return all logs for "Microsoft.DurableTask.Worker.*")
19+
logs = this.loggers
20+
.Where(kvp => kvp.Key.StartsWith(category, StringComparison.OrdinalIgnoreCase))
21+
.SelectMany(kvp => kvp.Value.GetLogs())
22+
.OrderBy(log => log.Timestamp)
23+
.ToList();
24+
return logs.Count > 0;
3125
}
3226

3327
public void Clear()

0 commit comments

Comments
 (0)