Skip to content

Commit 776709a

Browse files
committed
refactor(logging): convert to LoggerMessage source-generator
CA1873 modernization across protocol registry, sidecar plugin discovery, and CLI command registry. Replaces the suppressed runtime null-check `_logger?.LogXxx(...)` shape with source-generated partial methods that emit IsEnabled-gated dispatch (Microsoft.Extensions.Logging.LoggerMessage attribute). One internal static partial class per file holds the wrappers, matching the convention already used by AuthProviderLog and BackgroundUpdateCheckLog. Drops 5 CA1873 pragmas.
1 parent 79c08bf commit 776709a

3 files changed

Lines changed: 68 additions & 25 deletions

File tree

src/Kuestenlogik.Bowire.Cli/BowireCliCommandRegistry.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ public static BowireCliCommandRegistry Discover(
5151
{
5252
if (disabled.Contains(command.Id))
5353
{
54-
#pragma warning disable CA1873
55-
logger?.LogInformation(
56-
"Skipping disabled CLI command '{CommandId}' (--disable-cli-command).",
57-
command.Id);
58-
#pragma warning restore CA1873
54+
if (logger is not null)
55+
CliCommandRegistryLog.SkippingDisabledCommand(logger, command.Id);
5956
continue;
6057
}
6158
registry.Register(command);
@@ -72,3 +69,18 @@ public static BowireCliCommandRegistry Discover(
7269
return registry;
7370
}
7471
}
72+
73+
/// <summary>
74+
/// Source-generated logger wrappers for
75+
/// <see cref="BowireCliCommandRegistry"/>. Keeps CA1873 happy — the
76+
/// generator emits the <c>IsEnabled</c>-gated dispatch the analyzer
77+
/// wants in place of the suppressed runtime null-check.
78+
/// </summary>
79+
internal static partial class CliCommandRegistryLog
80+
{
81+
[LoggerMessage(
82+
EventId = 1,
83+
Level = LogLevel.Information,
84+
Message = "Skipping disabled CLI command '{CommandId}' (--disable-cli-command).")]
85+
public static partial void SkippingDisabledCommand(ILogger logger, string commandId);
86+
}

src/Kuestenlogik.Bowire/BowireProtocolRegistry.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,8 @@ public static BowireProtocolRegistry Discover(
120120
{
121121
if (disabled.Contains(protocol.Id))
122122
{
123-
#pragma warning disable CA1873 // logger is already null-checked
124-
logger?.LogInformation(
125-
"Skipping disabled protocol plugin '{PluginId}' (Bowire:DisabledPlugins).",
126-
protocol.Id);
127-
#pragma warning restore CA1873
123+
if (logger is not null)
124+
ProtocolRegistryLog.SkippingDisabledPlugin(logger, protocol.Id);
128125
// #29 -- still record the load attempt so the
129126
// 'skipped because disabled' branch shows up
130127
// in the Grafana panel separately from
@@ -218,3 +215,19 @@ private static void ForceLoadReferencedBowireAssemblies(ILogger? logger)
218215
}
219216
}
220217
}
218+
219+
/// <summary>
220+
/// Source-generated logger wrappers for
221+
/// <see cref="BowireProtocolRegistry"/>. Spinning these out of the
222+
/// inline calls keeps CA1873 happy (the generator emits the
223+
/// <c>IsEnabled</c>-gated dispatch the analyzer wants and avoids
224+
/// boxing of the <c>{PluginId}</c> tag when the level isn't enabled).
225+
/// </summary>
226+
internal static partial class ProtocolRegistryLog
227+
{
228+
[LoggerMessage(
229+
EventId = 1,
230+
Level = LogLevel.Information,
231+
Message = "Skipping disabled protocol plugin '{PluginId}' (Bowire:DisabledPlugins).")]
232+
public static partial void SkippingDisabledPlugin(ILogger logger, string pluginId);
233+
}

src/Kuestenlogik.Bowire/Plugins/Sidecar/SidecarPluginDiscovery.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,23 @@ public static IReadOnlyList<SidecarBowireProtocol> Discover(
5555
var manifest = SidecarPluginManifest.TryLoadFromFile(manifestPath);
5656
if (manifest is null)
5757
{
58-
#pragma warning disable CA1873
59-
logger?.LogWarning(
60-
"Sidecar manifest at {Path} is missing or malformed; skipping.",
61-
manifestPath);
62-
#pragma warning restore CA1873
58+
if (logger is not null)
59+
SidecarDiscoveryLog.ManifestMissingOrMalformed(logger, manifestPath);
6360
continue;
6461
}
6562

6663
if (string.IsNullOrEmpty(manifest.Protocol?.Id)
6764
|| string.IsNullOrEmpty(manifest.Executable))
6865
{
69-
#pragma warning disable CA1873
70-
logger?.LogWarning(
71-
"Sidecar manifest at {Path} is missing protocol.id or executable; skipping.",
72-
manifestPath);
73-
#pragma warning restore CA1873
66+
if (logger is not null)
67+
SidecarDiscoveryLog.ManifestMissingRequiredFields(logger, manifestPath);
7468
continue;
7569
}
7670

7771
if (disabled.Contains(manifest.Protocol.Id))
7872
{
79-
#pragma warning disable CA1873
80-
logger?.LogInformation(
81-
"Skipping disabled sidecar plugin '{PluginId}' (Bowire:DisabledPlugins).",
82-
manifest.Protocol.Id);
83-
#pragma warning restore CA1873
73+
if (logger is not null)
74+
SidecarDiscoveryLog.SkippingDisabledSidecar(logger, manifest.Protocol.Id);
8475
continue;
8576
}
8677

@@ -90,3 +81,30 @@ public static IReadOnlyList<SidecarBowireProtocol> Discover(
9081
return results;
9182
}
9283
}
84+
85+
/// <summary>
86+
/// Source-generated logger wrappers for
87+
/// <see cref="SidecarPluginDiscovery"/>. Folds three CA1873 sites into
88+
/// one place; the generator emits <c>IsEnabled</c>-gated dispatch so
89+
/// the runtime null-check the analyzer flagged is gone.
90+
/// </summary>
91+
internal static partial class SidecarDiscoveryLog
92+
{
93+
[LoggerMessage(
94+
EventId = 1,
95+
Level = LogLevel.Warning,
96+
Message = "Sidecar manifest at {Path} is missing or malformed; skipping.")]
97+
public static partial void ManifestMissingOrMalformed(ILogger logger, string path);
98+
99+
[LoggerMessage(
100+
EventId = 2,
101+
Level = LogLevel.Warning,
102+
Message = "Sidecar manifest at {Path} is missing protocol.id or executable; skipping.")]
103+
public static partial void ManifestMissingRequiredFields(ILogger logger, string path);
104+
105+
[LoggerMessage(
106+
EventId = 3,
107+
Level = LogLevel.Information,
108+
Message = "Skipping disabled sidecar plugin '{PluginId}' (Bowire:DisabledPlugins).")]
109+
public static partial void SkippingDisabledSidecar(ILogger logger, string pluginId);
110+
}

0 commit comments

Comments
 (0)