serilog-extensions-hosting icon indicating copy to clipboard operation
serilog-extensions-hosting copied to clipboard

Azure Isolated Function does not route logs to serilog

Open waynebrantley opened this issue 1 year ago • 4 comments

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?

waynebrantley avatar Sep 05 '24 13:09 waynebrantley

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

    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddSerilog();
    })

nblumhardt avatar Sep 05 '24 22:09 nblumhardt

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

waynebrantley avatar Sep 07 '24 13:09 waynebrantley

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.

nblumhardt avatar Sep 09 '24 05:09 nblumhardt

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.

waynebrantley avatar Sep 11 '24 16:09 waynebrantley

Closing as I don't think there's anything to action in this one, currently.

nblumhardt avatar Nov 27 '24 04:11 nblumhardt