Skip to content

Render logs on exception #122

@colinangusmackay

Description

@colinangusmackay

Set up a way to render logs when a test fails.

e.g.

public void Test()
{
  var logProvider = new TestCaptureLoggerProvider();
  using var rox = logProvider.RenderOnTestFailure(
    p => p.GetLogEntriesFor<ServiceBeingTested>(),
    Formatter.SimpleBySequence,
    Sink.Console);
  // do test stuff.

  rox.Success(); // If it gets here then logs are not rendered.
  // If the test method ends by throwing an exception the logs are automatically rendered.
  // i.e. rox is disposable, and if the success flag is not set, then anything that matches
  // the filter is rendered.
}

RenderOnTestFailure has three arguments

  • A function that filters the captured logs. e.g. p => GetLogsWithExceptions()
  • A formatter for rendering the log. e.g. l => $"[{le.LogLevel}] {le.FormattedMessage}"
  • A sink for forwarding the rendered log. e.g. s => XunitOutputHelper.WriteLine(s)

This method is available on the log provider and the logger itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions