olsh / resharper-structured-logging

An extension for ReSharper and Rider that highlights structured logging templates and contains some useful analyzers
MIT License
142 stars 14 forks source link

Support for custom extension methods #111

Closed AldeRoberge closed 3 months ago

AldeRoberge commented 3 months ago

I have an extension method :

// Normal
 _logger.Information("Added entity {EntityName} with name {TypeName}", entityState.name, type.Name);

// Extension
_logger.LogInformation("Added entity {EntityName} with name {TypeName}", entityState.name, type.Name);

That passes some context information using reflection (caller file, method

public static class LoggerExtensions
    {
        public static void LogInformation(this ILogger logger, string messageTemplate, params object[] propertyValues)
        {
            var callerInfo = GetCallerInfo();
            using (LogContext.PushProperty("Method", callerInfo.MethodName))
            using (LogContext.PushProperty("FilePath", callerInfo.FileName))
            using (LogContext.PushProperty("LineNumber", callerInfo.LineNumber))
            {
                logger.Information(messageTemplate, propertyValues);
            }
        }
        // ...
    }

Is it possible to have the analysis provided by this plugin for custom logging methods?

image

olsh commented 3 months ago

Hi @AldeRoberge

To make this work, you can decorate your extension method with the [MessageTemplateFormatMethod("messageTemplate")] attribute.

AldeRoberge commented 3 months ago

Wonderful, thank you!