Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Improve logging for function calls processor and kernel function #9495

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

SergeyMenshykh
Copy link
Member

Motivation, Context and Description

This PR improves the existing logging functionality in both FunctionCallsProcessor and KernelFunction by logging additional contextual information, such as function details, function choice behavior configuration, and function call details, to simplify troubleshooting.

@SergeyMenshykh SergeyMenshykh self-assigned this Oct 31, 2024
@SergeyMenshykh SergeyMenshykh requested a review from a team as a code owner October 31, 2024 22:14
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core labels Oct 31, 2024
@@ -23,45 +23,46 @@ internal static partial class KernelFunctionLogMessages
[LoggerMessage(
EventId = 0,
Level = LogLevel.Information,
Message = "Function {FunctionName} invoking.")]
Message = "Function {PluginName}-{FunctionName} invoking.")]
Copy link
Contributor

Choose a reason for hiding this comment

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

If PluginName is null, will the message become "Function -{FuctionName} invoking" or "Function Null-{FunctionName} invoking"?

Copy link
Member Author

Choose a reason for hiding this comment

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

If the plugin name is not provided and the function name is Function1, the log message will be: "Function (null) - Function1 invoking."

The message template can be slightly changed to better communicate that no plugin is provided. The log entry message would be: "Function(PluginName:(null), Name:Function1) invoking." if the plugin name is null.

If neither of the messages looks right, the source generated methods that produces logs above:

[LoggerMessage(
    EventId = 0,
    Level = LogLevel.Information,
    Message = "Function {PluginName}-{FunctionName} invoking.")]
public static partial void LogFunctionInvoking(
    this ILogger logger,
    string? pluginName,
    string functionName);

can be replaced by two actions and one method to handle the null plugin names:

/// <summary>
/// Action to log invocation of a <see cref="KernelFunction"/>.
/// </summary>
private static readonly Action<ILogger, string, string, Exception?> s_logPluginFunctionInvocation =
    LoggerMessage.Define<string, string>(
        logLevel: LogLevel.Information,
        eventId: 0,
        formatString: "Function {PluginName}-{FunctionName} invoking.",
        options: new LogDefineOptions { SkipEnabledCheck = true });

/// <summary>
/// Action to log invocation of a <see cref="KernelFunction"/>.
/// </summary>
private static readonly Action<ILogger, string, Exception?> s_logFunctionInvocation =
    LoggerMessage.Define<string>(
        logLevel: LogLevel.Information,
        eventId: 0,
        formatString: "Function {FunctionName} invoking.",
        options: new LogDefineOptions { SkipEnabledCheck = true });

/// <summary>
/// Logs invocation of a <see cref="KernelFunction"/>.
/// </summary>
public static void LogFunctionInvoking(this ILogger logger, string? pluginName, string functionName)
{
    if (logger.IsEnabled(LogLevel.Information))
    {
        if (!string.IsNullOrEmpty(pluginName))
        {
            s_logPluginFunctionInvocation(logger, pluginName!, functionName, null);
        }
        else
        {
            s_logFunctionInvocation(logger, functionName, null);
        }
    }
}

logLevel: LogLevel.Trace, // Sensitive data, logging as trace, disabled by default
eventId: 0,
"Function arguments: {Arguments}");
"Function {PluginName}-{FunctionName} arguments: {Arguments}");
Copy link
Contributor

@TaoChenOSU TaoChenOSU Oct 31, 2024

Choose a reason for hiding this comment

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

same question as above, also in a couple other places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel.core kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code
Projects
Status: Sprint: In Review
Development

Successfully merging this pull request may close these issues.

3 participants