Determine if string interpolation is used in a method
Closed this issue · 3 comments
ovation22 commented
Is it possible to determine if string interpolation is being used in a method?
I would like to enforce the use of params object?[] args instead of string interpolation (for performance reasons).
Contrived example:
public interface ILoggerAdapter<T>
{
void LogInformation(string message);
...
void LogInformation<T0, T1, T2>(string message, T0 arg0, T1 arg1, T2 arg2);
}
public class LoggerAdapter<T> : ILoggerAdapter<T>
{
public void LogInformation(string message)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation(message);
}
}
...
public void LogInformation<T0, T1, T2>(string message, T0 arg0, T1 arg1, T2 arg2)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation(message, arg0, arg1, arg2);
}
}
}
Preferred usage:
_logger.LogInformation("Got here {0} {1} {2}", var0, var1, var2);
I would like to write a test to prevent this:
_logger.LogInformation($"Got here {var0} {var1} {var2}");
NeVeSpl commented
That kind of rules you usually enforce through Roslyn analyzers. It is not architecture, and this library is used for testing architecture.
ovation22 commented
Ok, thanks for the quick reply.
sadedil commented
Hello @ovation22, I wonder if you find a good library or roslyn analyzer that matches your needs.