nuthanmurari
nuthanmurari

Reputation: 59

Do we really need ApplicationInsights.config file in C#.NET project?

To log a piece of information like errors, information, warnings and many more we used log4NET library and logging in a local file. But our new requirement, Along with file we need to log this information into Azure Application Insights.

We are working with .NET C# Library,

Is really required ApplicationInsights.config file with .NET C# Project? If not what is the best approach to solving this problem.

Is caching applicationInsights.config instrumentKey? if yes then what will be the solution?

Scenario 1: Tried without adding ApplicationInsights.config file Output: Log4Net logs are not adding in Azure Application Insights

Scenario 2: Tried with adding ApplicationInsights.config file Output: Log4Net logs are coming

Scenario 3: When I add Nuget API for ApplicationInsights.Log4NetAppender, It creates log4Net section and includes AIAppender. We have separate log4Net.config and it has only File Appender. We want to use only log4Net.config and remove log4net section from App.Config file. Output: I removed log4Net section from App.Config and added it in log4Net.config and tested but logs are not adding in Application Insights.

Scenario 4: Implemented TelemetryClient through C# code with ApplicationInsights.config logs are coming in Application Insights

TelemetryClient telemetryClient = new TelemetryClient();
telemetryClient.InstrumentKey =<Your Instrument Key>;

// To test
telemetryClient.TrackTrace("Test through code");

Upvotes: 2

Views: 8111

Answers (1)

JotaBe
JotaBe

Reputation: 39055

The configuration of Application Insights can be done by any combination of config file and code: only the one, or the other, or both of them.

If you find an ApplicationInsights.config file in your project, it was most probably created by installing one of the Application Insights (AI) Nuget packages, but you can remove it and make the same configuration by code.

To allow the mix of configuration by config file and code, the AI clients and configuration classes have GetDefault() or Active static members that give access to an object which can be used globally. For example, you can see that the Log4Net appender TelemetryClient.cs uses the active telemetry configuration:

public TelemetryClient() : this(TelemetryConfiguration.Active)

If you check the docs for TelemetryConfiguration.Active you'll see this:

Gets the active TelemetryConfiguration instance loaded from the ApplicationInsights.config file. If the configuration file does not exist, the active configuration instance is initialized with minimum defaults needed to send telemetry to Application Insights.

NOTE: Things are a bit different depending on the platform. For example in .NET Core is more advisable to use dependency injection. Have a look under at your case under Code-base monitoring.

For example, if you have a configuration file like this:

<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
  <InstrumentationKey>exxxxxxe-axxf-4xx6-bxx2-7xxxxxxxxxx3</InstrumentationKey>

this would take the instrumentation key from it:

  _configuration = TelemetryConfiguration.CreateDefault();

If you remove the config file, this will not fail, but you'll need to do set the instrumentation key in code:

_configuration.InstrumentationKey = "exxxxxxe-axxf-4xx6-bxx2-7xxxxxxxxxx3";

If your config file include intializers, modules or whatever, you can also configure them by code. For example:

 <TelemetryInitializers>
        <Add Type="...MyInitializer, ..." />
    </TelemetryInitializers>
    

can be setup in code like this:

  _configuration.TelemetryInitializers.Add(new MyInitializer(...));

Upvotes: 2

Related Questions