roslynator
roslynator copied to clipboard
Feature Request: Refactor log message to use LoggerMessageAttribute
A very useful refactor would be to refactor logger.LogDebug() into the performance optimized LoggerMessageAttribute method.
Reference:
- https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator
- https://learn.microsoft.com/en-us/dotnet/core/extensions/high-performance-logging
Before Refactor:
public class Program
{
private readonly ILogger _logger;
public Program(ILogger logger)
{
_logger = logger;
}
void Main(string[] args)
{
var message = "Blah";
_logger.LogDebug("This is a message: {message}", message);
}
}
After Refactor:
public partial class Program
{
private readonly ILogger _logger;
public Program(ILogger logger)
{
_logger = logger;
}
void Main(string[] args)
{
var message = "Blah";
LogMessage(_logger, message);
}
[LoggerMessage(1000, LogLevel.Debug, "This is a message: {message}")]
static partial void LogMessage(ILogger logger, string message);
}
@JosefPihrt I'd like to try to implement this refactoring as a pull request to this project. Looking at the code, its not apparent where to get started. Do you have a suggestion on where I could get started? Is there a sample refactoring I could look at and follow?
thanks, ~ Paul
Hi,
I created a tutorial that should help you to start with a development of a new refactoring. If you need any help just let me know.
Please keep in mind that the documentation is "work in progress" so the link may change. But the page will be somewhere on the website. Any feedback on the documentation is appreciated.
Regarding the actual development process my advice would be to discuss anything unclear early in the PR than to present complete work at the end.