.NET

TracesMetricsApp LogsCustom LogsProfiling

1. Prerequisites

  • Middleware Host Agent (for host-based mode). If your app is containerized, point it to the Host Agent using MW_AGENT_SERVICE.
    • Docker default bridge gateway: 172.17.0.1.
    • Kubernetes service DNS: mw-service.mw-agent-ns.svc.cluster.local.
  • .NET 6.0+ Check with:
    dotnet --version
    Scala APM

2. Host-based instrumentation (with Host Agent)

1 Add the Middleware reference (Logger & helpers)

Download the latest Middleware.dll and reference it in your project:

<!-- .csproj -->
<ItemGroup>
  <Reference Include="Middleware">
    <HintPath>path\to\Middleware.dll</HintPath>
  </Reference>
</ItemGroup>

The dotnet-plugin release notes confirm .NET 6+ compatibility and the Middleware.Logger.Logger API for custom logs.

2 Install the auto-instrumentation binaries

Linux
Windows (PowerShell)
Docker
Kubernetes
  • Download installer
    curl -sSfL https://install.middleware.io/apm/dotnet/v1.0.0-rc.1/scripts/mw-dotnet-auto-install.sh -O
  • Install core files (adds ~/.mw-dotnet-auto/):
    sh ./mw-dotnet-auto-install.sh
    rm -f mw-dotnet-auto-install.sh
  • Allow the instrumentation script:
    chmod +x $HOME/.mw-dotnet-auto/instrument.sh
  • Run your app with instrumentation:
MW_API_KEY="<MW_API_KEY>" \
. $HOME/.mw-dotnet-auto/instrument.sh && \
OTEL_SERVICE_NAME="{APM-SERVICE-NAME}" \
dotnet path/to/YourApplication.dll
  • Download installer
    $install = Join-Path $env:temp "mw-dotnet-auto-install-setup.ps1"
    Invoke-WebRequest -Uri "https://install.middleware.io/apm/dotnet/v1.0.0-rc.1/scripts/mw-dotnet-auto-install-setup.ps1" -OutFile $install -UseBasicParsing
  • Execute:
    . $install
  • Register service name in session:
    Register-OpenTelemetryForCurrentSession -OTelServiceName "{APM-SERVICE-NAME}"
  • Run your app
    .\YourNetApp.exe
  • Install deps and Middleware auto-installer:
    RUN apt-get update && apt-get install -y curl unzip
    RUN curl -sSfL https://install.middleware.io/apm/dotnet/v1.0.0-rc.1/scripts/mw-dotnet-auto-install.sh -O 
    RUN bash mw-dotnet-auto-install.sh 
    RUN rm -rf mw-dotnet-auto-install.sh
    RUN chmod +x $HOME/.mw-dotnet-auto/instrument.sh
    
    # ... your app build/publish here ...
  • Start with instrumentation:
    CMD . $HOME/.mw-dotnet-auto/instrument.sh && dotnet Mw-WebApplication.dll
  • If using ASP.NET, expose ports/URLs explicitly:
    ENV ASPNETCORE_HTTP_PORTS=5000
    ENV ASPNETCORE_URLS=http://0.0.0.0:5000
  • Add connectivity/env:
    ENV MW_AGENT_SERVICE=172.17.0.1
    ENV MW_API_KEY=<MW_API_KEY>
  • Install deps and Middleware auto-installer:
    RUN apt-get update && apt-get install -y curl unzip
    RUN curl -sSfL https://install.middleware.io/apm/dotnet/v1.0.0-rc.1/scripts/mw-dotnet-auto-install.sh -O 
    RUN bash mw-dotnet-auto-install.sh 
    RUN rm -rf mw-dotnet-auto-install.sh
    RUN chmod +x $HOME/.mw-dotnet-auto/instrument.sh
    
    # ... your app build/publish here ...
  • Start with instrumentation:
    CMD . $HOME/.mw-dotnet-auto/instrument.sh && dotnet Mw-WebApplication.dll
  • If using ASP.NET, expose ports/URLs explicitly:
    ENV ASPNETCORE_HTTP_PORTS=5000
    ENV ASPNETCORE_URLS=http://0.0.0.0:5000
  • Add the following environment variables to your container:
