Azure Webjob TextWriter logger being disposed in t

2019-03-26 01:47发布

问题:

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