Configuration on ASP.NET Core

edit

The UseElasticApm() extension method offers an overload to pass an IConfiguration instance to the APM Agent. To use this type of setup, which is typical in an ASP.NET Core application, your application’s Startup.cs file should contain code similar to the following:

using Elastic.Apm.AspNetCore;

public class Startup
{
    private readonly IConfiguration _configuration;

    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //Registers the agent with an IConfiguration instance:
        app.UseElasticApm(_configuration);

        //Rest of the Configure() method...
    }
}

With this setup, the Agent is able to be configured in the same way as any other library in your application. For example, any configuration source that has been configured on the IConfiguration instance being passed to the APM Agent can be used to set Agent configuration values.

More information is available in the official Microsoft .NET Core configuration docs You can find the key for each APM configuration option in this documentation, under the IConfiguration or Web.config key column of the option’s description.

It is also possible to call UseElasticApm() without the overload. In this case, the agent will only read configurations from environment variables.

The UseElasticApm method only turns on ASP.NET Core monitoring. To turn on tracing for everything supported by the Agent on .NET Core, including HTTP and database monitoring, use the UseAllElasticApm method from the Elastic.Apm NetCoreAll package. Learn more in ASP.NET Core setup.

Sample configuration file

edit

Here is a sample appsettings.json configuration file for a typical ASP.NET Core application that has been activated with UseElasticApm(). There are two important takeaways, which are listed as callouts below the example:

{
  "Logging": {
    "LogLevel": { 
      "Default": "Warning",
      "Elastic.Apm": "Debug"
    }
  },
  "AllowedHosts": "*",
  "ElasticApm": 
    {
      "ServerUrl":  "http://myapmserver:8200",
      "SecretToken":  "apm-server-secret-token",
      "TransactionSampleRate": 1.0
    }
}

With ASP.NET Core, you must set LogLevel for the internal APM logger in the standard Logging section with the Elastic.Apm category name.

The configurations below ElasticApm are fetched by the agent if the corresponding IConfiguration is passed to the agent.

In certain scenarios—​like when you’re not using ASP.NET Core—​you won’t activate the agent with the UseElasticApm() method. In this case, set the agent log level with ElasticApm:LogLevel, as shown in the following appsettings.json file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ElasticApm":
    {
      "LogLevel":  "Debug",
      "ServerUrl":  "http://myapmserver:8200",
      "SecretToken":  "apm-server-secret-token",
      "TransactionSampleRate": 1.0
    }
}