记录评估点

时间:2016-11-16 23:11:37

标签: c# .net asp.net-core .net-core ms-extensions-logging

DotNetCore Microsoft.Extensions.Logging

如果我将日志记录级别设置为信息,则

_logger.LogDebug(JsonConvert.SerializeObject(address));

评估

JsonConvert.SerializeObject(address)

部分?如果是,任何想法如何防止这样做,所以它不会在生产中使用过多的资源?

提前致谢

2 个答案:

答案 0 :(得分:1)

实现具有“条件”属性的扩展方法可达到目的 此外,我确认他们到目前为止还没有任何实施details

public static class LoggerExtensionsHelper
{
    public const string Debug = "DEBUG";
    public const string Trace = "TRACE";

    /// <summary>Formats and writes a debug log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="exception">The exception to log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    [Conditional(Debug)]
    public static void LogDebug(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
    {
        LoggerExtensions.LogDebug(logger, eventId, exception, message, args);
    }

    /// <summary>Formats and writes a debug log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    [Conditional(Debug)]
    public static void LogDebug(this ILogger logger, EventId eventId, string message, params object[] args)
    {
        LoggerExtensions.LogDebug(logger, eventId, message, args);
    }

    /// <summary>Formats and writes a debug log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    [Conditional(Debug)]
    public static void LogDebug(this ILogger logger, string message, params object[] args)
    {
        LoggerExtensions.LogDebug(logger, message, args);
    }

    /// <summary>Formats and writes a trace log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="exception">The exception to log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    [Conditional(Trace)]
    public static void LogTrace(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
    {
        LoggerExtensions.LogTrace(logger, eventId, exception, message, args);
    }

    /// <summary>Formats and writes a trace log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    [Conditional(Trace)]
    public static void LogTrace(this ILogger logger, EventId eventId, string message, params object[] args)
    {
        LoggerExtensions.LogTrace(logger, eventId, message, args);
    }

    /// <summary>Formats and writes a trace log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    [Conditional(Trace)]
    public static void LogTrace(this ILogger logger, string message, params object[] args)
    {
        LoggerExtensions.LogTrace(logger, message, args);
    }


    /// <summary>Formats and writes an informational log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="exception">The exception to log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogInformation(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
    {
        LoggerExtensions.LogInformation(logger, eventId, exception, message, args);
    }

    /// <summary>Formats and writes an informational log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogInformation(this ILogger logger, EventId eventId, string message, params object[] args)
    {
        LoggerExtensions.LogInformation(logger, eventId, message, args);
    }

    /// <summary>Formats and writes an informational log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogInformation(this ILogger logger, string message, params object[] args)
    {
        LoggerExtensions.LogInformation(logger, message, args);
    }

    /// <summary>Formats and writes a warning log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="exception">The exception to log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogWarning(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
    {
        LoggerExtensions.LogWarning(logger, eventId, exception, message, args);
    }

    /// <summary>Formats and writes a warning log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogWarning(this ILogger logger, EventId eventId, string message, params object[] args)
    {
        LoggerExtensions.LogWarning(logger, eventId, message, args);
    }

    /// <summary>Formats and writes a warning log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogWarning(this ILogger logger, string message, params object[] args)
    {
        LoggerExtensions.LogWarning(logger, message, args);
    }

    /// <summary>Formats and writes an error log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="exception">The exception to log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogError(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
    {
        LoggerExtensions.LogError(logger, eventId, exception, message, args);
    }

    /// <summary>Formats and writes an error log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogError(this ILogger logger, EventId eventId, string message, params object[] args)
    {
        LoggerExtensions.LogError(logger, eventId, message, args);
    }

    /// <summary>Formats and writes an error log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogError(this ILogger logger, string message, params object[] args)
    {
        LoggerExtensions.LogError(logger, message, args);
    }

    /// <summary>Formats and writes a critical log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="exception">The exception to log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogCritical(this ILogger logger, EventId eventId, Exception exception, string message, params object[] args)
    {
        LoggerExtensions.LogCritical(logger, eventId, exception, message, args);
    }

    /// <summary>Formats and writes a critical log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="eventId">The event id associated with the log.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogCritical(this ILogger logger, EventId eventId, string message, params object[] args)
    {
        LoggerExtensions.LogCritical(logger, eventId, message, args);
    }

    /// <summary>Formats and writes a critical log message.</summary>
    /// <param name="logger">The <see cref="T:Microsoft.Extensions.Logging.ILogger" /> to write to.</param>
    /// <param name="message">Format string of the log message.</param>
    /// <param name="args">An object array that contains zero or more objects to format.</param>
    public static void LogCritical(this ILogger logger, string message, params object[] args)
    {
        LoggerExtensions.LogCritical(logger, message, args);
    }
}

答案 1 :(得分:0)

查看源代码并思考一下:是的,无论函数如何,它总是会评估它。你在做什么与此类似:

string json = JsonConvert.SerializeObject(address); 
_logger.LogDebug(json);

因为在调用函数之前会评估参数。

在制作中不会发生的是,该功能不会做任何事情。