简体   繁体   English

ILogger输出到dotnet测试

[英]ILogger that outputs into dotnet test

When I do 当我做

LoggerFactory
  .Create(builder => builder.AddTraceSource(nameof(TestName)).AddConsole())
  .CreateLogger(nameof(TestName))
  .LogError("oh no");

I kind of expect that "oh no" will be visible in dotnet test --verbosity detailed output. 我有点期待在“网络dotnet test --verbosity detailed输出”中可以看到“哦不”。 But it is not. 但事实并非如此。 How to obtain ILogger so that its output is visible in test run output? 如何获得ILogger以使其输出在测试运行输出中可见?

I'm using Microsoft.Extensions.Logging 3.0.0-preview3.19153.1 and .NET Core SDK 2.1.500. 我正在使用Microsoft.Extensions.Logging 3.0.0-preview3.19153.1和.NET Core SDK 2.1.500。

This happens because console logging is performed in background thread. 这是因为控制台日志记录在后台线程中执行。 So to "fix" this try to either dispose services : 所以要“修复”这个尝试要么处置服务

((IDisposable) services)?.Dispose();

or at least give it some time to handle - sleep for a second: 或者至少给它一些时间来处理 - 睡一会儿:

LoggerFactory
  .Create(builder => builder.AddTraceSource(nameof(TestName)).AddConsole())
  .CreateLogger(nameof(TestName))
  .LogError("oh no");

System.Threading.Thread.Sleep(1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM