# Capturing coverage from the app process

There are **two ways to capture coverage from the app/service processes** using the SeaLights .NET Core agent

1. By running the process with our agent using the **run option** (command wrapper).
2. By running the SeaLights .NET Core agent as a **background listener**

The Background Listener option allows you to capture coverage from any application - without changing the command used to run it, but this requires a few more steps than the `run`option.

## Running the process with the SeaLights .NET Core agent using `run` option <a href="#running-the-process-with-the-sealights-.net-core-agent-using-run-option" id="running-the-process-with-the-sealights-.net-core-agent-using-run-option"></a>

To capture coverage from a single application, you can run it with the SeaLights .NET agent, which will capture coverage from it when it runs, and tests are run against it.

{% code overflow="wrap" lineNumbers="true" %}

```powershell
./sl-dotnet-agent/SL.DotNet.exe run --buildSessionIdFile buildSessionId.txt --instrumentationMode coverage --target c:\MyApplication\App.exe --workingDir c:\WorkingDir --targetArgs "-Flag1 true MyTest.dll"
```

{% endcode %}

{% hint style="info" %}
The sample command above is replacing the regular command used to startup the application captured: `c:\MyApplication\App.exe -Flag1 true MyTest.dll` executed from the `c:\WorkingDir` directory
{% endhint %}

{% hint style="success" %}
At the successful completion of this step, you will see new entries appearing in the *Cockpit > Live Agents Monitor:* the `testListener` and a entity called `Profiler` with the version details of your application.
{% endhint %}

{% hint style="info" %}
See [#starting-background-test-listener](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/command-reference#starting-background-test-listener "mention") for full parameters details.&#x20;
{% endhint %}

## Running the SeaLights .NET Core agent as a background listener <a href="#running-the-sealights-.net-core-agent-as-a-background-listener" id="running-the-sealights-.net-core-agent-as-a-background-listener"></a>

To capture coverage from any application without changing the command you use to run it, you can run the SeaLights .NET agent as a background process, which will capture coverage from any process with the SeaLights .NET agent defined as a profiler.

### Starting the SeaLights .NET Core agent background listener <a href="#starting-the-sealights-.net-core-agent-background-listener" id="starting-the-sealights-.net-core-agent-background-listener"></a>

To run the background process, you need to use the **startBackgroundTestListener** flag and provide a unique session key:

{% code overflow="wrap" lineNumbers="true" %}

```powershell
dotnet ./sl-dotnet-agent/SL.DotNet.dll startBackgroundTestListener --buildSessionIdFile buildSessionId.txt --testListenerSessionKey SL_CovCollectorAgent
```

{% endcode %}

### Setting the SeaLights .NET Core agent as a profiler <a href="#setting-the-sealights-.net-core-agent-as-a-profiler" id="setting-the-sealights-.net-core-agent-as-a-profiler"></a>

Now, you need to run (all) your processes - i.e. including your application - with the SeaLights .NET Core agent defined as a profiler. This is done by setting the following environment variables in the shell used to run your process:

{% tabs %}
{% tab title="Powershell" %}
{% code overflow="wrap" lineNumbers="true" %}

```powershell
[Environment]::SetEnvironmentVariable("CORECLR_ENABLE_PROFILING", "1", "Machine")
[Environment]::SetEnvironmentVariable("CORECLR_PROFILER", "{01CA2C22-DC03-4FF5-8350-59E32A3536BA}", "Machine")
[Environment]::SetEnvironmentVariable("CORECLR_PROFILER_PATH_32", "C:\Sealights\agent\SL.DotNet.ProfilerLib.Windows_x86.dll", "Machine")
[Environment]::SetEnvironmentVariable("CORECLR_PROFILER_PATH_64", "C:\Sealights\agent\SL.DotNet.ProfilerLib.Windows_x64.dll", "Machine")
[Environment]::SetEnvironmentVariable("SL_CollectorId", "SL_CovCollectorAgent", "Machine")
[Environment]::SetEnvironmentVariable("SL_LogDir", "C:\Sealights\logs", "Machine")
[Environment]::SetEnvironmentVariable("SL_LogLevel", "6", "Machine")
```

{% endcode %}
{% endtab %}

{% tab title="Linux" %}
{% code overflow="wrap" lineNumbers="true" %}

```sh
CORECLR_ENABLE_PROFILING=1
CORECLR_PROFILER={3B1DAA64-89D4-4999-ABF4-6A979B650B7D}
CORECLR_PROFILER_PATH_32=/usr/local/lib/libSL.DotNet.ProfilerLib.Linux.so
CORECLR_PROFILER_PATH_64=/usr/local/lib/libSL.DotNet.ProfilerLib.Linux.so
SL_CollectorId=SL_CovCollectorAgent
SL_LogDir=/tmp/logs
SL_LogLevel=6
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}

* Ensure the variables specifying the profiler path are correct, given where you installed the agent binaries (for example: `CORECLR_PROFILER_PATH_32=/usr/local/lib/libSL.DotNet.ProfilerLib.Linux.so`)
* You should **unset** the environment variables before running any process that you **do not want** to capture coverage from
* On Windows PowerShell, the `machine` **and** `user` **scopes both persist outside of the current process,** allowing you to save a new or changed environment variable.
  {% endhint %}

{% hint style="success" %}
At the successful completion of this step, after starting your application with its regular command, you will see new entries appearing in the *Cockpit > Live Agents Monitor:* the `testListener` and a entity called `Profiler` with the version details of your application.
{% endhint %}

### Stopping the SeaLights .NET Core agent background listener <a href="#stopping-the-sealights-.net-core-agent-background-listener" id="stopping-the-sealights-.net-core-agent-background-listener"></a>

Once you have finished collecting coverage from the process, you need to stop the background listener. This is done by using the **stopBackgroundTestListener** flag with the session key previously used

{% code overflow="wrap" lineNumbers="true" %}

```powershell
dotnet ./sl-dotnet-agent/SL.DotNet.dll stopBackgroundTestListener --buildSessionIdFile buildSessionId.txt --testListenerSessionKey SL_CovCollectorAgent
```

{% endcode %}

{% hint style="info" %}
See [command-reference](https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/.net-core-agent/command-reference "mention") for full parameter details of all the commands listed above.
{% endhint %}
