[dotnet] Improve logging interpolated string allocation#17391
Conversation
Review Summary by QodoAdd interpolated string handler support to logging infrastructure
WalkthroughsDescription• Add interpolated string handler support for deferred log message construction • Implement level-specific handlers (Trace, Debug, Info, Warn, Error) to avoid allocations • Add compatibility shims for .NET versions prior to 8.0 • Guard string construction with IsEnabled checks in Logger implementation File Changes1. dotnet/src/webdriver/Internal/Logging/ILogger.cs
|
Code Review by Qodo
1. ILogger adds new methods
|
There was a problem hiding this comment.
Pull request overview
This PR adds interpolated string handler support to the .NET internal logging API so log messages can defer formatting/allocation until the relevant log level is enabled, with polyfills to support older target frameworks.
Changes:
- Introduces per-log-level interpolated string handler types plus a shared
LogInterpolatedStringHandler. - Extends
ILogger/Loggerwith handler-based overloads and addsIsEnabledguards for the string overloads. - Adds compatibility shims for interpolated string handler attributes for non-net8 TFMs.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/src/webdriver/Internal/Logging/LogInterpolatedStringHandler.cs | Adds new interpolated string handler types used to defer message construction. |
| dotnet/src/webdriver/Internal/Logging/ILogger.cs | Adds new handler-based overloads for each log level. |
| dotnet/src/webdriver/Internal/Logging/Logger.cs | Implements the new overloads and adds enabled checks for existing string overloads. |
| dotnet/src/webdriver/Properties/InterpolatedStringHandlerAttribute.cs | Adds a pre-net8 attribute shim required for handler support on older TFMs. |
| dotnet/src/webdriver/Properties/InterpolatedStringHandlerArgumentAttribute.cs | Adds a pre-net8 attribute shim required for handler argument binding. |
Now compiler can guard IsEnabled pattern.
💥 What does this PR do?
This pull request introduces support for interpolated string handlers to the logging infrastructure, allowing log messages to defer string construction until it's confirmed that the corresponding log level is enabled. This improves performance by avoiding unnecessary string allocations when logging is disabled. The changes include new interpolated string handler types, interface and implementation updates, and compatibility shims for older .NET versions.
Interpolated String Handler Support
TraceLogStringHandler,DebugLogStringHandler,InfoLogStringHandler,WarnLogStringHandler,ErrorLogStringHandler, and the coreLogInterpolatedStringHandler) toLogInterpolatedStringHandler.cs, enabling deferred string formatting for log messages.ILoggerinterface inILogger.csto add overloads for each log level method that accept the corresponding interpolated string handler, allowing client code to use the new logging pattern.Loggerimplementation to support the new handler-based overloads, ensuring messages are only constructed and logged if the log level is enabled..NET Compatibility
InterpolatedStringHandlerAttributeandInterpolatedStringHandlerArgumentAttributeto thePropertiesfolder for compatibility with .NET versions prior to 8.0, ensuring the code can use interpolated string handlers across supported frameworks. [1] [2]Minor Updates
System.Runtime.CompilerServicesusing directive toILogger.csto support the new attributes and handler types.🤖 AI assistance
🔄 Types of changes