我们希望将结构化日志数据从我们的 Azure Functions 发送到事件中心。所以我设置了 Serilog 以登录到控制台并包含我想要的所有信息。但是现在我来在 Azure 中尝试这个,我所有来自 Serilog 的漂亮的 Json 格式数据都被忽略了——只显示标准的 ILogger 输出:(
这是我在Startup.ConfigureServices
.
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(CreateLogger()));
private static Logger CreateLogger()
{
var loggerConfig = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithCorrelationIdHeader("X-Request-Id")
.WriteTo.Console(new ElasticsearchJsonFormatter())
.WriteTo.File(new ElasticsearchJsonFormatter(),
$@"D:\home\LogFiles\Application\{applicationName}.txt",
fileSizeLimitBytes: 1_000_000,
rollOnFileSizeLimit: true,
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1));
}
然后我在我的函数中使用 ILogger 进行日志记录:
[FunctionName("Health")]
public IActionResult Health([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "users/health")]
HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a health request.");
return new OkObjectResult("Users is healthy");
}
在本地,我得到了两个日志——正常文本C# HTTP trigger function processed a health request.
,但也得到了预期的大 JSON blob。但是,在 Azure 中运行时,当我查看 Log Stream 时,我只看到普通的文本日志 :(
我可以在文件中看到预期的输出 - 但我希望能够使用诊断设置来导出日志 - 我无法在那里选择日志文件!:(