我已经编写了一个返回ILogger实例的函数。这样,我就可以在所有代码中获得一致的日志记录。
我的问题是,当从不同类型的运行时/程序中调用时,某些运行时记录有时会失败!即如果我将日志记录与WebAPI一起使用,那么一切都很好,但是如果我在Azure函数V1中使用它,它将永远无法工作。
记录器不会抛出或以其他方式指示任何错误。我唯一失败的迹象是,在Kibana / ElasticSearch中什么都没有显示。
我如何调试/疑难解答使用ElasticSearch的serilog接收器创建的Ilogger实例?
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);
}