© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
10 replies
dreadfullydistinct

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);
}
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
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);
}
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Warning CA1848 : For improved performance, use the LoggerMessage delegates...
C#CC# / help
2y ago
Discord.Net + MagicOnion, heheh
C#CC# / help
3y ago
❔ Classes in a namespace
C#CC# / help
3y ago
✅ Good resources to understand complex classes
C#CC# / help
8mo ago