Configure Azure Application Insights With An Existing Application
In this tutorial, we will be integrating application insight with an existing application which is already writing diagnostic logs.
In a software industry, every new development is cloud oriented. New applications use services provided by cloud platform to have an easy way of monitoring but existing applications lags behind and we need to do things manually. The solution for this is we can use different listeners in our services to collect logs and push it to azure.
We will be creating an app which will log diagnostics and we will see the logs will be automatically visible on azure application insight resource by performing some steps.
Configure Service to Collect Logs
We need to install Microsoft.ApplicationInsights package to the application which will be able to collect logs and to collect the specific type of logs we need to installed packages depending upon the type of log we need to collect. Below is the list of packages which we can use.
- For ILogger: Microsoft.Extensions.Logging.ApplicationInsights
- For NLog: Microsoft.ApplicationInsights.NLogTarget
- For Log4Net: Microsoft.ApplicationInsights.Log4NetAppender
- For System.Diagnostics: Microsoft.ApplicationInsights.TraceListener
In this tutorial we will be using package of System.Diagnostics. We would need to add listener to the application configuration. We can also use filter to collect specific traces. Filter options are Warning, Error, Information.
After this we need to add the ApplicationInsights.config to the application to add the telemetry module which we have added above in the config so that the events can be sent to application insights as traces.
Now we just need to create the Telemetry Configuration to tell where to send the collected logs. For this we would need to have the instrumentation key of the resource.
Once the application is started the telemetry will be collected and sent to azure resource. We can see in the above image that we have used telemetry client to track the trace and Debug object to write log which will be collected and send it to azure.
You can see both the logs are collect to send to azure resource.
Wrap Up
This is how simple is to integrate application insights with an existing application to send logs to azure. By just adding some packages and configuration we were able to collect logs.
Thanks for reading. Hope you enjoy it.
Have a lovely day. :)