Skip to content

Allocation-free logging for disabled log levels #36012

Closed
@mikernet

Description

@mikernet

I think there should be allocation-free extension methods for disabled log levels. Right now calls to Log always allocate an array and box values which makes it very expensive for trace level calls. This is fine if the log is enabled, but a call to log a disabled log level should have minimal impact on performance.

A set of extension methods following this pattern would solve the problem:

public static void LogTrace<T>(this ILogger logger, Exception exception, string message, T arg)
{
    if (logger.IsEnabled(LogLevel.Trace))
        logger.Log(LogLevel.Trace, exception, message, arg);
}

public static void LogTrace<T1, T2>(this ILogger logger, Exception exception, string message, T1 arg1, T2 arg2)
{
    if (logger.IsEnabled(LogLevel.Trace))
        logger.Log(LogLevel.Trace, exception, message, arg1, arg2);
}

public static void LogTrace<T1, T2, T3>(this ILogger logger, Exception exception, string message, T1 arg1, T2 arg2, T3 arg3)
{
    if (logger.IsEnabled(LogLevel.Trace))
        logger.Log(LogLevel.Trace, exception, message, arg1, arg2, arg3);
}

Is there an appetite for a PR for this? If so, I'll happily take it. If not, I'll just build these into my own library.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions