Skip to content

VerifyTests/Verify.Serilog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

611 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.Serilog

Discussions Build status NuGet Status

Extends Verify to allow verification of Serilog 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

Setup

Use VerifySerilog.Initialize()

[ModuleInitializer]
public static void Initialize() =>
    VerifySerilog.Initialize();

Or omit the above is VerifierSettings.InitializePlugins(); is called:

[ModuleInitializer]
public static void Initialize() =>
    VerifierSettings.InitializePlugins();

LoggerConfiguration callback

VerifySerilog.Initialize accepts an optional Action<LoggerConfiguration> callback for customising the underlying Serilog LoggerConfiguration — for example to register destructuring policies, enrichers, or filters. The callback runs after MinimumLevel.Verbose, Enrich.FromLogContext, and the VerifySink have been wired up, so it can layer on top of those defaults.

[ModuleInitializer]
public static void Initialize() =>
    VerifySerilog.Initialize(
        _ => _.Destructure.ByTransforming<Customer>(
            customer => new
            {
                customer.Name
            }));

snippet source | anchor

Ignoring messages by SourceContext

VerifySerilog.IgnoreSourceContext filters out log events whose SourceContext property matches a known type or string. Generic and string overloads are provided. Typically called from the module initializer alongside Initialize.

VerifySerilog.IgnoreSourceContext<MyNoisyType>();
VerifySerilog.IgnoreSourceContext("Some.Namespace.Logger");

Events emitted via Log.ForContext<T>() or Log.ForContext("SourceContext", "...") whose source context matches are dropped before being recorded.

Recording and writing logs

[Test]
public Task Usage()
{
    Recording.Start();

    var result = Method();

    return Verify(result);
}

static string Method()
{
    Log.Error("The Message");
    return "Result";
}

snippet source | anchor

Results in:

{
  target: Result,
  log: {
    Error: The Message
  }
}

snippet source | anchor

About

Extends Verify to allow verification of Serilog bits.

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors

Languages