.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:
    1dotnet --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:

1<!-- .csproj -->
2<ItemGroup>
3  <Reference Include="Middleware">
4    <HintPath>path\to\Middleware.dll</HintPath>
5  </Reference>
6</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#

  • Download installer
    1curl -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/):
    1sh ./mw-dotnet-auto-install.sh
    2rm -f mw-dotnet-auto-install.sh
  • Allow the instrumentation script:
    1chmod +x $HOME/.mw-dotnet-auto/instrument.sh
  • Run your app with instrumentation:
1MW_API_KEY="<MW_API_KEY>" \
2. $HOME/.mw-dotnet-auto/instrument.sh && \
3OTEL_SERVICE_NAME="{APM-SERVICE-NAME}" \
4dotnet path/to/YourApplication.dll
  • Install and run the Middleware Windows Host Agent so OTLP is available at http://localhost:9320.
  • From an elevated PowerShell session, download and run the IIS full-instrumentation script. It installs OpenTelemetry auto-instrumentation for your .NET applications on Windows:
1powershell -ExecutionPolicy Bypass -Command "Invoke-WebRequest -Uri 'https://install.middleware.io/apm/dotnet/iis-full-instrumentation.ps1' -OutFile 'iis-full-instrumentation.ps1'; .\iis-full-instrumentation.ps1 -OtlpEndpoint 'http://localhost:9320' -ApiKey '<MW_API_KEY>'"
  • Restart IIS or your .NET application so the instrumentation takes effect.
  • Install deps and Middleware auto-installer:
    1RUN apt-get update && apt-get install -y curl unzip
    2RUN curl -sSfL https://install.middleware.io/apm/dotnet/v1.0.0-rc.1/scripts/mw-dotnet-auto-install.sh -O 
    3RUN bash mw-dotnet-auto-install.sh 
    4RUN rm -rf mw-dotnet-auto-install.sh
    5RUN chmod +x $HOME/.mw-dotnet-auto/instrument.sh
    6
    7# ... your app build/publish here ...
  • Start with instrumentation:
    1CMD . $HOME/.mw-dotnet-auto/instrument.sh && dotnet Mw-WebApplication.dll
  • If using ASP.NET, expose ports/URLs explicitly:
    1ENV ASPNETCORE_HTTP_PORTS=5000
    2ENV ASPNETCORE_URLS=http://0.0.0.0:5000
  • Add connectivity/env:
    1ENV MW_AGENT_SERVICE=172.17.0.1
    2ENV MW_API_KEY=<MW_API_KEY>
  • Install deps and Middleware auto-installer:
    1RUN apt-get update && apt-get install -y curl unzip
    2RUN curl -sSfL https://install.middleware.io/apm/dotnet/v1.0.0-rc.1/scripts/mw-dotnet-auto-install.sh -O 
    3RUN bash mw-dotnet-auto-install.sh 
    4RUN rm -rf mw-dotnet-auto-install.sh
    5RUN chmod +x $HOME/.mw-dotnet-auto/instrument.sh
    6
    7# ... your app build/publish here ...
  • Start with instrumentation:
    1CMD . $HOME/.mw-dotnet-auto/instrument.sh && dotnet Mw-WebApplication.dll
  • If using ASP.NET, expose ports/URLs explicitly:
    1ENV ASPNETCORE_HTTP_PORTS=5000
    2ENV ASPNETCORE_URLS=http://0.0.0.0:5000
  • Add the following environment variables to your container:
1MW_AGENT_SERVICE=mw-service.mw-agent-ns.svc.cluster.local
2MW_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)#

1using Middleware.Logger;
2
3Logger.Info("This is info log");
4Logger.Warning("This is info log");
5Logger.Debug("This is info log");
6Logger.Error(new Exception("oops"));

Application logs (OpenTelemetry console exporter)#

Add packages to your .csproj:

1<Project Sdk="Microsoft.NET.Sdk.Web">
2  <ItemGroup>
3    <PackageReference Include="OpenTelemetry" Version="1.5.1" />
4    <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.5.1" />
5  </ItemGroup>
6</Project>

Wire up logging:

1using OpenTelemetry.Logs;
2using OpenTelemetry.Resources;
3
4var builder = WebApplication.CreateBuilder(args);
5
6builder.Logging.AddOpenTelemetry(options =>
7{
8    options.AddConsoleExporter();
9});
10
11var app = builder.Build();
12app.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#

1dotnet add package MW.APM

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

2 Program configuration#

1// Program.cs
2var builder = WebApplication.CreateBuilder(args);
3
4var configuration = new ConfigurationBuilder()
5    .SetBasePath(Directory.GetCurrentDirectory())
6    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
7    .AddEnvironmentVariables()
8    .Build();
9
10builder.Services.ConfigureMWInstrumentation(configuration);
11
12// optional: additional logging sinks
13builder.Logging.AddConfiguration(configuration.GetSection("Logging"));
14builder.Logging.AddConsole();
15builder.Logging.SetMinimumLevel(LogLevel.Debug);
16
17// After var app = builder.Build();
18Logger.Init(app.Services.GetRequiredService<ILoggerFactory>());

3 Account & routing (appsettings.json)#

1{
2  "MW": {
3    "ApiKey": "<MW_API_KEY>",
4    "TargetURL": "https://<MW_UID>.middleware.io:443",
5    "ServiceName": "<service-name>",
6  }
7}

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 apps not instrumented:
  • 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? Get in touch with us via our Contact Us or join our Slack channel.