ApplicationInsightLogging is a small library that writes logs and exceptions to Application Insights.
-
Install the library via its NuGet package.
-
Add the 'services.AddApplicationInsightLogging' type to the Startup.cs service registration section.
-
Inject an ILogHelper wherever you would like to use it.
This library allows you to log information, warning and error events.
It stores the information and warning logs under custom events in application insights, which can then be filtered by name using either 'Information' or 'Warning'.
When registering the library for dependency injection, there are two versions of the constructor. When using the SettingName and IConfiguration version, it allows you to either enable/disable individual logging elements based on a configuration setup.
The SettingsName parameter is used to tell the IConfiguration what parent element it should be looking for in the configuration.
The Configuration parameter is used to extract the settings and serialize them into an internal options object, that gets checked internally before firing each logging function.
Below is the structure the configuration will need to be in, (this could be from the appsettings.json file).
{
"SettingsNameParameterValue": {
"LogInformation": true,
"LogWarnings": true,
"LogErrors": true
}
}
When using any of the logging functions, the below is always injected into the Custom Dimensions, along with any parameters that have been supplied to the functions.
- SpanId
- ParentId
- RequestId
- TraceId
- LogTimeStamp (utc)
- LogMillisecondsStamp (utc)
If you are working locally and would prefer not to send data to application insights, you can either use the configuration settings version to turn off the logger or you can use the 'GenericLogger' to replace the 'ApplicationInsightLogger'. This can be done by using 'AddDevelopmentApplicationInsightLogging' at the service registration instead of 'AddApplicationInsightLogging'.