BenMorris / NetArchTest

A fluent API for .Net that can enforce architectural rules in unit tests.
MIT License
1.41k stars 83 forks source link

Determine if string interpolation is used in a method #107

Closed ovation22 closed 1 year ago

ovation22 commented 1 year ago

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 1 year ago

That kind of rules you usually enforce through Roslyn analyzers. It is not architecture, and this library is used for testing architecture.

ovation22 commented 1 year ago

Ok, thanks for the quick reply.

sadedil commented 1 year ago

Hello @ovation22, I wonder if you find a good library or roslyn analyzer that matches your needs.