C# 尽管等待,Console.Out.WriteLine仍会引发异常

C# 尽管等待,Console.Out.WriteLine仍会引发异常,c#,exception,asynchronous,console-application,C#,Exception,Asynchronous,Console Application,如果我在我的项目中取消注释,并注释掉后面的项目,偶尔会得到异常和堆栈跟踪: System.InvalidOperationException: Operation is not valid due to the current state of the object at System.IO.StreamWriter.CheckState () [0x00000] in <filename unknown>:0 at System.IO.StreamWriter.Write

如果我在我的项目中取消注释,并注释掉后面的项目,偶尔会得到异常和堆栈跟踪:

System.InvalidOperationException: Operation is not valid due to the current state of the object
  at System.IO.StreamWriter.CheckState () [0x00000] in <filename unknown>:0 
  at System.IO.StreamWriter.WriteLineAsync (System.String value) [0x00000] in <filename unknown>:0 
  at System.IO.SynchronizedWriter.WriteLineAsync (System.String value) [0x00000] in <filename unknown>:0 
  at Gerty.Service+<PullJob>c__async1.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <filename unknown>:0 
  at Gerty.Service+<Work>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
System.InvalidOperationException:由于对象的当前状态,操作无效
在0中的System.IO.StreamWriter.CheckState()[0x00000]处
在0中的System.IO.StreamWriter.WriteLineAsync(System.String值)[0x00000]处
0中的System.IO.SynchronizedWriter.WriteLineAsync(System.String值)[0x00000]处
在:0中的Gerty.Service+c__async1.MoveNext()[0x00000]处
---来自引发异常的上一个位置的堆栈结束跟踪---
0中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x00000]处
0中的System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000]处
在:0中的Gerty.Service+c__async0.MoveNext()[0x00000]处

我想让我困惑的是,尽管等待语句,我还是得到了这个异常。

随着情况的发展,使用
控制台。对于调试信息,Out
最终会干扰您的程序


在这篇文章中,我觉得这是最好的日志选择。特别是在异步应用程序中,因为它们的非阻塞API和非常简单的配置。

await不能保证进程仍然有控制台。一定要使用真正的记录器。同意,我应该马上开始。谢谢还有:有什么建议吗?