Star us on GitHub
Star
Menu

C# .NET ASP

Error Monitoring / Logging / Tracing in .NET 6.x / 8.x via the OpenTelemetry Protocol (OTLP).
1
Install the highlight-io .NET SDK.

Download the highlight SDK package from NuGet and save it to your project solution.

dotnet add Highlight.ASPCore
Copy
2
Set up your highlight.io browser SDK.

The installation differs from the normal frontend getting started guide in the configuration of the .NET trace propagation. The _traceParentContext value is set based on the server trace context so that client side tracing can carry the existing trace ID and session context. Update your Components/App.razor HTML template entrypoint based on the following:

@using OpenTelemetry.Trace <!DOCTYPE html> <html lang="en"> <head> <meta name="traceparent" content="@_traceParentContext"> <script src="https://unpkg.com/highlight.run"></script> <script> H.init('<YOUR_PROJECT_ID>', { serviceName: 'highlight-dot-net-frontend', tracingOrigins: true, enableOtelTracing: true, networkRecording: { enabled: true, recordHeadersAndBody: true, }, }); </script> @* your standard head contents here... *@ </head> <body> @* your standard body contents here... *@ <Routes/> <script src="_framework/blazor.web.js"></script> </body> </html> @code { private string? _traceParentContext; // set the `traceparent` meta tag to the current active span to propagate context to the client protected override void OnInitialized() { var currentTrace = Tracer.CurrentSpan; if (!currentTrace.IsRecording) { _traceParentContext = "00-00-00-00"; } var traceId = currentTrace.Context.TraceId; var spanId = currentTrace.Context.SpanId; _traceParentContext = $"00-{traceId.ToHexString()}-{spanId.ToHexString()}-01"; } }
Copy
3
.NET supports OpenTelemetry instrumentation out of the box.

The Highlight.ASPCore NuGet package sets up OpenTelemetry instrumentation and export for highlight, injecting configuration functions for your ASPCore app to simplify instrumentation.

4
Bootstrap Highlight with your ASP application object.

Update your Program.cs application entrypoint to initialize highlight.

using System.Diagnostics; using Serilog; var builder = WebApplication.CreateBuilder(args); // configure your web application // create a Serilog logger with Highlight export Log.Logger = new LoggerConfiguration() .Enrich.WithMachineName() .Enrich.WithHighlight() .Enrich.FromLogContext() .WriteTo.Async(async => async.HighlightOpenTelemetry(options => { options.ProjectId = "<YOUR_PROJECT_ID>"; options.ServiceName = "<YOUR_SERVICE_NAME>"; }) ) .CreateLogger(); // Initialize trace, error, metric, and log export builder.Services .AddHighlightInstrumentation(options => options.ProjectId = "<YOUR_PROJECT_ID>"); builder.Logging .AddHighlightInstrumentation(options => options.ProjectId = "<YOUR_PROJECT_ID>"); var app = builder.Build();
Copy
5
Verify your errors are being recorded.

Verify that the backend error handling works by triggering the code that reports the error to highlight and visiting the highlight errors portal.

6
Verify your backend logs are being recorded.

Visit the highlight logs portal and check that backend logs are coming in.

7
Verify your backend traces are being recorded.

Visit the highlight traces portal and check that backend traces are coming in.