One of the purpose of logging is monitoring your application for critical aspects of your application, such as performance, errors, user behaviors and much more. Azure Application Insights provides a repository that holds these kind of logs and presents them in Azure Application Insights reporting tools, enabling you to easily monitor your application in real time.
This page gives you an overview of Azure Application Insights Logging and how to enable Crosslight Logging Framework to work with Azure Application Insights.
Azure Application Insights provides an online repository where you can store your application logs and monitor it directly. To use this service, you will need an active Microsoft Azure subscription. In the Azure portal, you must create a new Application Insights Source for your application, please refer to the following links for details.
After you have created the Application Insights Source, you will get the instrumentation key to be used in your application.
Enabling Azure Insights Application Logging
To enable Azure Insights Application Logging in your application, you need to do the following steps.
- Configure your core project to use IAzureInsightService.
- Configure your WebAPI project to send your application log to Azure Insights Source.
- Register IAzureInsightService with your Azure Insights InstrumentationKey.
- Use AzureInsightEventSource to track your telemetries.
Configuring Core Project
To enable Azure Insight application logging, first you need to configure your projects to add the Crosslight Logging Extensions, namely Intersoft.Crosslight.Logging.AzureInsights and Intersoft.Crosslight.Logging.AzureInsights.Server. In the core project, you must add reference to Intersoft.Crosslight.Logging.AzureInsights as follows.
Configuring Web Api Project
Next, you need to configure your WebAPI project, please add reference to Intersoft.Crosslight.Logging.AzureInsights.Server as follows.
Afterward, you will also need to add Application Insights for WebApplications package from Nuget.
Lastly, you need to create a WebApiController that accepts the Azure Insights Telemetries from your application as follows.
After your projects are configured, you can then register the IAzureInsightService in your AppService class as follows.
Using AzureInsightEventSource to Track Telemetries
You can track custom telemetries by using AzureInsightsEventSource such as follows.
Using AzureInsightsApplicationEventSource to Track Application Events
By default, Crosslight Azure Insights component already provides a built-in AzureInsightsApplicationEventSource class which automatically track these events. More importantly, Crosslight Azure Insights component already take care all the heavy-lifting behind the scene such as:
- Store the events to the local storage when there's no Internet connection.
- Monitoring the wifi connection availability and send the stored events to the web server when the connection becomes available.
- Manage the queues to send the stored events in batch.
You can modify how the AzureInsightsApplicationEventSource works by implementing one of the following methods:
- Create a new class that implements IApplicationEventSource and then manually track your telemetries.
- Inherit from AzureInsightsApplicationEventSource and override the methods you want to alter.
Finally, you register the IApplicationEventSource in your AppService using the following code.
To learn more about application logging in Crosslight and see how it works, please checkout the following samples:
- Application Logging samples, demonstrates the logging best practice in Crosslight application – http://git.intersoftpt.com/projects/CROS-SUPP/repos/application-logging-sample/browse
- Logging samples, demonstrates each logging feature and built-in trace listeners available in Crosslight Logging Framework – http://git.intersoftpt.com/projects/CROS-SUPP/repos/logging-samples/browse