Integrating with Application Insights

In this blog post we’re going to cover how you can use railtown.ai to get extra value from the telemetry you are already collecting with Application Insights.

First off, if you are using a logging framework, like Serilog, to collect logs and then forward them to Application Insights via a sink, then you can simply plug in railtown.ai as an additional sink for Serilog. This approach works for NLog and Log4Net as well and has been covered in a previous article so we won’t cover that here.

However, if Application Insights is your only source of telemetry, you can add Railtown as a Telemetry Processor. This means it is registered with the Application Insights TelemetryConfiguration and all telemetry that is sent to Application Insights goes through the railtown.ai processor as well. 

For more information about telemetry processors, please see this article: https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#filteringadd 

There are a number of ways of setting up Application Insights and choosing the right approach depends upon which version of .NET you are using. In this post, we’ll cover integrating with an ASP.NET Core web app. 

(If you’re using .NET framework and ApplicationInsights.config, the set up is very easy and you can read about that on our Support site: Using Application Insights to send logs (notion.so)

ASP.NET Core

Before we start, you’ll need a Railtown Key. You can get this from the Configuration section of the railtown.ai website. 

If you don’t already have a railtown.ai account, sign up now.

From the configure project page, go to the logs section and copy the contents of the RailtownKey node from the Application Insights xml configuration.

Install Nuget Packages

Next, you’ll need to add the ApplicationInsights.Railtown nuget package to your project: NuGet Gallery | ApplicationInsights.Railtown 1.3.0

Additionally, if you added Application Insights to your web application via the Azure Portal, you’ll need to add Microsoft’s ApplicationInsights.AspNetCore package as well: NuGet Gallery | Microsoft.ApplicationInsights.AspNetCore 2.19.0

Startup.cs

If you were already using the ApplicationInsights ASP.NET Core package, you should see a line like this inside StartUp.cs:

services.AddApplicationInsightsTelemetry();

The railtown.ai telemetry processor will be added in the same place, usually the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)

{

     // …

    services.AddApplicationInsightsTelemetry();

    services.AddSingleton(new RailtownTelemetryProcessorOptions() { 

        // put the Railtown Key here (or retrieve it from configuration like appsettings.json)        

        RailtownKey = “” 

     });

    services
        .AddApplicationInsightsTelemetryProcessor<RailtownTelemetryProcessorWithOptions>();

 }

The railtown.ai telemetry processor uses the options pattern so you will inject the Railtown Key via an options class. Add the RailtownKey value you obtained right at the beginning of this process. The snippet above shows how you’d put the key right in source code, but it’s good practice to retrieve this from configuration instead. You should treat your Railtown Key as a password and never check it into source code. You can read more about the Options pattern here: Options pattern in ASP.NET Core | Microsoft Docs

Now your application is configured to send Application Insights logs to railtown.ai. Railtown will automatically receive any events captured by Application Insights from these sources:

ILogger.Log() 

ITelemetryClient.TrackException() 

ITelemetryClient.TrackTrace() 

throw Exception // when not caught

To configure other types of .NET projects or to troubleshoot, please visit our support page here: Using Application Insights to send logs (notion.so)

I hope this has demonstrated how easy it is to use your existing Application Insights instrumentation to send logs to railtown.ai.

If you don’t already have a railtown.ai account sign up and let us help you improve the quality of your software and increase your developer velocity.