Skip to content

fix: Suppress CA1873 at file level in SemanticKernelAgent sample#306

Merged
rokonec merged 4 commits intomainfrom
copilot/fix-ca1873-logging-errors
Mar 6, 2026
Merged

fix: Suppress CA1873 at file level in SemanticKernelAgent sample#306
rokonec merged 4 commits intomainfrom
copilot/fix-ca1873-logging-errors

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

This is a code sample — readability and simplicity take priority over micro-optimisation patterns. The previous fix introduced a partial Log helper class solely to host [LoggerMessage] source-generated methods, which added unnecessary complexity to a sample meant to be easy to follow.

Changes

  • Drop the internal static partial class Log and revert call sites to plain _logger.LogInformation(...)
  • Add a single file-level #pragma warning disable CA1873 to silence the analyser without touching class declarations
#pragma warning disable CA1873 // Avoid unnecessary lazy evaluation (coding sample — simplicity over micro-optimisation)
Original prompt

Problem

The release build is failing with CA1873 errors (treated as errors) in samples/SemanticKernelAgent/SemanticKernelTravelAgent.cs:

Error: CA1873 at line 56  — _logger.LogInformation("Getting exchange rate from {CurrencyFrom} to {CurrencyTo} for date {Date}", currencyFrom, currencyTo, date)
Error: CA1873 at line 240 — _logger.LogInformation("Initializing Semantic Kernel agent with Azure OpenAI deployment {DeploymentName}", deploymentName)
Error: CA1873 at line 254 — _logger.LogInformation("Initializing Semantic Kernel agent with OpenAI model {ModelId}", modelId)

CA1873 fires because argument expressions (even simple ones) are evaluated unconditionally regardless of whether the log level is enabled, which can be unnecessarily costly.

Fix

Replace the three offending _logger.LogInformation(...) calls with compile-time source-generated log methods using the [LoggerMessage] attribute pattern.

Steps

  1. In SemanticKernelTravelAgent.cs, add LoggerMessage-attributed partial methods to each of the relevant classes (or use a dedicated partial static class for log helpers), covering the three log call sites:

    • CurrencyPlugin: line 56 — LogGettingExchangeRate(ILogger logger, string currencyFrom, string currencyTo, string date)
    • SemanticKernelTravelAgent: line 240 — LogInitializingWithAzureOpenAI(ILogger logger, string deploymentName)
    • SemanticKernelTravelAgent: line 254 — LogInitializingWithOpenAI(ILogger logger, string modelId)
  2. Replace the raw _logger.LogInformation(...) calls at lines 56, 240, and 254 with calls to the generated log methods.

Approach

Use [LoggerMessage] on static partial methods. Since CurrencyPlugin and SemanticKernelTravelAgent are not partial classes, the cleanest approach is to add static partial log helper methods inside each class, or convert the classes to partial and add the log methods.

Example pattern for CurrencyPlugin:

[LoggerMessage(Level = LogLevel.Information, Message = "Getting exchange rate from {CurrencyFrom} to {CurrencyTo} for date {Date}")]
private static partial void LogGettingExchangeRate(ILogger logger, string currencyFrom, string currencyTo, string date);

Then replace the call:

// Before:
_logger.LogInformation("Getting exchange rate from {CurrencyFrom} to {CurrencyTo} for date {Date}", currencyFrom, currencyTo, date);

// After:
LogGettingExchangeRate(_logger, currencyFrom, currencyTo, date);

Do similarly for the two LogInformation calls in InitializeAgent() inside SemanticKernelTravelAgent.

Also mark the classes as partial to allow the source generator to work.

File: samples/SemanticKernelAgent/SemanticKernelTravelAgent.cs

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…generated methods

Co-authored-by: rokonec <25249058+rokonec@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CA1873 errors in SemanticKernelTravelAgent logging Fix CA1873: replace raw LogInformation calls with [LoggerMessage] source-generated methods Mar 6, 2026
…s, remove partial from CurrencyPlugin and SemanticKernelTravelAgent

Co-authored-by: rokonec <25249058+rokonec@users.noreply.github.com>
Copilot AI changed the title Fix CA1873: replace raw LogInformation calls with [LoggerMessage] source-generated methods Fix CA1873: consolidate [LoggerMessage] methods into dedicated Log class Mar 6, 2026
…pression

Co-authored-by: rokonec <25249058+rokonec@users.noreply.github.com>
Copilot AI changed the title Fix CA1873: consolidate [LoggerMessage] methods into dedicated Log class Suppress CA1873 at file level in SemanticKernelAgent sample Mar 6, 2026
@rokonec rokonec marked this pull request as ready for review March 6, 2026 15:00
@rokonec rokonec changed the title Suppress CA1873 at file level in SemanticKernelAgent sample Fix: Suppress CA1873 at file level in SemanticKernelAgent sample Mar 6, 2026
@rokonec rokonec changed the title Fix: Suppress CA1873 at file level in SemanticKernelAgent sample refactor: Suppress CA1873 at file level in SemanticKernelAgent sample Mar 6, 2026
@rokonec rokonec changed the title refactor: Suppress CA1873 at file level in SemanticKernelAgent sample fix: Suppress CA1873 at file level in SemanticKernelAgent sample Mar 6, 2026
@rokonec rokonec added this pull request to the merge queue Mar 6, 2026
Merged via the queue into main with commit 56a8e23 Mar 6, 2026
12 of 16 checks passed
@rokonec rokonec deleted the copilot/fix-ca1873-logging-errors branch March 6, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants