Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions csharp/src/Blockchain.Common/Logging/LoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.Logging;

namespace Train.Solver.Blockchain.Common;

/// <summary>
/// Logs a structured message using the compile-time source generator for high-performance logging.
/// </summary>
/// <remarks>
/// VS might show build error, since source generator works compile-time.
/// See: https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator
/// </remarks>
public static partial class TemporalLoggingExtensions
{
[LoggerMessage(
Level = LogLevel.Information,
Message = "Event listener: `{eventListenerId}` was not running. Started.")]
public static partial void EventListeningStarted(
this ILogger logger,
string eventListenerId);

[LoggerMessage(
Level = LogLevel.Information,
Message = "Event listener: `{eventListenerId}` should be terminated. Terminated.")]
public static partial void EventListeningTerminated(
this ILogger logger,
string eventListenerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public async Task RunAsync()

foreach (var network in activeNetworks)
{
if (!activeEventListenerWorkflowIds.Any(x => x == TemporalHelper.BuildEventListenerId(network.Name)))
var eventListnerId = TemporalHelper.BuildEventListenerId(network.Name);

if (!activeEventListenerWorkflowIds.Any(x => x == eventListnerId))
{
await ExecuteActivityAsync(
(WorkflowActivities x) => x.RunEventListeningWorkflowAsync(
Expand All @@ -37,19 +39,22 @@ await ExecuteActivityAsync(
_blockBachSize,
_waitIntervalInSeconds),
TemporalHelper.DefaultActivityOptions(Constants.CoreTaskQueue));

Logger.EventListeningStarted(eventListnerId);
}
}

var mustBeStoppedEventListenersIds = activeEventListenerWorkflowIds
.Where(workflowId =>
!activeNetworks.Any(x => workflowId == TemporalHelper.BuildEventListenerId(x.Name)))
.ToList();
.Where(workflowId => !activeNetworks.Any(
x => workflowId == TemporalHelper.BuildEventListenerId(x.Name)));

foreach (var eventListenerId in mustBeStoppedEventListenersIds)
{
await ExecuteActivityAsync(
(IWorkflowActivities x) => x.TerminateWorkflowAsync(eventListenerId),
TemporalHelper.DefaultActivityOptions(Constants.CoreTaskQueue));

Logger.EventListeningTerminated(eventListenerId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static TrainSolverBuilder WithOpenTelemetryLogging(
.ClearProviders()
.AddFilter("Microsoft", LogLevel.None)
.AddFilter("System", LogLevel.None)
.AddFilter("Azure", LogLevel.None)
.AddOpenTelemetry(logging =>
{
logging.AddOtlpExporter();
Expand Down