# Setting up a windows service

## Coverage Collector Service Installation <a href="#coverage-collector-service-installation" id="coverage-collector-service-installation"></a>

As an Administrator, install and start the coverage collector service:

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

```
SL.DotNet.CoverageCollectorService.exe install --start
```

{% endcode %}

Register our agent as a profiler in the services registry settings:

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

```
SL.DotNet.exe instrumentService --serviceName {YourService} --force
```

{% endcode %}

Restart your service manually via the Service Manager console or the following PowerShell command

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

```
Restart-Service -Name {YourService} -Force
```

{% endcode %}

{% hint style="success" %}
At the successful completion of this step, you will see a new entry appearing in the *Cockpit > Live Agents Monitor* for an entity called `CollectorService` for `dotnet` technology
{% endhint %}

## Capturing coverage <a href="#capturing-coverage" id="capturing-coverage"></a>

During the time you want to run your tests and capture coverage, you can now start and stop the coverage collection of the SeaLights agent test listener

### Starting coverage collection session <a href="#starting-coverage-collection-session" id="starting-coverage-collection-session"></a>

Before you start running your tests, you need to update the SeaLights test listener to start collecting coverage. You do so with the `startCollectorServiceSession` parameter.

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

```powershell
SL.DotNet.exe startCollectorServiceSession --buildSessionIdFile buildSessionId.txt --labId my_labId --processName YourProcess.exe --includeChildProcesses true
```

{% endcode %}

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

* Note that this can be run on a remote machine (like the CI running the tests) while providing the host to start the session on using the parameter: `--machine <host>`
* If you’re running several services on the machine, you can use the above command with `--processName *` and every service having the Sealights' environment variables defined in its Registry entry will be metered

{% hint style="info" %}
See [Command Reference](/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/.net-core-agent/command-reference.md) for full parameter details
{% endhint %}

<details>

<summary>Troubleshooting misconfiguration</summary>

If the `Profiler` doesn't start correctly, you can validate your configuration using the following approaches:

1. Open the *Event Viewer* and search for possible errors under *Local> Windows Log > Application*

   <figure><img src="/files/f4v7db9erQfDn2pYHLT2" alt=""><figcaption></figcaption></figure>
2. Validate the correct variables were attached to the process of your service using a tool like [Process Explorer - Sysinternals](https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer).\
   You can refer to our dedicated article [Deep Troubleshooting using Process Explorer](/knowledgebase/setup-and-configuration/troubleshooting-faq/.net-.netcore/deep-troubleshooting-using-process-explorer.md) for detailed instructions on how to use it with Sealights.
3. You can enable advanced logs at the Profiler level according to the following instructions: [Collecting Profiler Logs](/knowledgebase/setup-and-configuration/troubleshooting-faq/.net-.netcore/collecting-profiler-logs.md#collecting-profiler-logs-from-windows-services)

</details>

### Running your tests <a href="#running-your-tests" id="running-your-tests"></a>

Now you can run any one of your tests that runs against this service and capture coverage.

{% hint style="info" %}
See [Running tests](/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/.net-core-agent/running-tests.md) for details of how to run tests
{% endhint %}

### Stopping coverage collection session <a href="#stopping-coverage-collection-session" id="stopping-coverage-collection-session"></a>

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

```
SL.DotNet.exe stopCollectorServiceSession --buildSessionIdFile buildSessionId.txt --processName YourProcess.exe
```

{% endcode %}

## Uninstalling Profiler from your Service <a href="#uninstalling-profiler-from-your-service" id="uninstalling-profiler-from-your-service"></a>

If needed, you can un-register the SeaLights profiler from your Service

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

```
SL.DotNet.exe instrumentService --serviceName {YourService} --force --undo
```

{% endcode %}

## Sample script <a href="#sample-script" id="sample-script"></a>

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

```powershell
# ===== SECTION TO UPDATE ====
$SL_TOKEN="123456789" #Prefer an Env variable from your Credentials Manager
# Env specific 
$SL_BSID_FILE="$SEALIGHTS_PATH\buildSessionId.txt"
$SL_ENV_LABID="TestLabEnv@Automation"
#App specific
$SL_ServiceName="YourService" 
$SL_ProcessName="YourService.exe" 
# ============================

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$previousProgressPref=$global:ProgressPreference && $global:ProgressPreference = "SilentlyContinue" #Disable Progress bar topowershell  speed up download and expand

### Install Agent and Token ###
iwr -OutFile sealights-dotnet-agent.zip -UseBasicParsing -Uri https://agents.sealights.co/dotnetcore/latest/sealights-dotnet-agent-windows-self-contained.zip
Expand-Archive .\sealights-dotnet-agent.zip -DestinationPath sl-dotnet-agent -Force
Write-Output "[Sealights] .NetCore Agent version is: $(Get-Content .\sl-dotnet-agent\version.txt)"
$global:ProgressPreference = $previousProgressPref #Restore progress bar
Out-File -Input-Object $SL_TOKEN -NoNewline -Force ".\sl-dotnet-agent\sltoken.txt"

### Install SL Collector Service ###
Invoke-Expression .\sl-dotnet-agent\SL.DotNet.CoverageCollectorService.exe install start
Invoke-Expression .\sl-dotnet-agent\SL.DotNet.exe instrumentService --serviceName $SL_ServiceName --force
Restart-Service -Name $SL_ServiceName -Force

### Stop all session for specific Service version if they exist ###
SL.DotNet.exe stopCollectorServiceSession --buildSessionIdFile $SL_BSID_FILE --processName $SL_ProcessName

### Starting session for specific Service version ###
Invoke-Expression .\sl-dotnet-agent\SL.DotNet.exe startCollectorServiceSession --buildSessionIdFile $SL_BSID_FILE --processName $SL_ProcessName --labId $SL_ENV_LABID `
                    --includeChildProcesses true --logEnabled true --logAppendConsole true  

#### You can start your tests ####
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sealights.io/knowledgebase/setup-and-configuration/sealights-agents-and-plugins/.net-core-agent/capturing-coverage-from-the-application/ms-windows-specific-configurations/setting-up-a-windows-service.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
