【问题标题】:Despite being awaited, Console.Out.WriteLine throwing Exceptions尽管等待,Console.Out.WriteLine 抛出异常
【发布时间】:2014-03-15 13:00:51
【问题描述】:

如果我在我的一个项目中取消注释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 

我想让我感到困惑的是,尽管在等待声明,但我还是得到了这个异常。

【问题讨论】:

  • await 不保证进程仍然有控制台。请使用真正的记录器。
  • 同意,我应该马上开始。谢谢!另外:有什么建议吗?

标签: c# exception asynchronous console-application


【解决方案1】:

随着事情的发展,使用Console.Out 获取调试信息最终会干扰您的程序。

截至发帖时,我觉得NLog 是记录日志的最佳选择。特别是在异步应用程序中,因为它们的非阻塞 API 和非常简单的配置。

【讨论】:

    猜你喜欢
    • 2016-01-19
    • 2013-04-20
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2019-10-19
    • 2018-07-07
    相关资源
    最近更新 更多