MW_AGENT_SERVICE=mw-service.mw-agent-ns.svc.cluster.local
MW_API_KEY="your-initial-token"

The default namespace for running the Middleware agent is mw-service.mw-agent-ns.svc.cluster.local.

3. Sending custom data (optional)

Custom logs (Middleware logger)

using Middleware.Logger;

Logger.Info("This is info log");
Logger.Warning("This is info log");
Logger.Debug("This is info log");
Logger.Error(new Exception("oops"));

Application logs (OpenTelemetry console exporter)

Add packages to your .csproj:

<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>

Wire up logging:

using OpenTelemetry.Logs;
using OpenTelemetry.Resources;

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddOpenTelemetry(options =>
{
    options.AddConsoleExporter();
});

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

4. View your data

After deployment, give it 3–5 minutes and check APM → Traces, Logs, and Continuous Profiling in Middleware.

5. Serverless / Direct (no Host Agent)

Use the NuGet package and configure via appsettings.json.

1 Install package

dotnet add package MW.APM

(Available for .NET 6.0 / .NET Framework 4.8 as of v1.2.0.)

2 Program configuration

// Program.cs
var builder = WebApplication.CreateBuilder(args);

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

builder.Services.ConfigureMWInstrumentation(configuration);

// optional: additional logging sinks
builder.Logging.AddConfiguration(configuration.GetSection("Logging"));
builder.Logging.AddConsole();
builder.Logging.SetMinimumLevel(LogLevel.Debug);

// After var app = builder.Build();
Logger.Init(app.Services.GetRequiredService<ILoggerFactory>());

3 Account & routing (appsettings.json)

{
  "MW": {
    "ApiKey": "<MW_API_KEY>",
    "TargetURL": "https://<MW_UID>.middleware.io:443",
    "ServiceName": "<service-name>",
  }
}

6. Environment variables — quick reference

VariableWherePurposeExample
MW_AGENT_SERVICEHost (containers)Address/DNS of Host Agent172.17.0.1 (Docker), mw-service.mw-agent-ns.svc.cluster.local (K8s)
MW_API_KEYHost & DockerAuth token used by installer/runtimexxxx...
OTEL_SERVICE_NAMEHostAPM service name (auto-exporter uses this)orders-api
ASPNETCORE_HTTP_PORTS, ASPNETCORE_URLSDocker (ASP.NET)Bind HTTP ports / URLs inside container5000, http://0.0.0.0:5000

Serverless/direct uses appsettings.json keys under MW (ApiKey, TargetURL, ServiceName, etc.) rather than these env vars.

7. Troubleshooting

  • No data (Host mode):
    • Verify the Host Agent is running and reachable. In containers, set MW_AGENT_SERVICE to your bridge gateway or K8s service DNS.
    • Ensure you prepended the instrumentation script before dotnet ....
  • Windows session not instrumented:
    • Re-run Register-OpenTelemetryForCurrentSession -OTelServiceName "{APM-SERVICE-NAME}" before launching the app.
  • Docker ASP.NET not listening:
    • Set ASPNETCORE_HTTP_PORTS and ASPNETCORE_URLS as shown above and map the port in your runtime.
  • Prefer direct ingest / serverless:
    • Use the MW.APM package and appsettings.json config with TargetURL to bypass a local agent. (NuGet package & config shown above.
  • Need cluster-wide auto-instrumentation:
    • Middleware supports OTel Kubernetes auto-instrumentation (including .NET) if you’d rather manage it at the platform layer.

Need assistance or want to learn more about Middleware? Contact our support team at [email protected] or join our Slack channel.