Using Custom ILogger in Azure Functions


  • My integration testing has benefited dramatically thanks to the new ILogger implementation in Azure Functions.  I can pass my own ILogger of choice into my function and route the log messages however I wish.

    However, someone asked understandable question in, which made me wonder why I didn't think of it. 

    If we have a Serilog ILogger that we want to use throughout our function at runtime, how would that work today? 

    The ideal "scenario" is that the ILogger that comes into our function does all the things which it does today (logging to azure log files, and the live debugger in the portal), but also executes custom logging behavior.  I'm pretty sure this scenario isn't really possible today, to date I have seen no option/intention to support custom interact with the ASPNET startup processes. 

     So it seems the path to achieve would be to have the first line in every function take the ILogger we get from Azure, and put it in a wrapper class along with our custom logger instance and write to both of them with something like wrapper.loglevel(logmessage) 


    Thursday, May 25, 2017 5:17 PM

All replies

  • That's where I am.  I'm trying to share some repository and business code between my Asp.Net site and the functions that augment them.  Since I originally wrote these for the web site, I used the ILogger<T> in all my calls and dependent objects.

    Problem is, the Asp.Net DI requires ILogger<T> and Azure functions won't let me specify that.  Further, I can't figure out how to create an ILogger<T> on the fly to pass into my shared services and business logic.

    I found this article about a wrapper for something I would write.  Pretty sure that would work OK.  Haven't tried it yet.

    Fiddling with Serilog as a replacement on both ends for now...

    Thursday, June 8, 2017 7:04 PM
  • I opened an issue to track this just a couple of days ago. I'd like to expose a way for you to register your own LoggerProviders with our LoggerFactory so all logging would flow through it.

    You can follow the issue here:

    Feel free to list your scenarios in that issue so I can make sure we take everything into account.

    Brett Samblanet -- Azure Functions

    Thursday, June 8, 2017 7:44 PM