I would like to know how I can use Serilog in Azure function. I have created a startup class in my function with the following setup:
 var loggerConfiguration = new LoggerConfiguration()
            .WriteTo.Console()
            .CreateLogger();
 builder.Services.AddLogging(op => op.AddSerilog(loggerConfiguration));
How do I inject the ILogger from serilog into my Function?
using Serilog;
namespace LogFunction
public class XX
{
    [FunctionName("XX")]
    public void Run([TimerTrigger("*/60 * * * * *")]TimerInfo myTimer, ILogger log)
    {
        log.Information("Log this object {Car}", car); //using serilog not Microsoft.Extensions.Logging
    }
}
As you can see above I am referencing the ILogger from Serilog and not Microsoft Logging.
One possible solution would be that you let inject the logger in the constructor of your function class.
public class XX
{
    private ILogger _logger;
    public XX(ILogger logger)
    {
        _logger = logger;
    }
    [FunctionName("XX")]
    public void Run([TimerTrigger("*/60 * * * * *")]TimerInfo myTimer)
    {
        _logger.Information("Log this object {Car}", car); //using serilog not Microsoft.Extensions.Logging
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With