Azure Webjob TextWriter logger being disposed in the middle of my method

断了今生、忘了曾经 提交于 2019-12-21 04:11:38

问题


I'm using a Webjob with the Windows Azure Storage SDK. When a new item shows up in a Queue, a method in my class is invoked. According to the SDK docs, if I take a TextWriter as a parameter to my method, the SDK will provide me with a TextWriter that I can write to which will show up in the Webjob's logging infrastructure. This makes it pretty easy to diagnose issues and troubleshoot things.

public async static void ProcessQueueMessage([QueueTrigger("queueName")]MyModelType model, TextWriter logger)
{

    await logger.WriteLineAsync(string.Format("Processing Item {0}", model.SasUrl));

    // Some work here

    await logger.WriteLineAsync(string.Format("Done Processing Item {0}", model.SasUrl));
}

However, very frequently, within the body of my method, the TextWriter is being disposed of. I'm getting the following exception on the 2nd logger.WriteLineAsync:

System.ObjectDisposedException was unhandled by user code
  HResult=-2146232798
  Message=Cannot write to a closed TextWriter.
  Source=mscorlib
  ObjectName=""
  StackTrace:
       at System.IO.__Error.WriterClosed()
       at System.IO.StringWriter.Write(Char[] buffer, Int32 index, Int32 count)
       at System.IO.TextWriter.WriteLine(String value)
       at System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
       at System.IO.TextWriter.SyncTextWriter.WriteLineAsync(String value)
       at NameOfProject.Program.<ProcessQueueMessage>d__8.MoveNext() in c:\Dev\Path\To\Program.cs:line 173
  InnerException:

I can't find others having this problem, so I can't imagine there is a bug in the SDK or webjobs infrastructure.

Is there a way to tell if the logger is disposed of ahead of the call?

Is there a way to create a new logger within my method that will participate in the WebJobs logging subsystems and UI?


回答1:


That's because your method returns void. Try returning Task instead



来源:https://stackoverflow.com/questions/31507384/azure-webjob-textwriter-logger-being-disposed-in-the-middle-of-my-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!