'ILoggerProvider with generic types

colleagues!

Im trying to create app with Onion arch and several GUI. I used lib Log4net as my log provider. I created my own IMyLoger interface at application layer, then implement this interface at infrastructure layer, and created an IMyLoger to ILogger adapter. But I recieved a trouble when I tried to implement ILogProvider intrface. Trouble is consist that ILog provider doesnt contain "Create"-methods with generic ILogger.

I think, that it is possible in the ASP.NET Core, becuse ILogger is used at default Controllers template.

So, how can I add ILogger at ILoggerProvider's method Create?

This is my AppLayer interface:

public interface IVbLogger<TState> where TState : class
{
    void Debug(string message);
    void Debug(string message, Exception ex);
    public void Warn(string message);
    void Warn(string message, Exception ex);
    void Info(string message);
    void Info(string message, Exception ex);
    void Error(string message);
    void Error(string message, Exception ex);
    void Fatal(string message);
    void Fatal(string message, Exception ex);



}

Log Adapders code:

public class Log4NetAdapter : ILogger<Log4NetAdapter> 
{
    readonly IVbLogger<Log4NetAdapter> _logger;
    
    public Log4NetAdapter()
    {
        _logger = new VbLogger<Log4NetAdapter>();
    }

    public IDisposable BeginScope<TState>(TState state)
    {
        return null;
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return _logger != null;
    }
   
    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
    {
        Action<string, Exception> act;
        switch (logLevel)
        {
            case LogLevel.Debug:
                act = _logger.Debug;
                break;
            case LogLevel.Information:
                act = _logger.Info;
                break;
            case LogLevel.Trace:
                act = _logger.Debug;
                break;
            case LogLevel.Warning:
                act = _logger.Warn;
                break;
            case LogLevel.Error:
                act = _logger.Error;
                break;
            case LogLevel.Critical:
                act = _logger.Fatal;
                break;
            default:
                return;
        }

        act.Invoke(formatter(state, exception), exception);  
    }
}

And finally ILoggerProvider:

public class Log4NetProvider : ILoggerProvider
{
    public ILogger CreateLogger(string categoryName)
    {
        return new Log4NetAdapter();
    }

    public void Dispose()
    {
        
    }
}


Solution 1:[1]

It looks like you are facing issues while writing the logs.

To write the logs, you could refer to the code example below.

private readonly ILogger<HomeController> logger;

public HomeController(ILogger<HomeController> logger)
{
    this.logger = logger;
}

public IActionResult Index()
{
    this.logger.LogDebug("Creating /Home/Index");
    return View();
}

For more information, you could see this answer

Below are some helpful documents.

  1. How to use Log4Net with ASP.NET Core for logging
  2. Microsoft.Extensions.Logging.Log4Net.AspNetCore

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Deepak-MSFT