'Logging to elastic search with serilog and ILogger

I have written a function to return an ILogger instance. This way I can get consistent logging in all my code.

My problem is that when called from different types of runtimes/programs the logging sometimes fails with some runtimes! I.e. if I use logging with WebAPI everything works great, but if I use it in an Azure function V1, it never works.

The logger does not throw or otherwise indicate any errors. My only indication that it fails is that nothing shows up in Kibana/ElasticSearch.

How do I debug/troubleshoot an Ilogger instance made with a serilog sink for ElasticSearch?

public static Microsoft.Extensions.Logging.ILogger CreateILogger(string loggerName, string elasticPassword, string elasticIndexName = "logs-index-{0:yyyy.MM}")
{
    var elasticNodeURI = "https://1234567890.southeastasia.azure.elastic-cloud.com:9243";
    var elasticTemplateName = "logstemplate";

    var serilogLogger = new LoggerConfiguration()
        .Enrich.FromLogContext()
        .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticNodeURI))
        {
            IndexFormat = elasticIndexName,
            AutoRegisterTemplate = true,
            InlineFields = true,
            TemplateName = elasticTemplateName,
            ModifyConnectionSettings = x => x.BasicAuthentication("LogUser", elasticPassword),
            AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
            MinimumLogEventLevel = Serilog.Events.LogEventLevel.Debug,
            CustomFormatter = new ElasticsearchJsonFormatter()
        });

    var loggerFactory = (ILoggerFactory)new LoggerFactory();
    loggerFactory.AddSerilog(serilogLogger.CreateLogger());

    return loggerFactory.CreateLogger(loggerName);
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source