Rob Bowman
Rob Bowman

Reputation: 8711

Serilog not logging from queue triggered azure function

I need to use dotnet5 with Azure Functions so followed the guidance to create a new solution: https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide.

This worked great so next job was to add in serilog with sinks for console and sql server.

I have added nuget packages:

Here is the Program.Main:

{
    string EventName = "Main";
    
    Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Override("Microsoft.Azure", LogEventLevel.Warning)
                .Enrich.FromLogContext()
                .WriteTo.Console()
                .WriteTo.MSSqlServer(
                    logEventFormatter: new RenderedCompactJsonFormatter(),
                    restrictedToMinimumLevel: LogEventLevel.Debug,
                    connectionString: "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SmsRouter",
                    sinkOptions: new MSSqlServerSinkOptions
                    {
                        TableName = "LogEvents",
                        AutoCreateSqlTable = true,
                    })
                .CreateLogger();
    Serilog.Debugging.SelfLog.Enable(Console.Error);

    try
    {
        Log.Information("Starting up {EventName}", EventName);
        var host = new HostBuilder()
        .UseSerilog()
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(s =>
        {
            //services configured here
        })
        .Build();

        host.Run();
    }
    catch (Exception ex)
    {
        Log.Fatal(ex, "Application start-up failed");
    }
    finally
    {
        Log.CloseAndFlush();
    }
}

The 1st trigger to the FunctionApp is a Http Request into the following:

[Function("SendSimpleSms")]
    public async Task<QueueAndHttpOutputType> RunSimple([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
        FunctionContext executionContext)
    {
        HttpResponseData httpResponse;
        SmsOrder coreSmsOrder = new SmsOrder();
        string EventName = "SendSimpleSms";
        string CorrelationId = executionContext.FunctionId;
        try
        {
            Log.Information("Starting: {EventName}", EventName);

The Log.Information call here works great and a log is written to the console and Sql.

The "SendSimpleSms" function then queues a messages to be collected by the following function - running in the same app as "SendSimpleSms":

[Function("QueueOrchestration")]
    public async Task Execute([QueueTrigger("%SendSmsQueueName%")] SmsOrder smsOrder, FunctionContext executionContext)
    {
        Log.Information("Will this log?");
        Log.CloseAndFlush();

This time, nothing is logged to the console or Sql.

Any ideas why the logging would work when a function is triggered by http but not from a queue?

Upvotes: 0

Views: 524

Answers (1)

Rob Bowman
Rob Bowman

Reputation: 8711

Still using Serilog (and benefiting from the Sql Server Sink) but I've switched to using an ILogger injected into the constructor rather than the global Log object.

Upvotes: 1

Related Questions