Skip to content

.NET: Add LoggingAgent wrapper for ILogger-based observability - #2701

Merged
Roger Barreto (rogerbarreto) merged 13 commits into
mainfrom
copilot/add-logging-agent-wrapper
Dec 10, 2025
Merged

.NET: Add LoggingAgent wrapper for ILogger-based observability#2701
Roger Barreto (rogerbarreto) merged 13 commits into
mainfrom
copilot/add-logging-agent-wrapper

Conversation

CopilotAI commented Dec 8, 2025

Copy link
Copy Markdown
Contributor

Implementation Complete for LoggingAgent ✅

  • Create LoggingAgent class extending DelegatingAIAgent similar to OpenTelemetryAgent
    • Implement constructor accepting AIAgent and ILogger
    • Override RunAsync method with logging
    • Override RunStreamingAsync method with logging
    • Add JSON serialization configuration property
    • Add logging for invocations, completions, cancellations, and failures
    • Graceful error handling for JSON serialization failures
  • Create AIAgentBuilderExtensions.UseLogging extension method
    • Add method to configure logging in agent pipeline
    • Support optional ILoggerFactory parameter
    • Support optional configure callback
    • Skip adding LoggingAgent if NullLoggerFactory is detected
  • Create unit tests for LoggingAgent
    • Test constructor validation
    • Test property delegation to inner agent
    • Test RunAsync logging behavior
    • Test RunStreamingAsync logging behavior
    • Test JSON serialization options
    • Test error and cancellation scenarios
  • Create unit tests for UseLogging builder extension
    • Test null builder validation
    • Test successful agent creation
    • Test configure callback invocation
    • Test builder chaining
    • Test with all parameters
    • Test service provider resolution
    • Test NullLoggerFactory optimization
  • Build and test the implementation (351 tests passing)
  • Run code review (addressed all comments)
  • Fix file encoding issues (added UTF-8 BOM to all C# files)

Summary

Successfully implemented LoggingAgent wrapper following the OpenTelemetryAgent pattern. The implementation provides:

  1. Comprehensive Logging at appropriate levels (Debug for invocations, Trace for sensitive data, Error for failures)
  2. Configuration Flexibility through JsonSerializerOptions property and configure callback
  3. Performance Optimization by skipping LoggingAgent when NullLoggerFactory is detected
  4. Error Resilience with graceful handling of JSON serialization failures
  5. Complete Test Coverage with 18 new unit tests covering all scenarios
  6. Proper File Encoding with UTF-8 BOM on all C# files per repository standards

All existing tests continue to pass, ensuring no breaking changes were introduced.

Original prompt

This section details on the original issue you should resolve

<issue_title>.NET: LoggingAgent Wrapper</issue_title>
<issue_description># Motivation and COntext

Currently we starting adding Logging responsibility to the AgentChatClient, which moved a bit away from the wrapping approach used for the OpenTelemetryAgent.

The idea of this issue is to update the existing code similarly how we did to OpenTelemetryAgent.

</issue_description>

<agent_instructions>Similar how was done in here for Microsoft.Extensions.AI.

https://raw.githubusercontent.com/dotnet/extensions/refs/heads/main/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/LoggingChatClient.cs

I need to create a LoggingAgent : DelegatingAgent class in the Microsoft.Agents.AI project that can be used as a decorator with the sole responsibility of logging the agent interactions.

The pattern should follow something similar how the OpenTelemetryAgent has but only focused into ILogger.

Also I need to create a required AgentBuilder extensions similar how we have to OpenTelemetryAgent and the MEAI Builder in here:

https://raw.githubusercontent.com/dotnet/extensions/refs/heads/main/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/LoggingChatClientBuilderExtensions.cs

Please consider some configuration</agent_instructions>

Comments on the Issue (you are Copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

CopilotAIand others added 3 commits December 8, 2025 16:19
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
…ling
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
@markwallace-microsoftMark Wallace (markwallace-microsoft) added the .NET Usage: [Issues, PRs], Target: .Net label Dec 8, 2025
@github-actionsgithub-actionsBot changed the title [WIP] Add LoggingAgent wrapper for AgentChatClient.NET: [WIP] Add LoggingAgent wrapper for AgentChatClientDec 8, 2025
CopilotAI changed the title .NET: [WIP] Add LoggingAgent wrapper for AgentChatClient.NET: Add LoggingAgent wrapper for ILogger-based observabilityDec 8, 2025
@rogerbarreto

Copy link
Copy Markdown
Member

Copilot Formatting failing with the following:

Running formatters.
/app/dotnet/src/Microsoft.Agents.AI/LoggingAgent.cs(1,1): error CHARSET: Fix file encoding. [/app/dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj]
Running Code Style analysis.

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

CopilotAI commented Dec 8, 2025

Copy link
Copy Markdown
ContributorAuthor

Copilot Formatting failing with the following:

...

Fixed the file encoding issue. All C# files now have the required UTF-8 BOM (Byte Order Mark). The formatting check should pass now. Commit: 8657e4c

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a LoggingAgent wrapper class that provides ILogger-based observability for AI agent operations, following the same decorator pattern as OpenTelemetryAgent. The implementation enables comprehensive logging of agent interactions with configurable detail levels and graceful error handling.

Key Changes:

  • Implements LoggingAgent as a DelegatingAIAgent with configurable JSON serialization and multi-level logging (Debug/Trace/Error)
  • Adds UseLogging extension method to AIAgentBuilder with automatic NullLoggerFactory detection for performance optimization
  • Provides comprehensive test coverage with 18 new unit tests covering all scenarios

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
FileDescription
dotnet/src/Microsoft.Agents.AI/LoggingAgent.csNew sealed class implementing agent operation logging with LoggerMessage source generation, supporting Debug (invocations/completions), Trace (sensitive data), and Error (failures) levels
dotnet/src/Microsoft.Agents.AI/AIAgentBuilderExtensions.csAdds UseLogging extension method with service provider resolution, NullLoggerFactory optimization, and optional configuration callback
dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csprojAdds Microsoft.Extensions.Logging.Abstractions package reference
dotnet/tests/Microsoft.Agents.AI.UnitTests/LoggingAgentTests.csComprehensive unit tests for LoggingAgent covering constructor validation, property delegation, logging behavior at different levels, and error/cancellation scenarios
dotnet/tests/Microsoft.Agents.AI.UnitTests/LoggingAIAgentBuilderExtensionsTests.csUnit tests for UseLogging extension method covering null validation, logger factory resolution, configuration callbacks, builder chaining, and NullLoggerFactory optimization
dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csprojAdds Microsoft.Extensions.Logging package reference for testing

Comment threaddotnet/src/Microsoft.Agents.AI/LoggingAgent.cs
Comment threaddotnet/src/Microsoft.Agents.AI/LoggingAgent.cs
Aris Nguyen (arisng) pushed a commit to arisng/agent-framework that referenced this pull request Feb 2, 2026
…soft#2701)
* Initial plan
* Add LoggingAgent class and UseLogging extension method
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
* Add unit tests for LoggingAgent and fix JSON serialization error handling
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
* Add comments explaining unreachable code in test async iterators
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
* Fix file encoding - add UTF-8 BOM to all C# files
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
* Address Format issues
* Addres format
* Break up extensions in dedicated files
* Adjust class names
* Add xmldoc info
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NETUsage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: LoggingAgent Wrapper

5 participants

@rogerbarreto@SergeyMenshykh@markwallace-microsoft