gpt4 book ai didi

c# - 只有第一次记录显示,除非强行处置

转载 作者:行者123 更新时间:2023-11-30 14:47:13 25 4
gpt4 key购买 nike

我正在使用 vs 2017,编写一个 netcoreapp2.0 库,并使用 UnitTest 项目对其进行测试(XUnit 和 NUnit 给出相同的结果)。
我注意到,除非我强行处理我的 Serilog 记录器,否则只有第一行会在 Seq 中结束。

这是我的 2 个类(class)。图书馆一:

public class Class1
{
public static Logger _log;

public Class1(Logger log)
{
_log = log;
_log.Verbose("Class 1 constructor fineshed");
}

public void LogMessage(string s)
{
_log.Debug("Got message: {Message}", s);
}
}

单元测试:

public class UnitTest1
{
public static Logger _log = new LoggerConfiguration()
.WriteTo.Seq("http://localhost:5341", Serilog.Events.LogEventLevel.Verbose)
.MinimumLevel.Verbose()
.CreateLogger();
[Fact]
public void Test1()
{
_log.Debug("Test 1 logging");
var c = new Class1(_log);
c.LogMessage("Logging from test");

_log.Information("Test fineshed");

_log.Dispose(); // Without this, only "Test 1 logging" is logged.
}
}

引用的程序集(来自单元测试项目):

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="serilog" Version="2.5.0" />
<PackageReference Include="serilog.sinks.seq" Version="3.3.3" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />

有什么建议吗?

(这里有一个演示项目的链接,用 vs 2017 打开恢复包并运行测试:demo project)

最佳答案

看来您遇到了同样的问题as described in this blog post.简而言之,您负责处理您的记录器,这会将其内容刷新到适当的接收器。

关于c# - 只有第一次记录显示,除非强行处置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46260308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com