Thanks to Richard for pointing me in the right direction here.
I ended up implementing this by creating a custom attribute like so (NB - namespace is unimportant, but the class name must be this specifically):
[AttributeUsage(AttributeTargets.Method)]
public sealed class MessageTemplateFormatMethodAttribute: Attribute
{
public string FormatParameterName { get; }
public MessageTemplateFormatMethodAttribute(string formatParameterName)
{
FormatParameterName = formatParameterName;
}
}
This can then be used on a method, passing in the nameof
the string parameter:
public static class ILoggerExtensions
{
[MessageTemplateFormatMethodAttribute(nameof(message))]
public static void LogCustom(this ILogger logger, string? message, params object?[] args)
{
logger.LogDebug(message, args);
}
}
This gives us support in the IDE: