> ## Documentation Index
> Fetch the complete documentation index at: https://mw-docs.middleware.io/llms.txt
> Use this file to discover all available pages before exploring further.

# .NET

> .NET APM - Setup & Installation Docs | Middleware

<span
  style={{
background: "url(https://img.shields.io/github/v/release/middleware-labs/dotnet-plugin) no-repeat",
width: "125px",
height: "20px",
display: "inline-block",
}}
/>

| Traces | Metrics | App Logs | Custom Logs | Profiling |
| :----: | :-----: | :------: | :---------: | :-------: |
|    ✅   |    ✅    |    ✖️    |      ✅      |     ✅     |

This guide walks you through setting up Application Performance Monitoring (APM) on a .NET application. These instructions can also be found on the Installation page in your Middleware Account. View example code <a href="https://github.com/middleware-labs/demo-apm/tree/master/dotnet"> here. </a>

# Prerequisites

<Steps>
  <Step title="Infra Agent">
    Infrastructure Agent (Infra Agent). To install the Infra Agent, see our <a href="https://docs.middleware.io/docs/agent-installation/overview"> installation guide</a>.
  </Step>

  <Step title=".NET Version">
    `.NET version 6+`. Check your .NET version with the following command.

    ```bash Shell theme={null}
    dotnet --version
    ```
  </Step>
</Steps>

# Install

### Step 1: Install `.NET` APM Package

#### Step 1a: Run the following command in your terminal

```bash Shell theme={null}
curl -sSfL https://install.middleware.io/apm/dotnet/v1.0.0-rc.1/scripts/mw-dotnet-auto-install.sh -O
```

#### Step 1b: Install core files

```bash Shell theme={null}
sh ./mw-dotnet-auto-install.sh
rm -f mw-dotnet-auto-install.sh
```

#### Step 1c: Enable execution for the instrumentation script

```bash Shell theme={null}
chmod +x $HOME/.mw-dotnet-auto/instrument.sh
```

### Step 2: Setup Middleware .NET Project

Download the latest [Middleware.dll](https://github.com/middleware-labs/dotnet-plugin/releases) and add the following code in the `.csproj` file.

```xml XML theme={null}
<ItemGroup>
  <Reference Include="Middleware">
    <HintPath>path\to\Middleware.dll</HintPath>
  </Reference>
</ItemGroup>
```

### Step 3: Container Variables

<Note>Applications running in a container require an additional environment variable. If your application is not running in a container, move to [Step 4](/apm-configuration/dotnet#step-4-build-and-run-net-application). </Note>

#### Kubernetes

Add the following environment variable to your container:

```Shell Shell theme={null}
MW_AGENT_SERVICE=mw-service.mw-agent-ns.svc.cluster.local
```

<Note> The default namespace for running the Middleware agent is `mw-service.mw-agent-ns.svc.cluster.local`. </Note>

#### Docker

Add the following environment variables to your container:

```dockerfile dockerfile theme={null}
MW_AGENT_SERVICE=DOCKER_BRIDGE_GATEWAY_ADDRESS
MW_API_KEY=<MW_KEY>
```

<Note> The `DOCKER_BRIDGE_GATEWAY_ADDRESS` is the IP address of the gateway between the Docker host and bridge network. This is `172.17.0.1` by default.
Learn more about Docker bridge networking [here](https://docs.docker.com/network/network-tutorial-standalone/) </Note>

Specify your HTTP ports and URL with the following:

<Warning> This only applies to .NET projects using the ASP framework. </Warning>

```dockerfile dockerfile theme={null}
ASPNETCORE_HTTP_PORTS=5000
ASPNETCORE_URLS=http://0.0.0.0:5000
```

### Step 4: Enable Custom Logs \[Optional]

#### Step 4a: Add the following functions:

```csharp C# theme={null}
using Middleware.Logger;
...
Logger.Info("This is info log");
Logger.Warning("This is info log");
Logger.Debug("This is info log");
Logger.Error(<Object of Exception>);
```

#### Step 4b: Add packages to `YourApp.csproj` file

```xml XML theme={null}
<Project Sdk="Microsoft.NET.Sdk.Web">    
    ...
    <ItemGroup>
      <PackageReference Include="OpenTelemetry" Version="1.5.1" />
      <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.5.1" />
    </ItemGroup>
    ...
</Project>
```

#### Step 4c: Enable Logs in your `project.cs` file

```csharp C# theme={null}
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
...

var builder = WebApplication.CreateBuilder(args);
 
builder.Logging.AddOpenTelemetry(options => 
{
    options.AddConsoleExporter();
});
...

var app = builder.Build();
...

app.Run();
```

### Step 5: Build and Run .NET Application

Execute the following command to build and run your .NET application:

```bash Shell theme={null}
MW_API_KEY={MW_API_KEY} \
  . $HOME/.mw-dotnet-auto/instrument.sh && \
  OTEL_SERVICE_NAME="{APM-SERVICE-NAME}" \
  dotnet path/to/YourApplication.dll
```

# Continuous Profiling

Continuous profiling captures real-time performance insights from your application to enable rapid identification of resource allocation, bottlenecks, and more. Navigate to the [Continuous Profiling](https://docs.middleware.io/workflow/profiling#continuous-profiling) section to learn more about using Continuous Profiling with the .NET APM.

<Note> Need assistance or want to learn more about Middleware? Contact us at support\[at]middleware.io. </Note>
