Azure-functions-host: More advanced log configuration

Created on 10 May 2019  路  15Comments  路  Source: Azure/azure-functions-host

Microsoft.Azure.Functions.Extensions brought us the first attempt of being able to configure custom logging providers in Functions. With the current solution (where I can inject an ILoggerProvider), I can configure functions to log everything to a single log output. I would like to be able to do much more advanced logging configuration, though. Being able to register multiple logging provider, decide which log levels goes into which logs, and other features like that, is an essential part of most applications today.

Ideally, logging would be configured like any other application using Microsoft.Extensions.Logging:

[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            ...
            builder.Services.AddLogging(logging =>
            {
                logging.AddConfiguration(config.GetSection("Logging"));
                logging.AddConsole();
                logging.AddDebug();
                logging.AddEventSourceLogger();
            });
        }
    }
}
docs

All 15 comments

We do not have plans to support custom logging providers in functions currently. @fabiocav, correct me if I am not right.

That is actually supported with the new DI capabilities.
The documentation here shows the explicit registration of a logger provider: https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection

We're working on more detailed documentation for this scenario

@fabiocav It's perfectly fine to close this issue if it isn't something you are planning to work on. But for future views on this issue, I just want to point out that logging as described in the original question isn't supported with the current DI capabilities. Unless I've misunderstood something, you can only register a logger provider but not actually log to multiple locations, configure which log levels goes where, and all of the other features of Microsoft.Extensions.Logging.

@ThomasArdal that's fair. A lot of what you've mentioned above is supported and we're lacking documentation on how to do that with what is currently exposed. Can you clarify your distinction between registering a logger provider and logging to multiple locations?

I'll reopen that as requested so we can track the documentation updates and any other potential work.

@fabiocav Great. I'll look out for the documentation. I was looking to do something like this, which either isnt't supported or I cannot get to work:

builder.Services.AddLogging(logging =>
{
    logging.AddConfiguration(config.GetSection("Logging"));
    logging.AddConsole();
    logging.AddDebug();
    logging.AddEventSourceLogger();
});

Same here... can anyone show how to accomplish what @ThomasArdal is trying to do? Currently my logging is only going into app insights, but doesn't show up in the azure portal log window. I only see the built-in messages.

Not to pile up on this issue, but I know that a function will accept an ILogger in its method params and Azure Functions will provide it, but if another injected service wants an ILogger what is the best way to get that instance to it?

I am fine with the app insights-based ILogger instance being provided to the function method params to be provided elsewhere, but it seems that if I create and register a service with a dependency on ILogger it doesn't get that same instance.

Sorry I just noticed someone had posted about this over on the extensions repo where the DI extension lives: https://github.com/Azure/azure-functions-dotnet-extensions/issues/10

Any news on an example using custom logging from Azure Functions?

@fabiocav This issue can be closed (IMO). I got this working by changing the way my ILoggerProvider is registered.

For anyone else interested in seeing how to configure a custom ILoggerProvider with Azure Functions, there's a sample available here: https://github.com/elmahio/Elmah.Io.Extensions.Logging/tree/master/samples/Elmah.Io.Extensions.Logging.Function.

One thought - if we haven't added more detailed examples in the project / documentation on MS docs, then perhaps we can take several of the ones listed in this issue (as well as the IConfiguration issue I started), and put them in the project where most devs go to look?

Some extended documentation would be nice. Also, if there are some additional requirements for how to implement a new ILoggerProvider in order for it to work with Functions, it would be nice to write them somewhere. I don't know if my provider was just implemented poorly, but it worked with both console and ASP.NET Core in the old version. So, something definitely works differently there when including it in Functions.

Maybe someone around here can point us in the right direction of how to make this happen. Can we get one or two new issues created and label them as required documentation tasks. Then there we can discuss proper examples, and eventually get pull requests made out of them.

Good idea. I think this issue should be closed once the new issues have been created. This issue signals that custom providers aren't supported, which is not the case 馃槃

Thank you, all!

Will leave this open and flag this for follow up with @jeffhollan so we can prioritize documentation.

Was this page helpful?
0 / 5 - 0 ratings