Structured logging in an Azure Function v3 with Azure Log Analytics

964 Views Asked by At

I'm trying to use structured logging by logging in my Azure Function like this:

log.LogError("Failed to get object. ({customerId}, {objectId}, {errorMessage})", request.CustomerId,
                    request.ObjectId, errorMessage);

My Azure Function is plugged in to a Log Analytics workspace through Diagnostic settings.

Is there a way to extract the customerId, objectId and errorMessage from the Message ?

I would like to create for example a dashboard showing the number of errors per customerId.

I've tried to "Extract Fields" functionality to no avail. Do I need to use Application Insights and custom Dimensions ?

1

There are 1 best solutions below

1
On BEST ANSWER

You are almost already in the right path. With the example log statement you shared above, you should already see customerId, objectId, errorMessage in the customDimentions of Application Insights log if you have enabled built-in App Insights Integration. That's the exact reason, Loging in .net core is built this way for templating. You can also find details in Azure Function structured logging. enter image description here

But you mentioned you are using Log Analytics workspace through Diagnostic settings which I don't think has support for structured logging yet (and it's in preview currently, so might change in future). enter image description here

So, Application Insights would be the way to go for you. You can anyway query app insights from Log Analytics too apart from inbuilt query pane of App Insights.