{"id":15629815,"url":"https://github.com/martincostello/xunit-logging","last_synced_at":"2025-05-14T16:16:02.082Z","repository":{"id":34069639,"uuid":"145293601","full_name":"martincostello/xunit-logging","owner":"martincostello","description":"Logging extensions for xunit","archived":false,"fork":false,"pushed_at":"2025-05-12T09:50:42.000Z","size":1471,"stargazers_count":288,"open_issues_count":8,"forks_count":15,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-05-12T10:50:23.674Z","etag":null,"topics":["dotnet-core","logging","xunit"],"latest_commit_sha":null,"homepage":"https://blog.martincostello.com/writing-logs-to-xunit-test-output/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/martincostello.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["martincostello"],"buy_me_a_coffee":"martincostello"}},"created_at":"2018-08-19T10:33:15.000Z","updated_at":"2025-05-12T09:50:45.000Z","dependencies_parsed_at":"2023-01-15T04:23:43.142Z","dependency_job_id":"cc299c29-21d9-42f6-bf30-88f7d394e9df","html_url":"https://github.com/martincostello/xunit-logging","commit_stats":{"total_commits":835,"total_committers":8,"mean_commits":104.375,"dds":0.5437125748502993,"last_synced_commit":"9af4fcb7348d041619becd5d0ec912ad8444f0a8"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martincostello%2Fxunit-logging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martincostello%2Fxunit-logging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martincostello%2Fxunit-logging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martincostello%2Fxunit-logging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martincostello","download_url":"https://codeload.github.com/martincostello/xunit-logging/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254179905,"owners_count":22027884,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet-core","logging","xunit"],"created_at":"2024-10-03T10:28:57.624Z","updated_at":"2025-05-14T16:16:02.076Z","avatar_url":"https://github.com/martincostello.png","language":"C#","funding_links":["https://github.com/sponsors/martincostello","https://buymeacoffee.com/martincostello"],"categories":["others"],"sub_categories":[],"readme":"# xunit Logging\n\n[![Build status][build-badge]][build-status]\n[![codecov][coverage-badge]][coverage-report]\n[![OpenSSF Scorecard][scorecard-badge]][scorecard-report]\n\n| **xunit version** | **Package** | **NuGet Version** |\n|:------------------|:------------|:------------------|\n| xunit v2          | [MartinCostello.Logging.XUnit][package-download-v2] | [![NuGet][package-badge-version-v2]][package-download-v2] [![NuGet Downloads][package-badge-downloads-v2]][package-download-v2] |\n| xunit v3          | [MartinCostello.Logging.XUnit.v3][package-download-v3] | [![NuGet][package-badge-version-v3]][package-download-v3] [![NuGet Downloads][package-badge-downloads-v3]][package-download-v3] |\n\n## Introduction\n\n`MartinCostello.Logging.XUnit` and `MartinCostello.Logging.XUnit.v3` provide extensions to hook into\nthe `ILogger` infrastructure to output logs from your xunit tests to the test output.\n\nProjects using xunit v2 should use the `MartinCostello.Logging.XUnit` package, while projects using\nxunit v3 should use the `MartinCostello.Logging.XUnit.v3` package.\n\n\u003e [!NOTE]\n\u003e This library is designed for the Microsoft logging implementation of `ILoggerFactory`.\n\u003e For other logging implementations, such as [Serilog][serilog], consider using packages such as [Serilog.Sinks.XUnit][serilog-sinks-xunit] instead.\n\n### Installation\n\nTo install the library from NuGet using the .NET SDK run one of the following commands.\n\n#### For xunit v2\n\n```console\ndotnet add package MartinCostello.Logging.XUnit\n```\n\n#### For xunit v3\n\n```console\ndotnet add package MartinCostello.Logging.XUnit.v3\n```\n\n### Usage\n\n#### Dependency Injection\n\n```csharp\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Logging;\nusing Xunit;\nusing Xunit.Abstractions; // For xunit v2 - not required for xunit v3\n\nnamespace MyApp.Calculator;\n\npublic class CalculatorTests(ITestOutputHelper outputHelper)\n{\n    [Fact]\n    public void Calculator_Sums_Two_Integers()\n    {\n        // Arrange\n        using var serviceProvider = new ServiceCollection()\n            .AddLogging((builder) =\u003e builder.AddXUnit(outputHelper))\n            .AddSingleton\u003cCalculator\u003e()\n            .BuildServiceProvider();\n\n        var calculator = services.GetRequiredService\u003cCalculator\u003e();\n\n        // Act\n        int actual = calculator.Sum(1, 2);\n\n        // Assert\n        Assert.AreEqual(3, actual);\n    }\n}\n\npublic sealed class Calculator(ILogger\u003cCalculator\u003e logger)\n{\n    public int Sum(int x, int y)\n    {\n        int sum = x + y;\n\n        logger.LogInformation(\"The sum of {x} and {y} is {sum}.\", x, y, sum);\n\n        return sum;\n    }\n}\n```\n\n#### Standalone Logging Components\n\n```csharp\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Logging;\nusing Xunit;\nusing Xunit.Abstractions; // For xunit v2 - not required for xunit v3\n\nnamespace MyApp.Calculator;\n\npublic class CalculatorTests(ITestOutputHelper outputHelper)\n{\n    [Fact]\n    public void Calculator_Sums_Two_Integers()\n    {\n        // Arrange\n        var loggerFactory = LoggerFactory.Create(builder =\u003e builder\n            .AddProvider(new XUnitLoggerProvider(outputHelper, xunitLoggerOptions))\n            .SetMinimumLevel(LogLevel.Trace));\n\n        var logger = loggerFactory.CreateLogger\u003cCalculator\u003e();\n\n        var calculator = new Calculator(logger);\n\n        // Act\n        int actual = calculator.Sum(1, 2);\n\n        // Assert\n        Assert.AreEqual(3, actual);\n    }\n}\n\npublic sealed class Calculator(ILogger\u003cCalculator\u003e logger)\n{\n    public int Sum(int x, int y)\n    {\n        int sum = x + y;\n\n        logger.LogInformation(\"The sum of {x} and {y} is {sum}.\", x, y, sum);\n\n        return sum;\n    }\n}\n```\n\nSee below for links to more examples:\n\n- [Unit tests][example-unit-tests]\n- [Integration tests for an ASP.NET Core HTTP application][example-integration-tests]\n\n## Example Output\n\nIf your tests (and the system under test) are correctly configured, then you should see output from the\ntests in the same places you would expected to see [output from xunit tests][xunit-output].\n\nSee below for some examples from the [`Http_Get_Many`][example-test] test in this repository.\n\n\u003c!-- markdownlint-disable MD033 --\u003e\n\n### Visual Studio\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand\u003c/summary\u003e\n\n![Visual Studio Output][output-vs]\n\n\u003c/details\u003e\n\n### Visual Studio Code\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand\u003c/summary\u003e\n\n![Visual Studio Code Output][output-vscode]\n\n\u003c/details\u003e\n\n### Windows Terminal and .NET CLI\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand\u003c/summary\u003e\n\n![Windows Terminal Output][output-windows-terminal]\n\n\u003c/details\u003e\n\n## Migrating to xunit v3\n\n[Xunit v3][xunit-v3-whats-new] contains many major architectural changes which means the same package\nthat supports logging for xunit v2 cannot be used with xunit v3. The equivalent NuGet package to support\nlogging for xunit v3 is the new [MartinCostello.Logging.XUnit.v3][package-download-v3] package.\n\nTo migrate usage of `MartinCostello.Logging.XUnit` to `MartinCostello.Logging.XUnit.v3` for xunit v3:\n\n1. Follow the relevant steps to migrate any test projects from [xunit v2 to v3][xunit-v3-migration].\n    - The most relevant change in xunit v3 is that the `ITestOutputHelper` type has moved from the `Xunit.Abstractions` namespace to `Xunit`.\n1. Change any package references from `MartinCostello.Logging.XUnit` to `MartinCostello.Logging.XUnit.v3`.\n\n    ```diff\n    - \u003cPackageReference Include=\"MartinCostello.Logging.XUnit\" Version=\"0.5.0\" /\u003e\n    + \u003cPackageReference Include=\"MartinCostello.Logging.XUnit.v3\" Version=\"0.5.0\" /\u003e\n    ```\n\n## Feedback\n\nAny feedback or issues can be added to the issues for this project in [GitHub][issues].\n\n## Repository\n\nThe repository is hosted in [GitHub][repo]: \u003chttps://github.com/martincostello/xunit-logging.git\u003e\n\n## License\n\nThis project is licensed under the [Apache 2.0][license] license.\n\n## Building and Testing\n\nCompiling the solution yourself requires Git and the [.NET SDK][dotnet-sdk] to be installed (version `9.0.100` or later).\n\nTo build and test the solution locally from a terminal/command-line, run the following set of commands:\n\n```powershell\ngit clone https://github.com/martincostello/xunit-logging.git\ncd xunit-logging\n./build.ps1\n```\n\n[build-badge]: https://github.com/martincostello/xunit-logging/actions/workflows/build.yml/badge.svg?branch=main\u0026event=push\n[build-status]: https://github.com/martincostello/xunit-logging/actions?query=workflow%3Abuild+branch%3Amain+event%3Apush \"Continuous Integration for this project\"\n[coverage-badge]: https://codecov.io/gh/martincostello/xunit-logging/branch/main/graph/badge.svg\n[coverage-report]: https://codecov.io/gh/martincostello/xunit-logging \"Code coverage report for this project\"\n[scorecard-badge]: https://api.securityscorecards.dev/projects/github.com/martincostello/xunit-logging/badge\n[scorecard-report]: https://securityscorecards.dev/viewer/?uri=github.com/martincostello/xunit-logging \"OpenSSF Scorecard for this project\"\n[dotnet-sdk]: https://dot.net/download \"Download the .NET SDK\"\n[example-integration-tests]: https://github.com/martincostello/xunit-logging/blob/main/tests/Shared/Integration/HttpApplicationTests.cs \"Integration test examples\"\n[example-test]: https://github.com/martincostello/xunit-logging/blob/8951660a4667cfa5b07e2558b741b335a31d1e59/tests/Shared/Integration/HttpApplicationTests.cs#L26-L58 \"Http_Get_Many\"\n[example-unit-tests]: https://github.com/martincostello/xunit-logging/blob/main/tests/Shared/Examples.cs \"Unit test examples\"\n[issues]: https://github.com/martincostello/xunit-logging/issues \"Issues for this project on GitHub.com\"\n[license]: https://www.apache.org/licenses/LICENSE-2.0.txt \"The Apache 2.0 license\"\n[output-vs]: ./docs/images/output-vs.png \"xunit Output in Visual Studio\"\n[output-vscode]: ./docs/images/output-vscode.png \"xunit Output in Visual Studio Code\"\n[output-windows-terminal]: ./docs/images/output-terminal.png \"xunit Output in Windows Terminal with the .NET CLI\"\n[package-badge-downloads-v2]: https://img.shields.io/nuget/dt/MartinCostello.Logging.XUnit?logo=nuget\u0026label=Downloads\u0026color=blue\n[package-badge-downloads-v3]: https://img.shields.io/nuget/dt/MartinCostello.Logging.XUnit.v3?logo=nuget\u0026label=Downloads\u0026color=blue\n[package-badge-version-v2]: https://img.shields.io/nuget/v/MartinCostello.Logging.XUnit?logo=nuget\u0026label=Latest\u0026color=blue\n[package-badge-version-v3]: https://img.shields.io/nuget/v/MartinCostello.Logging.XUnit.v3?logo=nuget\u0026label=Latest\u0026color=blue\n[package-download-v2]: https://www.nuget.org/packages/MartinCostello.Logging.XUnit \"Download MartinCostello.Logging.XUnit from NuGet\"\n[package-download-v3]: https://www.nuget.org/packages/MartinCostello.Logging.XUnit.v3 \"Download MartinCostello.Logging.XUnit.v3 from NuGet\"\n[repo]: https://github.com/martincostello/xunit-logging \"This project on GitHub.com\"\n[serilog]: https://serilog.net/ \"Serilog website\"\n[serilog-sinks-xunit]: https://github.com/trbenning/serilog-sinks-xunit \"Serilog.Sinks.XUnit on GitHub\"\n[xunit-output]: https://xunit.net/docs/capturing-output \"Capturing Output - xUnit.net Documentation\"\n[xunit-v3-migration]: https://xunit.net/docs/getting-started/v3/migration \"Migrating from xunit v2 to v3\"\n[xunit-v3-whats-new]: https://xunit.net/docs/getting-started/v3/whats-new \"What's New in v3\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartincostello%2Fxunit-logging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartincostello%2Fxunit-logging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartincostello%2Fxunit-logging/lists"}