Newsletter Archive
Browse through our collection of past newsletters. Each edition is packed with C# and .NET insights.
Page 1 of 21 (61 editions)
When a request takes too long, the useful clue might be in a log, a metric, or a trace that crosses several services. The Aspire Dashboard can put those signals in one place, even when the app is not running inside an Aspire AppHost.
Your app can send telemetry to the dashboard using OpenTelemetry Protocol, or OTLP.
Add the Packages
In your ASP.NET Core project, add the OpenTelemetry packages:
dotnet add package OpenTelemetry.Extensions.Hostingdotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocoldotnet add package OpenTelemetry.Instrumentation.AspNetCoredotnet add package OpenTelemetry.Instrumentation.HttpConfigure the App
Add this to Program.cs:
using OpenTelemetry.Logs;using OpenTelemetry.Metrics;using OpenTelemetry.Resources;using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
var endpoint = new Uri( builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"] ?? "http://localhost:4317");
builder.Logging.AddOpenTelemetry(logging =>{ logging.AddOtlpExporter(options => options.Endpoint = endpoint);});
builder.Services.AddOpenTelemetry() .ConfigureResource(resource => resource.AddService("sample-api")) .WithMetrics(metrics => { metrics.AddAspNetCoreInstrumentation(); metrics.AddOtlpExporter(options => options.Endpoint = endpoint); }) .WithTracing(tracing => { tracing.AddAspNetCoreInstrumentation(); tracing.AddHttpClientInstrumentation(); tracing.AddOtlpExporter(options => options.Endpoint = endpoint); });
var app = builder.Build();
app.MapGet("/", () => "The app is sending telemetry.");
app.Run();Point OTEL_EXPORTER_OTLP_ENDPOINT at the dashboard’s OTLP receiver. The receiver address depends on how you started the dashboard, so use the endpoint printed by your Aspire setup. The example’s localhost:4317 is a common local OTLP gRPC address, not a promise that every dashboard listens there.
What You’ll See
The ASP.NET Core instrumentation records incoming requests. The HTTP client instrumentation records outgoing requests. The logging provider forwards logs, while the metrics and tracing providers send their own signals.
You can add your own meters and activity sources once the built in signals are flowing.
Key Takeaway
The Aspire Dashboard is useful beyond Aspire orchestration. If your app can export OTLP, it can send its logs, metrics, and traces there.
Don't miss the next tip 💧
Get a .NET tip and curated links delivered to your inbox every week.
You are walking a sequence and need both the item and its position. The usual answer is a counter you increment by hand. It works, but now you have one more thing to keep in sync.
In .NET 9 and later, Enumerable.Index() gives you both values together.
Try It
Create a .NET 10 console app:
dotnet new console -n EnumerableIndexDemo -f net10.0cd EnumerableIndexDemoReplace Program.cs with this:
var names = new[] { "Ada", "Grace", "Katherine" };
foreach (var (index, name) in names.Index()){ Console.WriteLine($"{index}: {name}");}Run it:
dotnet runThe index starts at zero, just like an array index.
The Part That Matters
Index() returns each item with its zero based index. It does not turn the source into a list or give you random access. It just keeps the current position alongside each item as you enumerate.
That makes it handy for adding line numbers, reporting progress, or pairing a sequence with its position. If the index is part of the actual data, put it in the data instead.
Key Takeaway
Use Enumerable.Index() when you need an item’s position during a foreach. One less counter to babysit.
Don't miss the next tip 💧
Get a .NET tip and curated links delivered to your inbox every week.
You have a log statement inside a loop that runs thousands of times. The message is useful, but building it over and over can mean extra work and extra garbage for the collector.
The logging source generator can build that method for you at compile time.
Try It
Create a .NET 10 console app and add the console logger:
dotnet new console -n LoggerMessageDemo -f net10.0cd LoggerMessageDemodotnet add package Microsoft.Extensions.Logging.ConsoleReplace Program.cs with this:
using Microsoft.Extensions.Logging;
using var loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole());
ILogger logger = loggerFactory.CreateLogger("Worker");
for (var item = 1; item <= 3; item++){ Log.ProcessedItem(logger, item);}
public static partial class Log{ [LoggerMessage( EventId = 1, Level = LogLevel.Information, Message = "Processed item {ItemNumber}")] public static partial void ProcessedItem( ILogger logger, int itemNumber);}Run it:
dotnet runThe generator supplies the body of ProcessedItem during the build. You call it like an ordinary method, and the log message keeps its structured ItemNumber field.
What’s the Catch?
The containing type and method need to be partial. The message template is fixed at compile time, so use regular logging when the message itself needs to be dynamic.
This is most useful for hot paths or logs you call often. For a one off startup message, plain logger.LogInformation is perfectly fine.
Key Takeaway
[LoggerMessage] gives you compile time generated logging code without giving up structured logs. Start with the log statements that run a lot. No need to turn every log into a ceremony.
Don't miss the next tip 💧
Get a .NET tip and curated links delivered to your inbox every week.