Output JSON in Azure Function Logs

匆匆过客 提交于 2020-07-23 06:32:26

问题


We'd like to send structured log data from our Azure Functions to Event Hub. So I set up Serilog to log to the console and include all the info I wanted. But now I come to try this in Azure, all of my nice Json formatted data from Serilog is being ignored - and only the standard ILogger output is being shown :(

Here's the config I'm using in 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));

}

Then I'm logging using ILogger in my function:

[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");
}

And locally I get both logs - the normal text C# HTTP trigger function processed a health request. but also a big JSON blob as expected. However, when running in Azure, when I look in Log Stream, I only see the normal text log :(

I can see the expected output in the file - but I want to be able to use Diagnostic settings to export the logs - and I can't select a log file there! :(

来源:https://stackoverflow.com/questions/62199104/output-json-in-azure-function-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!