Val
Val

Reputation: 1842

Custom logger extension method

I'm building a MAUI app where I added MetroLog. I'm now trying to create a custom logging method which would be an extension for the ILogger.

public static class LoggerCustomExtensions
{
    public static void LogErrorAndNotifyUser(this ILogger logger, string? message, params object?[] args)
    {
        logger.Log(LogLevel.Error, message!, args!);
        ShowErrorMessage();
    }
}

Then the logger is injected into class constructors elsewhere in the code.

private readonly ILogger<MyClass> _logger;
public MyClass(ILogger<MyClass> logger)
{
    _logger = logger;
}

Unfortunately, the method is not visible when I try to call it from the injected instance.

_logger.LogErrorAndNotifyUser("Failed to read items {0}. Error: {1}", ex.Message);

What am I doing wrong?

Upvotes: 0

Views: 174

Answers (0)

Related Questions