https://github.com/ianwold/aspnetcoretestlogoutput
A simple demonstration of testing log output in an ASP.NET Core integration test
https://github.com/ianwold/aspnetcoretestlogoutput
asp-net asp-net-core dotnet dotnet-core ilogger iloggerprovider integration-testing logging netcore testing
Last synced: 3 months ago
JSON representation
A simple demonstration of testing log output in an ASP.NET Core integration test
- Host: GitHub
- URL: https://github.com/ianwold/aspnetcoretestlogoutput
- Owner: IanWold
- Created: 2025-01-11T19:46:17.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-01-12T17:15:06.000Z (4 months ago)
- Last Synced: 2025-03-04T06:14:37.802Z (3 months ago)
- Topics: asp-net, asp-net-core, dotnet, dotnet-core, ilogger, iloggerprovider, integration-testing, logging, netcore, testing
- Language: C#
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ASP.NET Core Test Log Output
This repository gives a simple example as to how to test log output in an integration test for an ASP.NET Core API. It is a working example for my blog post [Testing Logging in ASP.NET Core](https://ian.wold.guru/Posts/testing_logging_in_asp_net_core.html), which gives a background and explanation of the code in this repository.
The `Api` project contains a simple API setup with one `/test` endpoint that logs an error level. The `Test` project contains an `ILoggerProvider` implementation to capture logs and expose them in a way that the tests can easily read. The single `Test.TestErrorLogged` test demonstrates how to inject the test logger into the host when setting up a `WebApplicationFactory`.
## Running
In order to run, you'll need to ensure you have [the .NET 9 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/9.0) installed.
To run in Visual Studio, open the SLN, build, navigate to the Test Explorer, and execute the test.
To run in VS Code, I recommend installing the [C# Dev Kit extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit). With that extension, you can open the folder containing the project, build with dotnet, then navigate to the Testing view to execute the test.