dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

An ILogger double out of the box as part of the testing utilities #47652

Open chivandikwa opened 3 years ago

chivandikwa commented 3 years ago

Is your feature request related to a problem? Please describe.

When writing tests for ASP.NET core there are a lot of utilities that make it easy like TestServer. However when it comes to testing logs there is nothing out of the box. Mocking ILogger can be quite tricky and option is to make a double. This seems like a really common case and should be handled out of the box.

Example: I am trying to check that a log of a given level has been made with a given scope.

Describe the solution you'd like

A memory based logger double that can be interacted with to check if a given log of certain level was made and the context available at time of log. This information can be then used with an assertion framework of choice so ideally getting a collection of log entries would be enough.

Additional context

Have a look here at something within the Google Cloud Functions framework that could help to understand what is required. The implementation there is pretty simple and solid. I got to know of this from a demo Jon Skeet gave on this at NDC London 2021.

https://github.com/GoogleCloudPlatform/functions-framework-dotnet/blob/master/src/Google.Cloud.Functions.Testing/MemoryLogger.cs

ghost commented 3 years ago

Tagging subscribers to this area: @maryamariyan See info in area-owners.md if you want to be subscribed.

Issue Details
### Is your feature request related to a problem? Please describe. When writing tests for ASP.NET core there are a lot of utilities that make it easy like TestServer. However when it comes to testing logs there is nothing out of the box. Mocking ILogger can be quite tricky and option is to make a double. This seems like a really common case and should be handled out of the box. Example: I am trying to check that a log of a given level has been made with a given scope. ### Describe the solution you'd like A memory based logger double that can be interacted with to check if a given log of certain level was made and the context available at time of log. This information can be then used with an assertion framework of choice so ideally getting a collection of log entries would be enough. ### Additional context Have a look here at something within the Google Cloud Functions framework that could help to understand what is required. The implementation there is pretty simple and solid. I got to know of this from a demo Jon Skeet gave on this at NDC London 2021. https://github.com/GoogleCloudPlatform/functions-framework-dotnet/blob/master/src/Google.Cloud.Functions.Testing/MemoryLogger.cs
Author: chivandikwa
Assignees: -
Labels: `area-Extensions-Logging`, `untriaged`
Milestone: -