Fody/MethodTimer

Add support for calling local log method

AnderssonPeter opened this issue · 3 comments

If you add support for calling a local instance method or just using a local ILogger variable.

This would make Frody.MethodTimer much more IOC friendly and a blizz to use with .net core!

Then one could write

[Time]
class Class1
{
    ILogger<Class1> logger;
    public Class1(ILogger<Class1> logger)
    {
        this.logger = logger;
    }

    public void Test()
    {
        Thread.Sleep(1000);
    }
}

Or if using ILogger is no option

[Time]
class Class1
{
    ILogger<Class1> logger;
    public Class1(ILogger<Class1> logger)
    {
        this.logger = logger;
    }

    public void Log(MethodBase methodBase, long milliseconds, string message)
    {
        logger.LogTrace("logic here");
    }

    public void Test()
    {
        Thread.Sleep(1000);
    }
}

I know it's not 100% pure dependency injection, but you have access to the type in your MethodTimer. In that case, you could retrieve the right logger from the IoC container yourself?

We could detect the ILogger during compilation, but will it be a field, or a property, or should we just check for a type and assume that's the one to use?

@GeertvanHorrik well i would just check for a field named logger or _logger and then check its type (or that it has a method that has the same parameters).

But I'm no expert.

To be raising issues you should be a patron of Fody. Any issues raised by non Patrons may not be actively triaged, and eventually closed as stale.