尽管等待,Console.Out.WriteLine抛出异常

时间:2014-03-15 13:00:51

标签: c# exception asynchronous console-application

如果我在我的项目中取消注释this line of code,并在其后注释掉那个,我偶尔会得到例外&堆栈跟踪:

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 

我想让我感到困惑的是,我在等待声明的同时得到了这个例外。

1 个答案:

答案 0 :(得分:1)

事实上,使用Console.Out进行调试信息最终会干扰您的程序。

截至发布时,我认为NLog是记录的最佳选择。特别是在异步应用程序中,由于它们具有非阻塞API和非常简单的配置。