Skip to content

Commit 6b66067

Browse files
committed
improve logging using with LoggerMessage
1 parent be78cc2 commit 6b66067

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/Smdn.Net.MuninNode.Hosting/Smdn.Net.MuninNode.Hosting/MuninNodeBackgroundService.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// SPDX-FileCopyrightText: 2025 smdn <smdn@smdn.jp>
22
// SPDX-License-Identifier: MIT
3-
4-
#pragma warning disable CA1848 // For improved performance, use the LoggerMessage delegates instead of calling 'LoggerExtensions.LogInformation(ILogger, string?, params object?[])'
5-
63
using System;
74
using System.Net;
85
using System.Threading;
@@ -14,6 +11,22 @@
1411
namespace Smdn.Net.MuninNode.Hosting;
1512

1613
public class MuninNodeBackgroundService : BackgroundService {
14+
private static readonly Action<ILogger, string, Exception?> LogStarting = LoggerMessage.Define<string>(
15+
LogLevel.Information,
16+
eventId: default, // TODO
17+
formatString: "Munin node '{HostName}' starting."
18+
);
19+
private static readonly Action<ILogger, string, Exception?> LogStopping = LoggerMessage.Define<string>(
20+
LogLevel.Information,
21+
eventId: default, // TODO
22+
formatString: "Munin node '{HostName}' stopping."
23+
);
24+
private static readonly Action<ILogger, string, Exception?> LogStopped = LoggerMessage.Define<string>(
25+
LogLevel.Information,
26+
eventId: default, // TODO
27+
formatString: "Munin node '{HostName}' stopped."
28+
);
29+
1730
private IMuninNode node;
1831
private readonly ILogger<MuninNodeBackgroundService>? logger;
1932

@@ -70,7 +83,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
7083
if (node is null)
7184
throw new ObjectDisposedException(GetType().FullName);
7285

73-
logger?.LogInformation("Munin node '{HostName}' starting.", node.HostName);
86+
if (logger is not null)
87+
LogStarting(logger, node.HostName, null);
7488

7589
await node.RunAsync(stoppingToken).ConfigureAwait(false);
7690
}
@@ -80,7 +94,8 @@ public override async Task StopAsync(CancellationToken cancellationToken)
8094
if (node is null)
8195
throw new ObjectDisposedException(GetType().FullName);
8296

83-
logger?.LogInformation("Munin node '{HostName}' stopping.", node.HostName);
97+
if (logger is not null)
98+
LogStopping(logger, node.HostName, null);
8499

85100
await base.StopAsync(cancellationToken).ConfigureAwait(false);
86101

@@ -90,7 +105,8 @@ public override async Task StopAsync(CancellationToken cancellationToken)
90105
if (node is NodeBase stoppableNode)
91106
await stoppableNode.StopAsync(cancellationToken).ConfigureAwait(false);
92107

93-
logger?.LogInformation("Munin node '{HostName}' stopped.", node.HostName);
108+
if (logger is not null)
109+
LogStopped(logger, node.HostName, null);
94110

95111
if (node is IDisposable disposableNode)
96112
disposableNode.Dispose();

0 commit comments

Comments
 (0)