Configuration on ASP.NET Coreedit

The UseElasticApm() extension method offers an overload to pass an IConfiguration instance to the APM Agent. By using this overload in a typical ASP.NET Core application, the Startup.cs file would contain code similar to the following:

public class Startup
{
    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...
    }
}

As explained in ASP.NET Core, the UseElasticApm method only turns on ASP.NET Core monitoring. If you want to turn on everything (including HTTP and Database monitoring) you can use the UseAllElasticApm method from the Elastic.Apm.NetCoreAll package, which has the same overloads and behavior, except it turns on tracing for everything that is supported by the agent on .NET Core including database and HTTP tracing.

With this you can use any configuration source that you configured on the IConfiguration instance that you passed to the APM Agent. You can find the key of each configuration option in the IConfiguration or Web.config key column of the corresponding option’s description.

By simply calling app.UseElasticApm() without the overload, the agent will read configurations only from environment variables.

Sample configuration fileedit

Below is a sample appsettings.json configuration file for a typical ASP.NET Core application. There are two important takeaways:

  1. The part below ElasticApm is fetched by the agent if the corresponding IConfiguration is passed to the agent.
  2. With ASP.NET Core, you must set LogLevel for the internal APM logger in the standard Logging section with the ElasticApm category name.
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning",
      "Elastic.Apm": "Debug"
    }
  },
  "AllowedHosts": "*",
  "ElasticApm":
    {
      "ServerUrls":  "http://myapmserver:8200",
      "TransactionSampleRate": 1.0
    }
}

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

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ElasticApm":
    {
      "LogLevel":  "Debug",
      "ServerUrls":  "http://myapmserver:8200",
      "TransactionSampleRate": 1.0
    }
}