serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with published Azure Function v3 writing to Azure Sql Database

jgraygi opened this issue · comments

I have created an Azure Function App .NET 3.1. I have added this sink to the application and it works great when I run under the debugger in Visual Studio 2019. Once I publish it to Azure and set all my configurations I don't get any logs in my database. I don't get any errors and the expected logs write to the Application Insights but none get written to my database. Is there something specific I need to do in Azure to make this work?

using AGN.USB.Cryptography;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(Startup))]

namespace AGN.USB.Cryptography
{
using Logging;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Models;
using Serilog;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Sinks.MSSqlServer;
using Services;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        var blogCollection = new BlogCollection
        {
            Deciphered = Environment.GetEnvironmentVariable("Decrypted"),
            Uploaded = Environment.GetEnvironmentVariable("Uploaded"),
            Unprocessed = Environment.GetEnvironmentVariable("Unprocessed"),
            Processed = Environment.GetEnvironmentVariable("Processed")
        };

        var connString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
        var sqlConnString = Environment.GetEnvironmentVariable("SQLServerConnString");
        var logTable = Environment.GetEnvironmentVariable("LogTable");


        var options = new ColumnOptions
        {
            AdditionalColumns = new Collection<SqlColumn>
            {
                new SqlColumn{ColumnName = "Application", DataLength=128, DataType = System.Data.SqlDbType.NVarChar, AllowNull = true }
            }
        };

        
        //Log.Logger = new LoggerConfiguration()
        var logger = new LoggerConfiguration()
            .Enrich.FromLogContext().Enrich.With(new ApplicationNameEnricher())
            .WriteTo.Console(
                outputTemplate:
                "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}")
            //.WriteTo.RollingFile("log.txt", LogEventLevel.Information,"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}")
            .WriteTo.MSSqlServer(
                sqlConnString,
                logTable,
                columnOptions: options)
            .MinimumLevel.Information()
            .MinimumLevel.Override("Function.AGN.USB.Cryptography.User", LogEventLevel.Information)
            .CreateLogger();
        
        SelfLog.Enable(Console.Error);
       // var loggingProvider = new SerilogLoggerProvider(Log.Logger);

         builder.Services.AddOptions<KeyVaultProperties>().Configure<IConfiguration>((kv, configuration)=>{
            configuration.GetSection("SAKeyVault").Bind(kv);
            });
        builder.Services.AddScoped<IKeyVaultService, StorageAccountKeyVaultService>();
        builder.Services.AddScoped<IBlobStorage>(x => new BlobStorage(x.GetRequiredService<ILogger<BlobStorage>>(), connString));
        builder.Services.AddScoped<ICryptographyService>(x => new CryptographyService(
            x.GetRequiredService<ILogger<CryptographyService>>(), x.GetRequiredService<IBlobStorage>(),
            blogCollection));
        builder.Services.AddLogging(lb => lb.AddSerilog(logger));
       // builder.Services.AddSingleton<ILoggerProvider, SerilogLoggerProvider>(_ => loggingProvider);
    }
}

}

Hello, I am facing a similar issue but instead of SQL server I am not able to see the Serilog entries in the Logger Console for Function App. Did you find a solution ?

Hi, sorry we couldn't answer your question here; if you're still looking for information on this, tagging a Stack Overflow post with the serilog tag will get the right eyes onto it. Hope this helps!