Recently I setup a prototype of a WCF server application where Azure Application Insights is used for capturing runtime telemetry data as well as storage backend for operational logging data. I choose NLog as logging framework and the Microsoft.ApplicationInsights.NLogTarget
adapter for Application Insights which is still actively maintained by Microsoft (Github).
The adapter configuration was straight forward and did follow certain best practices (f.e. using dedicated config files like NLog.config or AppInsights.conf). Then I was curious about how the adapter is handling structured logs and if it has a bulking strategy for submitting multiple log enties.
Bulked submission of log enties
So let’s spin up Fiddler and see how theMicrosoft.ApplicationInsights.NLogTarget
adapter submits multiple logs. As expected, it combines multiple entries per HTTP request sent to the AI webservice. Check #1 ✅
Structured log data
Very important for my evaluation was support for the Message Templates specification. It’s “a format for conveniently capturing and rendering structured application log events“. By capturing and storing logging data from your systems in a semi-structured mannor, you lay the foundation for doing specific reportings or setting up automated monitoring rules without having to munge any unstructured text messages first using a myriad of regular expressions or other text patterns.
NLog did support custom event properties since the early days, which lays the foundation for a more fluent capturing of those that has been introduced in version 4.5. We do no longer have to put bulky literal dictionary definitions into our logging statements, but instead can use named placeholders (as opposed to ordinal ones as before and also known from String.Format(...)
and similar) which can read as simple as:
When this structured log message is processed by theMicrosoft.ApplicationInsights.NLogTarget
adapter internally, it automatically puts all custom event properties right into the baseData.properties
bag which, is meant to contain any custom event/trace data that will be stored in the AI analytics database and can later be used for doing complex reportings of the analytics data.
In the log query console of Application Insights you can then easily define conditions right on the actual log data, instead duck typing any unstructured textual data. Check #2 ✅
Summary
My first impressions of the Application Insights adapter for NLog is quite good. It’s actively maintained by Microsoft, covers several advanced aspects by default and can be effortlessly integrated to existing systems.