.Net Core SDK Guide
This guide shows how to add OpenTelemetry to your .NET application and install the monoscope SDK. OpenTelemetry sends traces, metrics and logs. The SDK adds request and response details, error reports and outgoing request monitoring.
Prerequisites
You need a monoscope project and its API key. If you do not have them yet, follow Get started.
Set Up With Your Coding Agent
Your coding agent can add the instrumentation for you. Install the monoscope skills and the monoscope CLI:
# Claude Code
claude plugin marketplace add monoscope-tech/skills
claude plugin install monoscope-skills@monoscope-skills
# Cursor, Codex, Cline and other agents
npx skills add monoscope-tech/skills
# monoscope CLI
curl https://monoscopehq.com/install.sh | sh
monoscope auth login
Then ask your agent to instrument the project. In Claude Code, run:
/monoscope-skills:instrument Add OpenTelemetry for monoscope to this project
The skill uses the CLI to add the SDK and to make sure that events arrive. To do the setup by hand, follow the steps below. For help from a person, email us.
OpenTelemetry Setup
OpenTelemetry sends traces, metrics and logs to monoscope. This guide uses the OpenTelemetry .NET auto-instrumentation.
Install OpenTelemetry
Run the following command to install the OpenTelemetry auto instrumentation for .NET:
# Download the bash script
curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -O
# Install core files
sh ./otel-dotnet-auto-install.sh
# Enable execution for the instrumentation script
chmod +x $HOME/.otel-dotnet-auto/instrument.sh
# Setup the instrumentation for the current shell session
. $HOME/.otel-dotnet-auto/instrument.sh
On Windows, follow the PowerShell setup.
OpenTelemetry Configuration
Configure the instrumentation with these environment variables:
export OTEL_EXPORTER_OTLP_ENDPOINT="http://otelcol.monoscope.tech:4317" # Specifies the endpoint to send the traces to.
export OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES="APItoolkit.HTTPInstrumentation" # The ActivitySource of the monoscope SDK.
export OTEL_SERVICE_NAME="my-service" # Specifies the name of the service.
export OTEL_RESOURCE_ATTRIBUTES="x-api-key=YOUR_API_KEY" # Adds your API KEY to the resource.
export OTEL_EXPORTER_OTLP_PROTOCOL="grpc" # Specifies the protocol to use for the OpenTelemetry exporter.
Build and run your application. Logs, traces and metrics appear in monoscope.
monoscope SDK Configuration
The monoscope middleware adds request and response details to each request span. It also lets you report errors and monitor outgoing requests.
Installation
Run the command below to install the SDK:
dotnet add package ApiToolkit.Net
Configuration
Initialize the client in your application’s entry point (for example, Program.cs) and register the middleware:
using ApiToolkit.Net;
// Initialize the monoscope client
var config = new Config
{
ServiceVersion = "v2.0",
ServiceName = "MyService",
};
var client = APIToolkit.NewClient(config);
// END Initialize the monoscope client
// Register monoscope's middleware
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
});
// END Register monoscope's middleware
app.MapGet("/hello", async context =>
{
await context.Response.WriteAsync("Hello, world!");
});
app.Run();
Config accepts these optional fields:
| Option | Description |
|---|---|
Debug |
Set to true to enable debug mode. |
Tags |
A list of defined tags for your services (used for grouping and filtering data on the dashboard). |
ServiceVersion |
A defined string version of your application (used for further debugging on the dashboard). |
RedactHeaders |
A list of HTTP header keys to redact. authorization, cookie, set-cookie, proxy-authorization and x-api-key are always redacted. |
RedactRequestBody |
A list of JSONPaths from the request body to redact. |
RedactResponseBody |
A list of JSONPaths from the response body to redact. |
ServiceName |
Set the name of the service. |
CaptureRequestBody |
Set to true to capture the request body. |
CaptureResponseBody |
Set to true to capture the response body. |
Tip
- Add the monoscope middleware before
UseEndpointsand other middleware. - Replace
YOUR_API_KEYwith an API key from Settings > API Keys in monoscope.
Redacting Sensitive Data
<div class=”callout”> <i class=”fa-solid fa-circle-info”></i> <p><code>authorization</code>, <code>cookie</code>, <code>set-cookie</code>, <code>proxy-authorization</code> and <code>x-api-key</code> headers are always redacted, on requests and responses and on outgoing calls, whatever you configure. Header names match case-insensitively.</p> </div>
If you have fields that are sensitive and should not be sent to monoscope servers, you can mark those fields to be redacted (the fields will never leave your servers).
To mark a field for redacting via this SDK, you need to provide additional arguments to the config variable with paths to the fields that should be redacted. There are three arguments you can provide to configure what gets redacted, namely:
-
RedactHeaders: A list of HTTP header keys. -
RedactRequestBody: A list of JSONPaths from the request body. -
RedactResponseBody: A list of JSONPaths from the response body.
JSONPath is a query language used to select and extract data from JSON files. For example, given the following sample user data JSON object:
{
"user": {
"name": "John Martha",
"email": "[email protected]",
"addresses": [
{
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
},
{
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
}
],
"credit_card": {
"number": "4111111111111111",
"expiration": "12/28",
"cvv": "123"
}
}
}
Examples of valid JSONPath expressions would be:
| JSONPath | Description |
|---|---|
$.user.addresses[*].zip |
In this case, monoscope will replace the zip field in all the objects of the addresses list inside the user object with the string [CLIENT_REDACTED]. |
$.user.credit_card |
In this case, monoscope will replace the entire credit_card object inside the user object with the string [CLIENT_REDACTED]. |
Tip
To learn more about JSONPaths, please take a look at the official docs or use this JSONPath Evaluator to validate your JSONPath expressions.
You can also use our JSON Redaction Tool to preview what the final data sent from your API to monoscope will look like, after redacting any given JSON object.
Here’s an example of what the configuration would look like with redacted fields:
using ApiToolkit.Net;
var config = new Config
{
RedactHeaders = new List<string> { "content-type", "Authorization", "HOST" },
RedactRequestBody = new List<string> { "$.user.email", "$.user.addresses" },
RedactResponseBody = new List<string> { "$.users[*].email", "$.users[*].credit_card" }
};
var client = APIToolkit.NewClient(config);
// Register the middleware to use the initialized client
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
});
Note
- The
RedactHeadersconfig field expects a list of case-insensitive headers as strings. - The
RedactRequestBodyandRedactResponseBodyconfig fields expect a list of JSONPaths as strings. - The list of items to be redacted will be applied to all endpoint requests and responses on your server.
Error Reporting
monoscope automatically detects different unhandled errors, API issues, and anomalies but you can report and track specific errors at different parts of your application. This will help you associate more detail and context from your backend with any failing customer request.
To report an error by hand, call the static Client.ReportError() method with the context and the exception:
using ApiToolkit.Net;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var config = new Config
{
ServiceName = "Backend"
};
var client = APIToolkit.NewClient(config);
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
});
app.MapGet("/error-tracking", async context =>
{
try
{
// Attempt to open a non-existing file
using (var fileStream = System.IO.File.OpenRead("non_existing_file.txt"))
{
// File opened successfully, do something if needed.
}
await context.Response.WriteAsync($"Hello");
}
catch (Exception error)
{
// Report the error to monoscope
Client.ReportError(context, error);
await context.Response.WriteAsync("Error reported!");
}
});
Monitoring Outgoing Requests
Outgoing requests are external API calls you make from your API. By default, monoscope monitors all requests users make from your application and they will all appear in the Log Explorer page. To see only outgoing requests, open the Outgoing tab of the API Catalog. Each outgoing request links to the incoming request that triggered it.
To monitor outgoing HTTP requests, use the APIToolkitObservingHandler() handler. Here’s an example of the outgoing requests configuration with this SDK on a sample /monitor-requests endpoint that makes an asynchronous HttpClient GET request to a sample public endpoint URL.
using ApiToolkit.Net;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var config = new Config
{
ServiceName = "Backend"
};
var client = APIToolkit.NewClient(config);
app.Use(async (context, next) =>
{
var apiToolkit = new APIToolkit(next, client);
await apiToolkit.InvokeAsync(context);
});
app.MapGet("/monitor-requests", async (context) =>
{
var observingHandlerOptions = new ATOptions
{
PathWildCard = "/posts/{id}",
RedactHeaders = ["User-Agent"],
RedactRequestBody = ["$.user.password"],
RedactResponseBody = ["$.user.data.email"]
};
using var httpClient = new HttpClient(client.APIToolkitObservingHandler(context, observingHandlerOptions));
var response = await httpClient.GetAsync("https://jsonplaceholder.typicode.com/posts/1");
var body = await response.Content.ReadAsStringAsync();
await context.Response.WriteAsync(body);
});
client.APIToolkitObservingHandler takes the context and an optional ATOptions object with these fields:
| Option | Description |
|---|---|
PathWildCard |
The url_path string for URLs with path parameters. |
RedactHeaders |
A list of HTTP header keys to redact. authorization, cookie, set-cookie, proxy-authorization and x-api-key are always redacted. |
RedactRequestBody |
A list of JSONPaths from the request body to redact. |
RedactResponseBody |
A list of JSONPaths from the response body to redact. |
Non-HTTP Entry Points (Background Jobs, Workers, CLIs)
The ASP.NET Core middleware only covers HTTP requests. IHostedService / BackgroundService workers, Quartz / Hangfire jobs, channel consumers, and standalone console hosts are invisible until you wrap each handler in a span yourself. Always cover these alongside your HTTP routes.
Create a dedicated ActivitySource and wrap each work unit in StartActivity. With the auto-instrumentation, add the source name to OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES as a comma-separated list (for example, APItoolkit.HTTPInstrumentation,MyService.Worker). If you configure the OpenTelemetry SDK in code, register it with AddSource:
using System.Diagnostics;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace;
// Program.cs — register the source
var workerSource = new ActivitySource("MyService.Worker");
builder.Services.AddOpenTelemetry().WithTracing(b => b.AddSource("MyService.Worker"));
public class EmailWorker : BackgroundService
{
private static readonly ActivitySource Source = new("MyService.Worker");
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
var job = await _queue.DequeueAsync(stoppingToken);
using var activity = Source.StartActivity(
"email.send", ActivityKind.Consumer);
activity?.SetTag("messaging.system", "channel");
activity?.SetTag("messaging.operation", "process");
activity?.SetTag("messaging.destination.name", "emails");
activity?.SetTag("code.function", nameof(EmailWorker.ExecuteAsync));
try
{
await SendEmailAsync(job, stoppingToken);
activity?.SetStatus(ActivityStatusCode.Ok);
}
catch (Exception ex)
{
activity?.RecordException(ex);
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}
}
}
}
For one-shot console hosts (Host.CreateApplicationBuilder followed by a single RunAsync), the host’s shutdown lifecycle calls Dispose on the OTel SDK and flushes pending spans. If you exit via Environment.Exit, call tracerProvider.ForceFlush() and Dispose() first or you’ll lose the final spans.
Next Steps
- Send a few requests to your application. Then open Log Explorer in monoscope to see the requests, logs and traces.
- Find your way around the dashboard: issues, monitors, dashboards and the API catalog.
- Investigate from your terminal or your agent with the CLI, the skills and the MCP server.
Explore the .Net Core SDK