c# – How to achieve message template highlighting for structured loggin?


When using the Microsoft.Extensions.Logging.ILogger<T> with message templates for structured logging, Visual Studio highlights the message template “named holes”/parameters like this (see {Address}):

Message template with highlighting

Is it possible to achieve the same highlighting effect at the call site for one of your own methods?

If so, how? What are the mechanics behind the highlighting?


Background (but I’m curius in the general case):

We’re using an exception filter method as discussed here, like this:

public static class LoggerExtensions
{
    public static bool LogExceptionNoCatch<T>(this ILogger<T> logger, Exception exception, string? message, params object?[] args)
    {
        logger.LogError(exception, message, args);
        return false;
    }
    // ...
}

However, when using this extension method there is no highlighting indicating that message templates is being used:

Message template withoud highlighting

Leave a Reply

Your email address will not be published. Required fields are marked *