Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 777
Nate Jackson Avatar asked Jul 20 '15 00:07

Nate Jackson


1 Answers

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

like image 94
Victor Hurdugaci Avatar answered Oct 31 '22 03:10

Victor Hurdugaci