C
C#•9mo ago
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 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
6 Replies
dreadfullydistinct
dreadfullydistinctOP•9mo ago
I've seen both approaches used in grep.app https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/clients/csharp/Client.Core/src/HubConnection.Log.cs https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/ResponseCaching/src/LoggerExtensions.cs and yeah it's preference at the end of the day, just wondering what other people's preferences are and why
jcotton42
jcotton42•9mo ago
for the in-class one, you can also leave off the logger parameter and it will use the class member though not sure if it will pick up primary ctor parameters
dreadfullydistinct
dreadfullydistinctOP•9mo ago
very interesting unfortunately the generator kind of died
dreadfullydistinct
dreadfullydistinctOP•9mo ago
No description
dreadfullydistinct
dreadfullydistinctOP•9mo ago
GitHub
LoggerMessage source generator does not work with logger from prima...
Description The source generator for LoggerMessage does not work if the logger is injected from the primary constructor. Reproduction Steps Create the project. dotnet new classlib dotnet add packag...
dreadfullydistinct
dreadfullydistinctOP•9mo ago
this actually doesn't sound too too hard to fix 👀 maybe I will look (and get proven wrong)
Want results from more Discord servers?
Add your server