serilog / serilog-extensions-hosting

Serilog logging for Microsoft.Extensions.Hosting
Apache License 2.0
141 stars 34 forks source link

Azure Isolated Function does not route logs to serilog #93

Open waynebrantley opened 2 months ago

waynebrantley commented 2 months ago

nothing that the function framework outputs ends up in serilog. This could be exceptions from any host extension like sqlTrigger.

Any injection of ILogger does work as expected.

Reproduction: ``func init

That gives you a base function project. Change program.cs to:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .Enrich.FromLogContext()
    .WriteTo.Seq("http://localhost:5341")
    .CreateLogger();

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
        services.AddSerilog(Log.Logger);
    })
    .Build();

host.Run();

When you run it you will see lots of output on the console screen - and none of that ends up in seq (did not go through serilog).

Add a function to the project (and the Microsoft.Azure.Functions.Worker.Extensions.Sql package):

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.Sql;

namespace Company.FunctionApp1;

public static class SqlTrigger
{

    [Function("SomeTrigger")]
    public static void Run(
        [SqlTrigger("[dbo].[SomeTable]", "Main.ConnectionString")]
        FunctionContext context)
    {
    }
}

Run that and you will see lots more logs and errors about the connection string not being valid - again none of that is in seq.

There are logs in seq (going thru serilog) in the above example from Microsoft.AspNetCore.DataProtection and Microsoft.Hosting, etc - so that part of the serilog works - but anything from function app does not.

Not sure if this is an issue with logging not setup properly by AddSerilog or if this is an issue with https://github.com/Azure/azure-functions-host or https://github.com/Azure/azure-webjobs-sdk

Thoughts?

nblumhardt commented 2 months ago

G'day Wayne - I think you want to ConfigureLogging() (instead of adding Serilog directly to services) in this case:

    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddSerilog();
    })
waynebrantley commented 2 months ago

Nice to hear from you Nicholas. I tried the above and still so many items 'logged' to the console including errors and such when you do a basic 'broken' setup like described above. Not sure if this is isolated function thing or what. If you give it a shot you will see all the logs that are missed. There are like 5 total logs written through serilog - and probably 30 logs on the console output.

also there is not a AddSerilog((provider, loggerConfiguration) overload in this, so cannot do my normal thing

nblumhardt commented 2 months ago

Hmm interesting --- any chance you can spot a difference between the messages that make it through the Serilog provider, and the ones that hit the console?

I'm unfortunately not familiar with Azure Functions; looks like previous similar questions on Stack Overflow have found some qualified eyes (https://stackoverflow.com/questions/71034036/how-to-setup-serilog-with-azure-functions-v4-correctly) - posting a question with some examples over there might be your best bet.

waynebrantley commented 2 months ago

While I get serilog logs - no errors or anything from the host or providers (like SQL) do not go thru serilog. This is likely a azure function host limitation.