APM Agent .NET does not work - Invalid certificate problem

Hello,

I have a problem using an Elastic APM Agent .NET in a Windows instance. I am using the Profiler Auto Instrumentation option and I have the .NET Framework 4.8 installed in my Windows instance. I have created all environment variables in the same way that I did for another Windows instance with .NET Core that I have and it is working fine, but in my current instance with .NET Framework is failing (the documents are not being sent to Kibana). The environment variables created are the following:

COMPlus_LoaderOptimization="1"
COR_ENABLE_PROFILING="1"
COR_PROFILER="{FA65FE15-F085-4681-9B20-95E04F6C03CC}"
COR_PROFILER_PATH="C:\Program Files (x86)\APM Agent .NET\elastic_apm_profiler_1.31.0-win-x64\elastic_apm_profiler.dll"
ELASTIC_APM_ENVIRONMENT="DEV"
ELASTIC_APM_GLOBAL_LABELS="xxxxxxxxxxxxxxxxx"
ELASTIC_APM_PROFILER_HOME="C:\Program Files (x86)\APM Agent .NET\elastic_apm_profiler_1.31.0-win-x64"
ELASTIC_APM_PROFILER_INTEGRATIONS="C:\Program Files (x86)\APM Agent .NET\elastic_apm_profiler_1.31.0-win-x64\integrations.yml"
ELASTIC_APM_SECRET_TOKEN="xxxxxxxxxxxx"
ELASTIC_APM_SERVER_URL="xxxxxxxxxxxxxxxx"
ELASTIC_APM_SERVICE_NAME="xxxxxxxxxx"
ELASTIC_APM_TRANSACTION_SAMPLE_RATE="0.1"
ELASTIC_APM_VERIFY_SERVER_CERT="false"
ELASTIC_HTTPS_PROTOCOLS="TLSv1.1,TLSv1.2"
ELASTIC_OTEL_LOG_LEVEL="trace"
ELASTIC_OTEL_LOG_TARGETS="file"
OTEL_DOTNET_AUTO_LOG_DIRECTORY="C:\Program Files (x86)\APM Agent .NET\elastic_apm_profiler_1.31.0-win-x64\logs"
OTEL_LOG_LEVEL="trace"

The error that I receive is the following (ok, it is really a warning, but I think this is the root cause that is causing the documents won't be sent to Kibana):

[2024-12-11 09:50:00.568 +00:00][24][Warning] - {PayloadSenderV2} Failed sending events. Following events were not transferred successfully to the server (https://xxxxxxxxxxxxxxxxxx/):
Elastic.Apm.Metrics.MetricSet,
Elastic.Apm.Metrics.MetricSet,
Elastic.Apm.Metrics.MetricSet,
Elastic.Apm.Metrics.MetricSet,
Elastic.Apm.Metrics.MetricSet +-> Exception: System.Net.Http.HttpRequestException: An error occurred while sending the request.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Elastic.Apm.Report.PayloadSenderV2.ProcessQueueItems(Object queueItems)
+-> Inner Exception: System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
+-> Inner Exception: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)

I have created the following environment variable in Windows in the same way than the others:

ELASTIC_APM_VERIFY_SERVER_CERT="false"

As far I understand, if I have this environment variable created, the certificate should not be verified and this kind of errors should be omitted (I have a .NET Framework higher than 4.7.2 so the variable should work).

Can someone help me to solve this problem? I have analyzed the rest of the agent logs and I can't see more errors or exceptions than this so I think those exceptions are the root cause of my problem.

Many thanks in advance to all!!!

This is probably a bug [BUG] Profiler auto-instrumentation on .NET Framework 4.7.2+ doesn't honour the `VerifyServerCert` configuration · Issue #2530 · elastic/apm-agent-dotnet · GitHub

Many thanks Guillaume. Let's see if this bug can be fixed soon and our problem is solved after apply it.