I know there are several closely relevant Stack Overflow questions out there, but I'm having a hard time making the mental leap to apply them to what I am working with (for example, ASP.NET Core Web API Logging from a Static Class refers to a Startup.cs, which my project does not use).
I have a .Net Core 8 web API project. I use logging like this:
// In Program.cs public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); builder.Logging.ClearProviders(); builder.Logging.AddLog4Net(); } // In appsettings.json "Logging": { "LogLevel": { "Default": "Error", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information", "Log4Net": "Error" } }, // In one of many non-static classes public class OrderNumberController : ControllerBase { private readonly ILogger<OrderNumberController> _logger; public OrderNumberController(ILogger<OrderNumberController> logger) { _logger = logger; } public ActionResult Get() { _logger.LogInformation("log something"); } } That has been working fine since I wrote this application last year. Now, I want to log some stuff in the IdentityApiEndpointRouteBuilderExtensions.cs class, which is static. I've been through a few attempts at this and I seem to find a problem every time. For example, the attempt below gets me "cannot declare instance members in a static class" and "static types cannot be used as type arguments".
private static readonly ILogger<IdentityApiEndpointRouteBuilderExtensions> _logger; Is there a way to get Log4Net working in this static class or do I need to rip out or fundamentally change the way I've been handling logging? I'm trying not to change a lot about the way this application works because it's pretty business critical and it has performed well in production.