https://github.com/verifytests/verify.microsoftlogging
Extends Verify to allow verification of MicrosoftLogging bits.
https://github.com/verifytests/verify.microsoftlogging
Last synced: 2 months ago
JSON representation
Extends Verify to allow verification of MicrosoftLogging bits.
- Host: GitHub
- URL: https://github.com/verifytests/verify.microsoftlogging
- Owner: VerifyTests
- License: mit
- Created: 2021-10-01T08:56:27.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-14T11:24:09.000Z (about 1 year ago)
- Last Synced: 2025-04-14T12:29:46.048Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 431 KB
- Stars: 7
- Watchers: 1
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license.txt
- Code of conduct: code_of_conduct.md
Awesome Lists containing this project
README
#
Verify.MicrosoftLogging
[](https://github.com/orgs/VerifyTests/discussions)
[](https://ci.appveyor.com/project/SimonCropp/verify-microsoftlogging)
[](https://www.nuget.org/packages/Verify.MicrosoftLogging/)
Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of MicrosoftLogging bits.
**See [Milestones](../../milestones?state=closed) for release notes.**
## Sponsors
### Entity Framework Extensions
[Entity Framework Extensions](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.MicrosoftLogging) is a major sponsor and is proud to contribute to the development this project.
[](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.MicrosoftLogging)
### Developed using JetBrains IDEs
[](https://jb.gg/OpenSourceSupport)
## NuGet
* https://nuget.org/packages/Verify.MicrosoftLogging
## Usage
```cs
[ModuleInitializer]
public static void Initialize() =>
VerifyMicrosoftLogging.Initialize();
```
snippet source | anchor
Logging Recording allows, when a method is being tested, for any [logging](https://docs.microsoft.com/en-us/dotnet/core/extensions/logging) made as part of that method call to be recorded and verified.
Call `LoggerRecording.Start();` to get an instance of the `LoggerProvider`. `LoggerProvider` implements both `ILogger` and `ILoggerProvider`.
Then pass in the `LoggerProvider` instance to a class/method that write log entries:
```cs
[Fact]
public Task Logging()
{
Recording.Start();
var logger = new RecordingLogger();
var target = new ClassThatUsesLogging(logger);
var result = target.Method();
return Verify(result);
}
class ClassThatUsesLogging(ILogger logger)
{
public string Method()
{
logger.LogWarning("The log entry");
using (logger.BeginScope("The scope"))
{
logger.LogWarning("Entry in scope");
}
return "result";
}
}
```
snippet source | anchor
Results in:
```txt
{
target: result,
log: [
{
Warning: The log entry
},
{
Message: StartScope,
State: The scope
},
{
Warning: Entry in scope
},
{
Message: EndScope
}
]
}
```
snippet source | anchor
### Typed
A common pattern is to use a type logger (`Logger`). `LoggerProvider` provides a builder method `CreateLogger` to construct a `Logger`:
```cs
[Fact]
public Task LoggingTyped()
{
Recording.Start();
var logger = RecordingProvider.CreateLogger();
var target = new ClassThatUsesTypedLogging(logger);
var result = target.Method();
return Verify(result);
}
class ClassThatUsesTypedLogging(ILogger logger)
{
public string Method()
{
logger.LogWarning("The log entry");
return "result";
}
}
```
snippet source | anchor
Results in:
```txt
{
target: result,
log: {
Warning: The log entry,
Category: ClassThatUsesTypedLogging
}
}
```
snippet source | anchor
## Icon
[Log](https://thenounproject.com/term/log/324064/) designed by [Ben Davis](https://thenounproject.com/smashicons/) from [The Noun Project](https://thenounproject.com).