@page "/metrics" @inject ILogger Logger @using System.Diagnostics Metrics

Metrics Demo

This page demonstrates various metrics that can be tracked and visualized in Grafana.

Counter Metrics

Counter: @counterValue

Timer Metrics

Simulate a slow operation and track its duration.

@if (lastDuration > 0) {

Last operation took: @lastDuration ms

}
Error Simulation

Generate different types of log entries for testing.

Custom Metrics

Page visits: @pageVisits

This counter increments each time you visit this page.
@code { private int counterValue = 0; private int pageVisits = 0; private bool isProcessing = false; private long lastDuration = 0; private static readonly ActivitySource ActivitySource = new("BlazorApp.Metrics"); protected override void OnInitialized() { pageVisits++; Logger.LogInformation("Metrics page visited. Total visits: {PageVisits}", pageVisits); } private void IncrementCounter() { counterValue++; Logger.LogInformation("Counter incremented to {CounterValue}", counterValue); } private async Task SimulateSlowOperation() { isProcessing = true; var sw = Stopwatch.StartNew(); using var activity = ActivitySource.StartActivity("SlowOperation"); activity?.SetTag("operation.type", "simulated"); Logger.LogInformation("Starting slow operation"); try { await Task.Delay(Random.Shared.Next(1000, 3000)); sw.Stop(); lastDuration = sw.ElapsedMilliseconds; Logger.LogInformation("Slow operation completed in {Duration}ms", lastDuration); activity?.SetTag("duration.ms", lastDuration); } finally { isProcessing = false; } } private void SimulateError() { var errorId = Guid.NewGuid(); Logger.LogError("Simulated error with ID: {ErrorId}", errorId); Logger.LogWarning("This is a test warning associated with error {ErrorId}", errorId); } }