Skip to content

Repository files navigation

Verify.MicrosoftLogging

DiscussionsBuild statusNuGet Status

Extends Verify to allow verification of MicrosoftLogging bits.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Usage

[ModuleInitializer]publicstaticvoidInitialize()=>VerifyMicrosoftLogging.Initialize();

snippet source | anchor

Logging Recording allows, when a method is being tested, for any 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:

[Fact]publicTaskLogging(){Recording.Start();varlogger=newRecordingLogger();vartarget=newClassThatUsesLogging(logger);varresult=target.Method();returnVerify(result);}classClassThatUsesLogging(ILoggerlogger){publicstringMethod(){logger.LogWarning("The log entry");using(logger.BeginScope("The scope")){logger.LogWarning("Entry in scope");}return"result";}}

snippet source | anchor

Results in:

{
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<T>). LoggerProvider provides a builder method CreateLogger<T> to construct a Logger<T>:

[Fact]publicTaskLoggingTyped(){Recording.Start();varlogger=RecordingProvider.CreateLogger<ClassThatUsesTypedLogging>();vartarget=newClassThatUsesTypedLogging(logger);varresult=target.Method();returnVerify(result);}classClassThatUsesTypedLogging(ILogger<ClassThatUsesTypedLogging>logger){publicstringMethod(){logger.LogWarning("The log entry");return"result";}}

snippet source | anchor

Results in:

{
target: result,
log: {
Warning: The log entry,
Category: ClassThatUsesTypedLogging
}
}

snippet source | anchor

Icon

Log designed by Ben Davis from The Noun Project.

About

Extends Verify to allow verification of MicrosoftLogging bits.

Resources

Code of conduct

Stars

7 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages