0

In DotNetCore Microsoft.Extensions.Logging

If I set logging level to Information, will

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

evaluate

JsonConvert.SerializeObject(address)

part? If yes any idea how to prevent that so it won't use excessive resources in production?

Thanks in advance

Steven
  • 166,672
  • 24
  • 332
  • 435
cilerler
  • 9,010
  • 10
  • 56
  • 91
  • That depends entirely on how you set up the log levels in your Startup file. If you use Debug only for Development, then it will still evaluate it but not log it (I believe) – Camilo Terevinto Nov 16 '16 at 23:15
  • That's my point and I was looking into the code and didn't see any definition to use `[Conditional]` etc. _I tough it is smart enough to define those based on the environment but couldn't find any so need hard evidence. – cilerler Nov 16 '16 at 23:19
  • I really wouldn't do that just for logging though... The ASP.NET Core logging middle ware will automatically log the output if you return that, IIRC – Camilo Terevinto Nov 16 '16 at 23:28
  • I wouldn't neither but still want to have meaningful logs **IF** i want to run those and don't want to use `#if DEBUG` definitions all over the code since we already have `[Conditional("DEBUG")]` and I just want to hear that they implemented it. – cilerler Nov 16 '16 at 23:33

2 Answers2

1

Implementing an extension method with "conditional" attribute serves the purpose.
Also I have a confirmation that they don't have any implementation so far 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);
    }
}
cilerler
  • 9,010
  • 10
  • 56
  • 91
0

Looking at the source code and thinking a little: yes, it will always evaluate that regardless of the function. What you are doing is similar to this:

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

Because the parameters are evaluated before calling the function.

What won't happen, in production, is that the function won't do anything.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • In order me to accept this answer I need a workaround. I already had the same conclusion when I asked the question therefore I asked for workaround since we have this capability/implementation on _Pre-Core_ world – cilerler Nov 16 '16 at 23:55
  • I can think of 3 workarounds: use #if, use [Conditional] methods, or leave debug code outside of production code – Camilo Terevinto Nov 17 '16 at 00:00
  • Using me `[Conditional]` won't solve anything since its _Microsoft.Extensions.Logging_ that has to handle it. `#if` is last option, ugly one and separating code for debug/production is not an elegant way to handle this. So I hope folks from @Microsoft will read this and adds those beautiful `[Conditionals]` into the _Microsoft.Extensions.Logging_ – cilerler Nov 17 '16 at 00:05
  • @cilerler perhaps it'd be better if you published this to the ASP.NET Core github page. You'll at least get answers from the ASP.NET team – Camilo Terevinto Nov 17 '16 at 00:07
  • I will post it to [here](https://github.com/aspnet/Logging/issues) if I don't get any response from the StackOverflow community. Thanks! – cilerler Nov 17 '16 at 00:08