Skip to content

Commit

Permalink
Internal switch to LoggerMessage instead of resource strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bdongus committed Apr 10, 2024
1 parent ac570c2 commit 6b40220
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 62 deletions.
2 changes: 1 addition & 1 deletion idee5.Common.Data.Tests/ServiceCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ServiceCollectionTests {
services.RegisterHandlers(typeof(ICommandHandlerAsync<>));

// Assert
Assert.AreEqual(3, services.Count);
Assert.AreEqual(4, services.Count);
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions idee5.Common.Data.Tests/ValidationReporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ValidationReporterTests {
reporter.Report(validationResult);

// Assert
Assert.AreEqual("Member names : ", loggerFactory.Sink.LogEntries.First().Message);
Assert.AreEqual("Invalid members : ", loggerFactory.Sink.LogEntries.First().Message);
}

[TestMethod]
Expand All @@ -55,7 +55,7 @@ public class ValidationReporterTests {
reporter.Report(null);

// Assert
Assert.AreEqual("Validation result is NULL.", loggerFactory.Sink.LogEntries.First().Message);
Assert.AreEqual("Validation result is NULL", loggerFactory.Sink.LogEntries.First().Message);
}

[TestMethod]
Expand All @@ -70,7 +70,7 @@ public class ValidationReporterTests {
await reporter.ReportAsync(validationResult).ConfigureAwait(false);

// Assert
Assert.AreEqual("Member names : ", loggerFactory.Sink.LogEntries.First().Message);
Assert.AreEqual("Invalid members : ", loggerFactory.Sink.LogEntries.First().Message);
}

[TestMethod]
Expand All @@ -84,7 +84,7 @@ public class ValidationReporterTests {
await reporter.ReportAsync(null).ConfigureAwait(false);

// Assert
Assert.AreEqual("Validation result is NULL.", loggerFactory.Sink.LogEntries.First().Message);
Assert.AreEqual("Validation result is NULL", loggerFactory.Sink.LogEntries.First().Message);
}
}
}
12 changes: 12 additions & 0 deletions idee5.Common.Data/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ internal static partial class Log {
public static partial void Importing(this ILogger logger, int rowNumber, string rowData);
[LoggerMessage(5, LogLevel.Error, "Row with invalid data detected: {errorMessage}")]
public static partial void ImportError(this ILogger logger, string errorMessage);
[LoggerMessage(6, LogLevel.Trace, "Invoking command {commandName}")]
public static partial void InvokingCommand(this ILogger logger, string commandName);
[LoggerMessage(7, LogLevel.Information, "Command parameters are : {commandParameters}")]
public static partial void CommandParametersAre(this ILogger logger, string commandParameters);
[LoggerMessage(8, LogLevel.Trace, "Command {commandName} executed")]
public static partial void CommandExecuted(this ILogger logger, string commandName);
[LoggerMessage(9, LogLevel.Error, "Validation failed : {errorMessage}")]
public static partial void ValidationError(this ILogger logger, string errorMessage);
[LoggerMessage(10, LogLevel.Warning, "Validation result is NULL")]
public static partial void NoValidationResult(this ILogger logger);
[LoggerMessage(11, LogLevel.Error, "Invalid members : {memberNames}")]
public static partial void InvalidMembers(this ILogger logger, string memberNames);
}
13 changes: 4 additions & 9 deletions idee5.Common.Data/LogValidationReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,16 @@ public class LogValidationReporter : IValidationResultReporter {
/// <inheritdoc/>
public void Report(ValidationResult validationResult) {
if (validationResult == null) {
_logger.LogWarning(Resources.NoValidationResult);
_logger.NoValidationResult();
} else {
_logger.LogError(Resources.MemberNames, validationResult.MemberNames.JoinAsString(","));
_logger.LogError(Resources.Error, validationResult.ErrorMessage);
_logger.InvalidMembers(validationResult.MemberNames.JoinAsString(","));
_logger.ValidationError(validationResult.ErrorMessage ?? "");
}
}

/// <inheritdoc/>
public Task ReportAsync(ValidationResult validationResult, CancellationToken cancellationToken = default) {
if (validationResult == null) {
_logger.LogWarning(Resources.NoValidationResult);
} else {
_logger.LogError(Resources.MemberNames, validationResult.MemberNames.JoinAsString(","));
_logger.LogError(Resources.Error, validationResult.ErrorMessage);
}
Report(validationResult);
return Task.CompletedTask;
}
}
16 changes: 8 additions & 8 deletions idee5.Common.Data/LoggingCommandHandlerAsync.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using idee5.Common.Data.Properties;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Threading;
Expand All @@ -10,7 +9,7 @@ namespace idee5.Common.Data;
/// <see cref="ICommandHandlerAsync{TCommand}"/> decorator logging the invocation to "Trace"
/// and the parameters to "Info".
/// </summary>
/// <typeparam name="TCommand">Type of the commad parameters</typeparam>
/// <typeparam commandName="TCommand">Type of the commad parameters</typeparam>
public class LoggingCommandHandlerAsync<TCommand> : ICommandHandlerAsync<TCommand> {
private readonly ICommandHandlerAsync<TCommand> _handler;
private readonly ILoggerFactory _loggerFactory;
Expand All @@ -19,8 +18,8 @@ public class LoggingCommandHandlerAsync<TCommand> : ICommandHandlerAsync<TComman
/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="handler">The decorated handler.</param>
/// <param name="loggerFactory">Factory creating the logger to use.</param>
/// <param commandName="handler">The decorated handler.</param>
/// <param commandName="loggerFactory">Factory creating the logger to use.</param>
public LoggingCommandHandlerAsync(ICommandHandlerAsync<TCommand> handler, ILoggerFactory loggerFactory) {
_handler = handler ?? throw new ArgumentNullException(nameof(handler));
_loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
Expand All @@ -34,9 +33,10 @@ public class LoggingCommandHandlerAsync<TCommand> : ICommandHandlerAsync<TComman
#else
ArgumentNullException.ThrowIfNull(command);
#endif
_logger.LogTrace(Resources.InvokingComand, typeof(TCommand).Name);
_logger.LogInformation(Resources.CommandParametersAre, Environment.NewLine + command.AsString());
string commandName = typeof(TCommand).Name;
_logger.InvokingCommand(commandName);
_logger.CommandParametersAre(Environment.NewLine + command.AsString());
await _handler.HandleAsync(command, cancellationToken).ConfigureAwait(false);
_logger.LogTrace(Resources.CommandInvoked, typeof(TCommand).Name);
_logger.CommandExecuted(commandName);
}
}
27 changes: 0 additions & 27 deletions idee5.Common.Data/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions idee5.Common.Data/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,16 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="CommandInvoked" xml:space="preserve">
<value>Command {0} invoked.</value>
<comment>LoggingCommandHandler</comment>
</data>
<data name="CommandParametersAre" xml:space="preserve">
<value>Command parameters are : {0}</value>
<comment>LoggingCommandHandler</comment>
</data>
<data name="Error" xml:space="preserve">
<value>Error : {0}</value>
<comment>ConsoleValidationReporter</comment>
</data>
<data name="InvokingComand" xml:space="preserve">
<value>Invoking command {0}</value>
<comment>LoggingCommandHandler</comment>
</data>
<data name="MemberNames" xml:space="preserve">
<value>Member names : {0}</value>
<comment>ConsoleValidationReporter</comment>
</data>
<data name="NoValidationResult" xml:space="preserve">
<value>Validation result is NULL.</value>
<comment>LogValidationReporter</comment>
<comment>ConsoleValidationReporter</comment>
</data>
</root>

0 comments on commit 6b40220

Please sign in to comment.