Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/ConnyConsole/Cli/Config/SetConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ConnyConsole.Cli.Config;

public sealed class SetConfigCommand : Command
public sealed partial class SetConfigCommand : Command
{
private readonly IConfigurationEditor _configurationEditor;
private readonly ILogger _logger;
Expand Down Expand Up @@ -64,7 +64,7 @@ private void Handle(ParseResult parseResult, SettingKeyArgument keyArgument, Set
var scope = GetConfigurationScope(parseResult);

var resultMessage = _configurationEditor.SetValue(key, value, scope);
_logger.LogInformation("Set setting result: {Message}", resultMessage);
LogSetSettingResultMessage(resultMessage);
}
catch (Exception exception)
{
Expand Down
9 changes: 9 additions & 0 deletions src/ConnyConsole/Cli/Config/SetConfigCommand.logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Logging;

namespace ConnyConsole.Cli.Config;

public sealed partial class SetConfigCommand
{
[LoggerMessage(LogLevel.Information, "Set setting result: {message}")]
private partial void LogSetSettingResultMessage(string message);
}
22 changes: 11 additions & 11 deletions src/ConnyConsole/Infrastructure/ConsoleCancellationTokenSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ConnyConsole.Infrastructure;

/// <inheritdoc />
public class ConsoleCancellationTokenSource(
public partial class ConsoleCancellationTokenSource(
ILogger<ConsoleCancellationTokenSource> logger,
IEnvironmentProvider environmentProvider)
: CancellationTokenSource
Expand Down Expand Up @@ -31,9 +31,7 @@ public ConsoleCancelEventHandler CreateCancellationHandler(TimeSpan timeout)
{
if (_isGracefulCancelled)
{
logger.LogInformation(
"Received interrupt signal, attempting to shut down gracefully but will force-close in {Seconds} seconds. Send again to immediately force-close.",
timeout.TotalSeconds);
LogGracefulShutdownInitiated(timeout.TotalSeconds);

Cancel();
cancelEvent.Cancel = true;
Expand All @@ -43,7 +41,7 @@ public ConsoleCancelEventHandler CreateCancellationHandler(TimeSpan timeout)
}
else
{
logger.LogInformation("Second interrupt received, force-closing the app");
LogForceShutdownInitiated();
ExitApplication();
}
};
Expand All @@ -54,12 +52,6 @@ public ConsoleCancelEventHandler CreateCancellationHandler(TimeSpan timeout)
/// </summary>
private void EnforceExitAfterTimeout(int timeoutInMilliseconds)
{
void LogAndExit()
{
logger.LogInformation("Timeout reached, force-closing app.");
ExitApplication();
}

if (timeoutInMilliseconds > 0)
{
_ = new Timer(_ => LogAndExit(),
Expand All @@ -71,6 +63,14 @@ void LogAndExit()
{
LogAndExit();
}

return;

void LogAndExit()
{
LogForceShutdownAfterTimeout();
ExitApplication();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Logging;

namespace ConnyConsole.Infrastructure;

public sealed partial class ConsoleCancellationTokenSource
{
[LoggerMessage(LogLevel.Information,
"Received interrupt signal, attempting to shut down gracefully but will force-close in {seconds} seconds. Send again to immediately force-close.")]
private partial void LogGracefulShutdownInitiated(double seconds);

[LoggerMessage(LogLevel.Information, "Second interrupt received, force-closing the app")]
private partial void LogForceShutdownInitiated();

[LoggerMessage(LogLevel.Information, "Timeout reached, force-closing app.")]
private partial void LogForceShutdownAfterTimeout();
}
31 changes: 7 additions & 24 deletions src/ConnyConsole/Services/LogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,18 @@
namespace ConnyConsole.Services;

/// <inheritdoc cref="ILogService" />
public sealed class LogService(ILogger<LogService> logger) : ILogService
public sealed partial class LogService(ILogger<LogService> logger) : ILogService
{
private const string MessageTemplate = "{Message}";
private const string MessageTemplate = "{message}";

public void Log(LogLevel level, string? message)
{
var logMessage = message ?? string.Empty;

// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault - is marked by SonarQube rule csharpsquid:S3458
switch (level)
{
case LogLevel.Critical:
logger.LogCritical(MessageTemplate, logMessage);
break;
case LogLevel.Error:
logger.LogError(MessageTemplate, logMessage);
break;
case LogLevel.Warning:
logger.LogWarning(MessageTemplate, logMessage);
break;
case LogLevel.Debug:
logger.LogDebug(MessageTemplate, logMessage);
break;
case LogLevel.Trace:
logger.LogTrace(MessageTemplate, logMessage);
break;
default:
logger.LogInformation(MessageTemplate, logMessage);
break;
}
var logLevel = level == LogLevel.None
? LogLevel.Information
: level;

LogMessage(logLevel, logMessage);
}
}
9 changes: 9 additions & 0 deletions src/ConnyConsole/Services/LogService.logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Logging;

namespace ConnyConsole.Services;

public sealed partial class LogService
{
[LoggerMessage(MessageTemplate)]
private partial void LogMessage(LogLevel logLevel, string message);
}
2 changes: 1 addition & 1 deletion tests/ConnyConsole.Tests/Services/LogServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public void Log_LeadingAndTrailingWhitespacesMessage_LogsMessageWithLeadingAndTr
{
// Arrange
var logService = new LogService(_logger);
var message = $" {TestMessage} ";
const string message = $" {TestMessage} ";

// Act
logService.Log(LogLevel.Information, message);
Expand Down