简体   繁体   English

在xunit.console.exe出口上干净地解决Serilog Pump ThreadAbortException

[英]Cleanly resolving Serilog Pump ThreadAbortException on xunit.console.exe exit

I'm getting the following exception from xunit.console.exe when I shut down my app, which uses Serilog: 当我关闭使用Serilog的应用程序时,我从xunit.console.exe收到以下异常:

System.AggregateException: One or more errors occurred. ---> System.Threading.ThreadAbortException: Thread was being aborted.
   at Serilog.Sinks.Async.BackgroundWorkerSink.Pump()
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Serilog.Sinks.Async.BackgroundWorkerSink.Dispose()
   at Serilog.LoggerConfiguration.<CreateLogger>b__28_0()
---> (Inner Exception #0) System.Threading.ThreadAbortException: Thread was being aborted.
   at Serilog.Sinks.Async.BackgroundWorkerSink.Pump()
   at System.Threading.Tasks.Task.Execute()<---

Yes, I've done some bold things (creating a Serilog WriteTo.Async -wrapped set of loggers), but I need to figure out the cleanest way to not provoke the exception and get back to clean test runs. 是的,我已经做了一些大胆的事情(创建了一个由Serilog WriteTo.Async包装的记录器),但是我需要找出最干净的方法来避免引发异常,然后回到干净的测试运行中。 (If there was a hook for Assembly Uninitialization, I'd call Log.CloseAndFlush() in it) (如果有一个用于程序集未初始化的钩子,我会在其中调用Log.CloseAndFlush()

(Yes, I appreciate that a Unit Test should not be wiring up heavyweight logging, but I will still need to resolve the issue for integration tests) (是的,我很欣赏单元测试不应连接重量级的日志记录,但是我仍然需要解决集成测试的问题)

(Will be self-answering when I suss it out; I just couldnt google up an answer and believe one should be able to) (当我提出疑问时,它会自我回答;我只是无法在谷歌上找到答案,并相信一个人应该能够)

There's no point not just fixing your code - the whole point of having tests is to listen to them ;) 不仅要修复您的代码,没有意义-进行测试的全部目的是聆听它们;)

In this specific instance, some code was statically building loggers/auditors that are pretty central to the apps behavior, which should have been surfaced as constructor dependencies, not hidden in static variables deep within in the first instance. 在此特定实例中,一些代码静态地构建了记录器/审计器,这些记录器/审计器对于应用程序的行为非常重要,它们应该以构造函数依赖关系的形式出现,而不是隐藏在第一个实例内部的静态变量中。

The reason I arrived at this point was that my app has cross-cutting logging requirements, sufficiently central to the throughput to result in my switching logging approaches - being forced into this pushes the management of the logs to a better place, which is a win (and hiding it would have been another step down the slippery slope of paying perf costs for incidental things in the context of a test). 我到达这一点的原因是,我的应用程序具有跨领域的日志记录要求,对于吞吐量而言,它是至关重要的,以至于导致我切换日志记录方法-被迫将其推向更好的位置,这是一个胜利(并且将它隐藏起来,是在测试的背景下,为支付附带的事情的性能费用而滑坡的另一步)。

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

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