问题
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