# How to set APM Configuration outside appsettings?

**URL:** <https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [May 5, 2021, 10:10am UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167 "2021-05-05T10:10:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![KARAOGLU](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@KARAOGLU](https://discuss.elastic.co/u/KARAOGLU)\
**Post date:** [May 5, 2021, 10:10am UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167/1 "2021-05-05T10:10:56Z")

</div>

My application is running in test environment. I see that it throws apm logs, but it does not work in production environment. I think the reason is from the configuration in appsettings.json by default, briefly as follows:

```
var environmentName = Environment.GetEnvironmentVariable('ASPNETCORE_ENVIRONMENT');
                        var builder = new ConfigurationBuilder()
                        .SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
                        .AddJsonFile('appsettings.json', false)
                        .AddJsonFile($'appsettings.{environmentName}.json', true)
                        .AddEnvironmentVariables();

UseElasticApm(new HttpDiagnosticsSubscriber());

```

I have 2 different configuration files according to the environment information in which the configuration is active. However, APM seems to be searching for the appsettings.json file by default.

When I check the logs, I see errors like the following.

**Elastic.Apm[0] {CentralConfigFetcher} Exception was thrown while fetching configuration from APM Server and parsing it. ETag: `<null>'. URL: `[http://localhost:8200/config/v1/agents](http://localhost:8200/config/v1/agents)?**

**Elastic.Apm[0] {PayloadSenderV2} Failed sending events. Following events were not transferred successfully to the server ([http://localhost:8200/](http://localhost:8200/)):**

By the way, the application is a console application.

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [May 5, 2021, 10:53am UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167/2 "2021-05-05T10:53:54Z")

</div>

Hi @KARAOGLU

This log only means that the agent wasn't able to fetch configs from APM server - maybe central config is just disabled:

```auto
Elastic.Apm[0] {CentralConfigFetcher} Exception was thrown while fetching configuration from APM Server and parsing it. ETag: <null>'. URL:

```

So that's fine.

This second one (below) is a real problem and it means the agent wasn't able to send the data to the APM Server - further parts of the log should have more info:

```auto
Elastic.Apm[0] {PayloadSenderV2} Failed sending events. Following events were not transferred successfully to the server (http://localhost:8200/):

```

Now to the `appsettings.json` and `IConfiguration` part: I assume you use the `Elastic.Apm.Extensions.Hosting` package, right?

In that case the agent will just use the `IConfiguration` that you configure on the `IServiceProvider` - you have this `var builder = new ConfigurationBuilder()`, but then nothing happens with the `builder` you create - is that the full code?

You should have something like this:

```auto
private static IHostBuilder CreateHostBuilder(string[] args) =>
	Host.CreateDefaultBuilder(args)
		.ConfigureServices((context, services) => { services.AddHostedService<HostedService>();
		.ConfigureAppConfiguration((hostContext, configurationBuilder) =>
		{
			var environmentName =Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
			 configurationBuilder.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).Ful
				.AddJsonFile("appsettings.json", false)
				.AddJsonFile($"appsettings.{environmentName}.json", true)
				.AddEnvironmentVariables();
		})
		.UseElasticApm(new HttpDiagnosticsSubscriber());

```

With this the APM agent will pick up the `configurationBuilder` you configure here and it'll read from that as any other library does.

---

<div class="post-metadata">

**Author:** ![KARAOGLU](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@KARAOGLU](https://discuss.elastic.co/u/KARAOGLU)\
**Post date:** [May 5, 2021, 11:07am UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167/3 "2021-05-05T11:07:46Z")

</div>

Hi @GregKalapos Thank you for your quick reply.

Here is the working part of the application

using Elastic.Apm.DiagnosticSource;  
using Elastic.Apm.Extensions.Hosting;

```
public static IHostBuilder CreateHostBuilder(string[] args) =>

                Host.CreateDefaultBuilder(args)
                .UseSystemd()
                .ConfigureServices((hostContext, services) =>
                {
                    var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
                    var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
                    .AddJsonFile("appsettings.json", true)
                    .AddJsonFile($"appsettings.{environmentName}.json", true)
                    .AddEnvironmentVariables();

                    configuration = builder.Build();

                    hostContext.Configuration = configuration;

                    ConsoleHelper.WriteWarningCommandLine($"✓ EnvironmentName bilgisi:{ environmentName}");

                    services.Configure<RabbitMQSettings>(hostContext.Configuration.GetSection(nameof(RabbitMQSettings)));
                    services.Configure<SentimentServiceConfig>(hostContext.Configuration.GetSection(nameof(SentimentServiceConfig)));
                    services.Configure<TurkcellSentimentServiceConfig>(hostContext.Configuration.GetSection(nameof(TurkcellSentimentServiceConfig)));

                    var solrSettings = configuration.GetSection("SolrSettings").Get<SolrSettings>();
                    var serilogSettings = configuration.GetSection("SerilogSettings").Get<SerilogSettings>();

                    services.AddSniffactorRepository();
                    services.AddSniffactorBolts();
                    services.AddLogging(loggingBuilder => loggingBuilder.Services.AddSniffactorSerilog(serilogSettings));
                    services.AddSniffactorNhibernate(hostContext.Configuration.GetConnectionString("OracleConnection"));
                    services.AddSniffactorServiceConnector();

                    services.AddSniffactorSolr(solrSettings);
                    services.AddBusConfiguration();
                    services.AddSingleton(x => hostContext.Configuration);
                    services.AddMemoryCache();
                    services.AddOptions();

                    services.AddMassTransitHostedService();

                    services.AddSingleton<ITracer, Tracer>();

                }).UseElasticApm(new HttpDiagnosticsSubscriber());

```

Now the error has changed, it says that it sent successfully, but it is trying to send metrics to APM running in the test environment.

`Elastic.Apm[0] {PayloadSenderV2} Failed sending events. Following events were not transferred successfully to the server (http://xxx.xx.x.xx:8200/)`

So my application is now trying to send it to a real ip and not localhost: 8200, so I see that the config file is working, but it configures according to the information in the fixed appsettings.json file, not the environment information.

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [May 5, 2021, 11:42am UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167/4 "2021-05-05T11:42:18Z")

</div>

Ok, I see. The code itself looks ok to me, the agent should pick up the `ConfigurationBuilder` you configure in `ConfigureServices`.

You say

> but it configures according to the information in the fixed appsettings.json file, not the environment information.

What's exactly the goal here? Where do you want to read the values from? You configure the 2 `.json` files, plus the environment variable. Where do you expect the value coming from? And could you show me the content of those files or show me the environment variable? One thing to keep in mind here (and I admit this can be confusing) is that if you set environment variables in this case, then you'll need to follow the `IConfiguration` format - so e.g. for configuring the server url, you need to set the environment variable `ElasticApm:ServerUrl` in this case (and not `ELASTIC_APM_SERVER_URL`).

Another small nit: you have this at the end of the code snippet: `services.AddSingleton<ITracer, Tracer>();` - if that's the `Elastic.Apm.ITracerer` then you can remove it, the `.UseElasticApm` adds it for you.

---

<div class="post-metadata">

**Author:** ![KARAOGLU](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@KARAOGLU](https://discuss.elastic.co/u/KARAOGLU)\
**Post date:** [May 5, 2021, 11:53am UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167/5 "2021-05-05T11:53:50Z")

</div>

@GregKalapos

In short, this is what I want. Environment-based configuration instead of dealing with settings file

appsettings.json:

```
  "ElasticApm": {
    "ServerUrl": "http://xxx.xx.x.xx:8200",
    "ServiceName": "SnifferService",
    "CloudProvider": "none",
    "Environment": "test"
  },

```

appsettings.Production.json:

```
 "ElasticApm": {
    "ServerUrls": "http://yyy.yy.yy.yy:8200",
    "ServiceName": "SnifferService",
    "CloudProvider": "none",
    "Environment": "Production"
  }

```

I am installing my application in the production environment. The environment information is writing to the screen as "Production". All other external applications (Solr, MassTransit etc.) configure according to the information in appsettings.Production.json, but APM tries to go to the address in appsettings.json.

In the production environment, when I look at the APM logs, I see this.

**Elastic.Apm[0] {PayloadSenderV2} Failed sending events. Following events were not transferred successfully to the server ([http://xxx.xx.x.xx:8200/](http://xxx.xx.x.xx:8200/)):**

---

<div class="post-metadata">

**Author:** ![KARAOGLU](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@KARAOGLU](https://discuss.elastic.co/u/KARAOGLU)\
**Post date:** [May 5, 2021, 12:12pm UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167/6 "2021-05-05T12:12:16Z")

</div>

Hi @GregKalapos I solved it 🙂

Looking carefully, I saw that the "ServerUrls" parameter is incorrect, my fault 😔

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [May 5, 2021, 1:01pm UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167/7 "2021-05-05T13:01:00Z")

</div>

Great, happy to hear that!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 26, 2021, 9:01am UTC](https://discuss.elastic.co/t/how-to-set-apm-configuration-outside-appsettings/272167/8 "2021-05-26T09:01:02Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
