3
votes

I am using the relatively new ILogger (vs. TraceWriter) option in Azure functions and trying to understand how logs are captured.

Here's my function:

    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
    {
        log.LogTrace("Function 1 {level}", "trace");
        log.LogWarning("Function 1 {level}", "warning");
        log.LogError("Function 1 {level}", "error");

        return req.CreateResponse(HttpStatusCode.OK, "Success!!!!");
    }

When I look at the server logs, the LogFiles directory has a hierarchy.

enter image description here

The yellow highlighted file includes my log statements:

2017-08-19T13:58:31.814 Function started (Id=d40f2ca6-4cb6-4fbe-a05f-006ae3273562)
2017-08-19T13:58:33.045 Function 1 trace
2017-08-19T13:58:33.045 Function 1 warning
2017-08-19T13:58:33.045 Function 1 error
2017-08-19T13:58:33.075 Function completed (Success, Id=d40f2ca6-4cb6-4fbe-a05f-006ae3273562, Duration=1259ms)

The structured directory contains nothing here, but it seems to have various "codeddiagnostic" log statements in my real function applications directory.

What should I expect here? Ultimately, I would like to have a single sink for logging from all of my application components and take advantage of structured logging across the board.

2

2 Answers

4
votes

The best way to collect structured logging from Azure Functions is to use Application Insights. One you defined that your Logger is based on ILogger, you can define a Template that specifies the properties you want to log. Then on Application Insights traces, using the Application Insights Query Language (aka Kusto) you can access the values of each of these properties with the name customDimensions.prop__{name}.

You can find a sample of how to do this with Azure Functions v2 on this post https://platform.deloitte.com.au/articles/correlated-structured-logging-on-azure-functions

1
votes

I had the same question. The log file logger doesn't really respect structured logging, but if you use AppInsights for Azure Functions it will in fact add custom properties for your structured logging

I had a conversation going about it here https://github.com/Azure/azure-webjobs-sdk-script/issues/1675