-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net Agents - Add dynamic logging methods (#7069)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Migrate logging to dynamic logging methods for better maintability and discoverability (best practice). ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Moved each instance of logging a message into a class specific log-message file. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
- Loading branch information
Showing
24 changed files
with
855 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
dotnet/src/Agents/Abstractions/Logging/AgentChatLogMessages.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.SemanticKernel.Agents; | ||
|
||
#pragma warning disable SYSLIB1006 // Multiple logging methods cannot use the same event id within a class | ||
|
||
/// <summary> | ||
/// Extensions for logging <see cref="AggregatorAgent"/> invocations. | ||
/// </summary> | ||
/// <remarks> | ||
/// This extension uses the <see cref="LoggerMessageAttribute"/> to | ||
/// generate logging code at compile time to achieve optimized code. | ||
/// </remarks> | ||
[ExcludeFromCodeCoverage] | ||
internal static partial class AgentChatLogMessages | ||
{ | ||
/// <summary> | ||
/// Logs retrieval of <see cref="AgentChat"/> messages. | ||
/// </summary> | ||
private static readonly Action<ILogger, string, string, string, Exception?> s_logAgentChatGetChatMessages = | ||
LoggerMessage.Define<string, string, string>( | ||
logLevel: LogLevel.Debug, | ||
eventId: 0, | ||
"[{MethodName}] Source: {MessageSourceType}/{MessageSourceId}."); | ||
public static void LogAgentChatGetChatMessages( | ||
this ILogger logger, | ||
string methodName, | ||
Agent? agent) | ||
{ | ||
if (logger.IsEnabled(LogLevel.Debug)) | ||
{ | ||
if (null == agent) | ||
{ | ||
s_logAgentChatGetChatMessages(logger, methodName, "primary", "primary", null); | ||
} | ||
else | ||
{ | ||
s_logAgentChatGetChatMessages(logger, methodName, agent.GetType().Name, agent.Id, null); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Logs <see cref="AgentChat"/> adding messages (started). | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Debug, | ||
Message = "[{MethodName}] Adding Messages: {MessageCount}.")] | ||
public static partial void LogAgentChatAddingMessages( | ||
this ILogger logger, | ||
string methodName, | ||
int messageCount); | ||
|
||
/// <summary> | ||
/// Logs <see cref="AgentChat"/> added messages (complete). | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Information, | ||
Message = "[{MethodName}] Adding Messages: {MessageCount}.")] | ||
public static partial void LogAgentChatAddedMessages( | ||
this ILogger logger, | ||
string methodName, | ||
int messageCount); | ||
|
||
/// <summary> | ||
/// Logs <see cref="AgentChat"/> invoking agent (started). | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Debug, | ||
Message = "[{MethodName}] Invoking agent {AgentType}/{AgentId}.")] | ||
public static partial void LogAgentChatInvokingAgent( | ||
this ILogger logger, | ||
string methodName, | ||
Type agentType, | ||
string agentId); | ||
|
||
/// <summary> | ||
/// Logs <see cref="AgentChat"/> invoked agent message | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Trace, | ||
Message = "[{MethodName}] Agent message {AgentType}/{AgentId}: {Message}.")] | ||
public static partial void LogAgentChatInvokedAgentMessage( | ||
this ILogger logger, | ||
string methodName, | ||
Type agentType, | ||
string agentId, | ||
ChatMessageContent message); | ||
|
||
/// <summary> | ||
/// Logs <see cref="AgentChat"/> invoked agent (complete). | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Information, | ||
Message = "[{MethodName}] Invoked agent {AgentType}/{AgentId}.")] | ||
public static partial void LogAgentChatInvokedAgent( | ||
this ILogger logger, | ||
string methodName, | ||
Type agentType, | ||
string agentId); | ||
|
||
/// <summary> | ||
/// Logs <see cref="AgentChat"/> creating agent channel (started). | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Debug, | ||
Message = "[{MethodName}] Creating channel for {AgentType}: {AgentId}")] | ||
public static partial void LogAgentChatCreatingChannel( | ||
this ILogger logger, | ||
string methodName, | ||
Type agentType, | ||
string agentId); | ||
|
||
/// <summary> | ||
/// Logs <see cref="AgentChat"/> created agent channel (complete). | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Information, | ||
Message = "[{MethodName}] Created channel for {AgentType}: {AgentId}")] | ||
public static partial void LogAgentChatCreatedChannel( | ||
this ILogger logger, | ||
string methodName, | ||
Type agentType, | ||
string agentId); | ||
} |
45 changes: 45 additions & 0 deletions
45
dotnet/src/Agents/Abstractions/Logging/AggregatorAgentLogMessages.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.SemanticKernel.Agents; | ||
|
||
#pragma warning disable SYSLIB1006 // Multiple logging methods cannot use the same event id within a class | ||
|
||
/// <summary> | ||
/// Extensions for logging <see cref="AggregatorAgent"/> invocations. | ||
/// </summary> | ||
/// <remarks> | ||
/// This extension uses the <see cref="LoggerMessageAttribute"/> to | ||
/// generate logging code at compile time to achieve optimized code. | ||
/// </remarks> | ||
[ExcludeFromCodeCoverage] | ||
internal static partial class AggregatorAgentLogMessages | ||
{ | ||
/// <summary> | ||
/// Logs <see cref="AggregatorAgent"/> creating channel (started). | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Debug, | ||
Message = "[{MethodName}] Creating channel {ChannelType}.")] | ||
public static partial void LogAggregatorAgentCreatingChannel( | ||
this ILogger logger, | ||
string methodName, | ||
string channelType); | ||
|
||
/// <summary> | ||
/// Logs <see cref="AggregatorAgent"/> created channel (complete). | ||
/// </summary> | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Information, | ||
Message = "[{MethodName}] Created channel {ChannelType} ({ChannelMode}) with: {AgentChatType}.")] | ||
public static partial void LogAggregatorAgentCreatedChannel( | ||
this ILogger logger, | ||
string methodName, | ||
string channelType, | ||
AggregatorMode channelMode, | ||
Type agentChatType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.