Thomas Ardal

Thomas Ardal

Meet Thomas, the founder and developer behind elmah.io. Thomas blogs about everything technical from C# and ASP.NET Core to machine learning.

Denmark
320 posts
Testing logging code with Microsoft.Extensions.Logging and FakeLogger

Shows how to unit test logging calls in a Math.Divide method using the FakeLogger<T> class from the Microsoft.Extensions.Diagnostics.Testing NuGet package, asserting on logger.Collector.LatestRecord.Message and Collector.Count with NUnit rather than only relying on NullLogger. ...

"Secret" elmah.io features #1 - Include source code in errors

Explains how to install the Elmah.Io.Client.Extensions.SourceCode NuGet package, set EmbedAllSources true in the .csproj, and call msg.WithSourceCodeFromPdb() inside the AddElmahIo OnMessage callback so elmah.io shows the exact source line from a stack trace without cloning the repo. ...

Validate NuGet packages before publishing from GitHub Actions

Uses a joke NuGet package called RickrollingMiddleware to show how dotnet-validate (installed via dotnet tool install) catches failing Source Link, Deterministic, and Compiler Flags checks in GitHub Actions, then fixes them by adding DebugType embedded and PublishRepositoryUrl to the .csproj. ...

Dependency Injection using keyed services is finally in ASP.NET

Demonstrates ASP.NET Core 8's AddKeyedSingleton and FromKeyedServices attribute by injecting two named ServiceBusSender clients, taskTopic and preprocessorTopic, from Azure.Messaging.ServiceBus into a controller, replacing a hand-written Topics wrapper class used to work around the ambiguity. ...

Validate JSON files against schema in Azure DevOps build

Provides copy-paste PowerShell code using the Test-Json cmdlet against a schema.json file to validate JSON syntax and structure in an Azure DevOps build, covering both the classic-editor PowerShell Script task and a YAML PowerShell@2 pipeline step, plus the pwsh: true fix needed on windows-latest. ...

Setting up better logging in Azure Functions

Shows how to replace host.json's Application-Insights-only logging in in-process Azure Functions by installing Microsoft.Azure.Functions.Extensions, adding a FunctionsStartup-based Startup.cs, and calling AddLogging with Microsoft.Extensions.Logging.Console for full ILoggingBuilder control. ...

Conditionally add middleware in ASP.NET Core

Extends a TimingMiddleware class that sets an X-Took response header, comparing an in-class StartsWithSegments("/api") if-check, which still runs the middleware for every request, against wrapping app.UseWhen in Program.cs so the pipeline branch only executes for matching paths. ...

How to force reload cached JSON Schemas in Visual Studio

Fixes stale IntelliSense from Visual Studio's undocumented local JSON Schema cache: since the cache folder location isn't published, the workaround is Tools > Options > Text Editor > JSON > Schema, removing and re-adding the schemastore.org URL entry to force a refetch after reopening the file. ...

How to modify response headers in ASP.NET Core middleware

Walks through the System.InvalidOperationException thrown when a TimingMiddleware class tries to set an X-Took response header after calling await _next(context), then fixes it by moving the header assignment into context.Response.OnStarting, which runs just before headers are sent. ...