Setting .NET Core agent environment variables per process

When installing the .NET Core agent, most of the required environment variables are set by our installer, and if your .NET Core application is hosted in IIS, your application should automatically report data to New Relic after an iisreset since these environment variables are added directly to the Windows registry.

As noted in our documentation, for non-IIS .NET Core applications, and .NET Core applications running in Linux, we do not set the environment variable CORECLR_ENABLE_PROFILING=1 as this would cause all .NET Core processes on the system to be instrumented, including processes other than your applications. While the .NET Framework agent avoids this by requiring the process name to be targeted for instrumentation, that functionality does not exist with the .NET Core agent.

In our documentation, we recommend setting the environment variables for each application you want to instrument. But what’s the best way to do that? Although there are many ways to do this in Windows and Linux, I thought I’d make some suggestions here.

Windows Services

In order to selectively monitor .NET Core Windows services, add CORECLR_ENABLE_PROFILING=1 to the service’s registry in Windows.

  1. In the Windows Registry Editor, create a multi-string value named Environment in HKLM\System\CurrentControlSet\Services\_NAME_OF_SERVICE_
  2. Add CORECLR_ENABLE_PROFILING=1

Or, you can edit and run the following Powershell command with the name of your service to do this:

  [String[]] $v = @("CORECLR_ENABLE_PROFILING=1")
  Set-ItemProperty HKLM:SYSTEM\CurrentControlSet\Services\_NAME_OF_SERVICE_ -Name Environment -Value $v

Other Windows non-IIS applications

For non-IIS applications that are not Windows services, you might set the CORECLR_ENABLE_PROFILING environment variable from a batch file before starting your application:

rem set environment variable for New Relic agent
SET CORECLR_ENABLE_PROFILING=1

rem start application
_process_name_.exe

Applications in Linux

If you are managing your Linux services with systemd or supervisord you can add the required environment variables to the relevant process sections of those daemons’ configuration files as described in their documentation.

If you’re not using the above management tools, you might simply set the environment variable on the command line prior to launching the process using the export command.

You can confirm the presence of the required environment variables in your processes by running:

ps aux | grep [process name]  # to get the process ID
cat /proc/[process ID]/environ | tr '\0' '\n'

You could try starting up your applications via the $CORECLR_NEWRELIC_HOME/run.sh dotnet MYAPP.dll command which is included with the agent installation.

For Linux, our other recommended ways of enabling the agent are described here.


I hope this helps! If you have any other tips for accomplishing this, feel free to reply to this post!

1 Like