Skip to content

Commit

Permalink
move to source generated logger
Browse files Browse the repository at this point in the history
  • Loading branch information
lsfera committed Aug 19, 2024
1 parent 6cbcf1e commit be963fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
18 changes: 18 additions & 0 deletions src/Blumchen/DependencyInjection/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Blumchen.Subscriptions.Replication;
using Microsoft.Extensions.Logging;

namespace Blumchen.DependencyInjection;

internal static partial class LoggerExtensions

{
[LoggerMessage(Message = "{workerName} started", Level = LogLevel.Information)]
public static partial void ServiceStarted(this ILogger logger, string workerName);

[LoggerMessage(Message = "{workerName} sopped", Level = LogLevel.Information)]
public static partial void ServiceStopped(this ILogger logger, string workerName);

[LoggerMessage(Message = "{message} processed", Level = LogLevel.Trace)]
public static partial void MessageProcessed(this ILogger logger, IEnvelope message);

}
26 changes: 3 additions & 23 deletions src/Blumchen/DependencyInjection/Worker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Concurrent;
using Blumchen.Subscriptions;
using Blumchen.Subscriptions.Replication;
using Microsoft.Extensions.Hosting;
Expand All @@ -11,25 +10,6 @@ public class Worker<T>(
ILogger<Worker<T>> logger): BackgroundService where T : class, IMessageHandler
{
private string WorkerName { get; } = $"{nameof(Worker<T>)}<{typeof(T).Name}>";
private static readonly ConcurrentDictionary<string, Action<ILogger, string, object[]>> LoggingActions = new(StringComparer.OrdinalIgnoreCase);
private static void Notify(ILogger logger, LogLevel level, string template, params object[] parameters)
{
LoggingActions.GetOrAdd(template,_ => LoggerAction(level, logger.IsEnabled(level)))(logger, template, parameters);
return;

static Action<ILogger, string, object[]> LoggerAction(LogLevel ll, bool enabled) =>
(ll, enabled) switch
{
(LogLevel.Information, true) => (logger, template, parameters) => logger.LogInformation(template, parameters),
(LogLevel.Debug, true) => (logger, template, parameters) => logger.LogDebug(template, parameters),
(LogLevel.Trace, true) => (logger, template, parameters) => logger.LogTrace(template, parameters),
(LogLevel.Warning, true) => (logger, template, parameters) => logger.LogWarning(template, parameters),
(LogLevel.Error, true) => (logger, template, parameters) => logger.LogError(template, parameters),
(LogLevel.Critical, true) => (logger, template, parameters) => logger.LogCritical(template, parameters),
(_, _) => (_, _, _) => { }
};
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await options.OuterPipeline.ExecuteAsync(async token =>
Expand All @@ -38,11 +18,11 @@ await options.InnerPipeline.ExecuteAsync(async ct =>
await using var subscription = new Subscription();
await using var cursor = subscription.Subscribe(options.SubscriberOptions, ct)
.GetAsyncEnumerator(ct);
Notify(logger, LogLevel.Information, "{WorkerName} started", WorkerName);
logger.ServiceStarted(WorkerName);
while (await cursor.MoveNextAsync().ConfigureAwait(false) && !ct.IsCancellationRequested)
Notify(logger, LogLevel.Trace, "{cursor.Current} processed", cursor.Current);
logger.MessageProcessed(cursor.Current);
}, token).ConfigureAwait(false), stoppingToken).ConfigureAwait(false);
Notify(logger, LogLevel.Information, "{WorkerName} stopped", WorkerName);
logger.ServiceStopped(WorkerName);
}

}

0 comments on commit be963fb

Please sign in to comment.