I want to be able to use Ilogger to log different things in different methods for my Azure Function. As a standard the Ilogger is used in the methods parameters. But this wont work for me as I need to be able to call my methods without parameters.
Example:
public async Task Run([TimerTrigger("0 0 10 * * *")] Ilogger logger)
{
await Method2()
}
public static async Task<String> Method2()
{
Method3()
//Here I need to also log using Ilogger.
}
public static async Task<String> Method3()
{
//Here I need to also log using Ilogger.
}
The solution would be in my head to also have the Ilogger logger used as a parameter for the Method2. But this will lead to a problem, as I then need to have it in my Run method.
So I do not know how I am able to log in different methods.
I have in total 5 methods that it would be needed to log in.
I have done some research on the Ilogger, but I could not find anything relating to my direct problem.
I thought making a constructor with the Ilogger in like so:
public class Function1
{
private static ILogger<Function1> logger;
public Function1(ILogger<Function1> logger)
{
Function1.logger = logger;
}
public async Task Run([TimerTrigger("0 0 10 * * *")])
{
await Method2()
logger.Loginformation("Log stuff");
}
public static async Task<String> Method2()
{
//Here I need to also log using Ilogger.
logger.Loginformation("Log stuff");
}
...
}
I have read this could work. I have done some debugging with this and changed a bit, but without luck.
If anyone have some ideas or solution on how to solve this, please do let me know :)
EDIT:
I have made it so it is no longer static, and changed it to the following:
public class Function1
{
private ILogger<Function1> logger;
public Function1(ILogger<Function1> logger)
{
this.logger = logger;
}
public async Task Run([TimerTrigger("0 0 10 * * *")])
{
await Method2()
this.logger.Loginformation("Log stuff");
}
public async Task<String> Method2()
{
this.logger.Loginformation("Log stuff");
}
...
}
I have no exceptions, I tried to debug this, but unfortunately without luck to find the reason for the problem.