LoggerMessage organisation

Looking for some insight in how people using SG logging organise their methods.

Do you have them inside the class?

public partial class Class(ILogger<Class> logger)
{
    public void OpenSocket(string host)
    {
        SocketOpened(logger, host);
    }

    [LoggerMessage(Level = LogLevel.Information, Message = "Opened socket to `{HostName}`")]
    private static partial void SocketOpened(ILogger logger, string hostName);
}

disadvantages: have to make all classes that use logging partial (although maybe that isn't so bad), and also I like the ext method syntax more

or do I just accumulate a static Log class for extension methods
public class Class(ILogger<Class> logger)
{
    public void OpenSocket(string host)
    {
        logger.SocketOpened(host);
    }
}

public static partial class Log
{
    [LoggerMessage(Level = LogLevel.Information, Message = "Opened socket to `{HostName}`")]
    public static partial void SocketOpened(this ILogger logger, string hostName);
}

I like the ext method here but there is no way to separate the logging methods and make them private, so I can see the code completion getting huge and naming collisions becoming a problem
Was this page helpful?