An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Verify.MicrosoftLogging

[![Discussions](https://img.shields.io/badge/Verify-Discussions-yellow?svg=true&label=)](https://github.com/orgs/VerifyTests/discussions)
[![Build status](https://img.shields.io/appveyor/build/SimonCropp/verify-microsoftlogging)](https://ci.appveyor.com/project/SimonCropp/verify-microsoftlogging)
[![NuGet Status](https://img.shields.io/nuget/v/Verify.MicrosoftLogging.svg)](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.

[![Entity Framework Extensions](https://raw.githubusercontent.com/VerifyTests/Verify.MicrosoftLogging/refs/heads/main/docs/zzz.png)](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.MicrosoftLogging)

### Developed using JetBrains IDEs

[![JetBrains logo.](https://raw.githubusercontent.com/VerifyTests/Verify.MicrosoftLogging/main/docs/jetbrains.png)](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).