From e7db61be3aeb878120bb7db4c43abc8b75930cd5 Mon Sep 17 00:00:00 2001 From: alliscode <25218250+alliscode@users.noreply.github.com> Date: Wed, 13 May 2026 15:14:16 -0700 Subject: [PATCH] Fix CA1873 in DevUI by using LoggerMessage source generator Replaces two ILogger.LogWarning(string, params object?[]) calls in DevUIAuthFilter and DevUIExtensions with allocation-free [LoggerMessage] partial methods on a new internal DevUILog class. Preserves original message templates and structured property names ({RemoteIp}, {EnvVar}). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../DevUIAuthFilter.cs | 4 +--- .../DevUIExtensions.cs | 5 +---- .../src/Microsoft.Agents.AI.DevUI/DevUILog.cs | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 dotnet/src/Microsoft.Agents.AI.DevUI/DevUILog.cs diff --git a/dotnet/src/Microsoft.Agents.AI.DevUI/DevUIAuthFilter.cs b/dotnet/src/Microsoft.Agents.AI.DevUI/DevUIAuthFilter.cs index b8e4b499f8..7f238ab3a0 100644 --- a/dotnet/src/Microsoft.Agents.AI.DevUI/DevUIAuthFilter.cs +++ b/dotnet/src/Microsoft.Agents.AI.DevUI/DevUIAuthFilter.cs @@ -51,9 +51,7 @@ public DevUIAuthFilter(IOptions options, ILogger if (!isLoopback && !this._options.AllowRemoteAccess) { - this._logger.LogWarning( - "Rejected non-loopback DevUI request from {RemoteIp}. Set DevUIOptions.AllowRemoteAccess to permit remote callers.", - remoteIp); + DevUILog.RejectedNonLoopbackRequest(this._logger, remoteIp); return Results.Problem( statusCode: StatusCodes.Status403Forbidden, title: "DevUI access denied", diff --git a/dotnet/src/Microsoft.Agents.AI.DevUI/DevUIExtensions.cs b/dotnet/src/Microsoft.Agents.AI.DevUI/DevUIExtensions.cs index 18d0ae24cf..d22cd46f61 100644 --- a/dotnet/src/Microsoft.Agents.AI.DevUI/DevUIExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.DevUI/DevUIExtensions.cs @@ -100,10 +100,7 @@ private static void WarnIfInsecurelyExposed(ILogger logger, DevUIOptions options if (options.AllowRemoteAccess && !tokenConfigured && options.ConfigureEndpoints is null) { - logger.LogWarning( - "DevUI is configured with AllowRemoteAccess=true and no authentication. " + - "Set DevUIOptions.AuthToken, the {EnvVar} environment variable, or attach an authorization policy via ConfigureEndpoints.", - DevUIOptions.AuthTokenEnvironmentVariable); + DevUILog.InsecurelyExposed(logger, DevUIOptions.AuthTokenEnvironmentVariable); } } } diff --git a/dotnet/src/Microsoft.Agents.AI.DevUI/DevUILog.cs b/dotnet/src/Microsoft.Agents.AI.DevUI/DevUILog.cs new file mode 100644 index 0000000000..a963b42a6f --- /dev/null +++ b/dotnet/src/Microsoft.Agents.AI.DevUI/DevUILog.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Net; + +namespace Microsoft.Agents.AI.DevUI; + +internal static partial class DevUILog +{ + [LoggerMessage( + EventId = 1, + Level = LogLevel.Warning, + Message = "Rejected non-loopback DevUI request from {RemoteIp}. Set DevUIOptions.AllowRemoteAccess to permit remote callers.")] + public static partial void RejectedNonLoopbackRequest(ILogger logger, IPAddress? remoteIp); + + [LoggerMessage( + EventId = 2, + Level = LogLevel.Warning, + Message = "DevUI is configured with AllowRemoteAccess=true and no authentication. Set DevUIOptions.AuthToken, the {EnvVar} environment variable, or attach an authorization policy via ConfigureEndpoints.")] + public static partial void InsecurelyExposed(ILogger logger, string envVar); +}