0

I need to write two logs in two separate files: one writes 530 chars 30 times per second, the other one 50 chras 60 times per second. I save the data that will be written in two separate variables and write them every n and m frames separately. To write the variables I use:

using (StreamWriter writer = new StreamWriter(newFileName, true))

and then

writer.Write(data)

Now... I know the pros of using "using", but I was wondering: does it have a overhead? Why not declare a StreamWriter at the beginning of the code and use it when needed?

4
  • 7
    Close/dispose unmanaged resources as soon as you're finished with it. Commented Nov 22, 2013 at 13:46
  • To directly answer your question directly: the reason that you would want to surround a resource with a using rather than just declaring it and accessing it when you use it is because of unexpected exits. If something in your code throws an error, or the process is stopped for any reason then you won't have a chance to clean up. You need to decide how likely it is you will exit unexpectedly, which exceptions to catch and if not cleaning up is acceptable in these situations. Commented Nov 22, 2013 at 13:50
  • Building on @Tigran's answer, take a look at this SO post, it details a similar scenario. Commented Nov 22, 2013 at 13:51
  • Who is consuming said log files and how often? If you didn't close the stream(s) or Flush() them, then anyone attempting to read the log files might not see the latest data (or might even error out, depending how they read it). Something to think about... Commented Nov 22, 2013 at 13:55

3 Answers 3

6

Well, if you need to write data with such speed and such frequency, I would suggest do not use using at all, as using at the end will close and dispose stream object, so on the next request you need to reinitialize and reopen stream, which costs.

So just open ones and use all along you need it. After dispose it manually.

Sign up to request clarification or add additional context in comments.

1 Comment

Why did this get a negative vote? It is clearly the best thing he can do. Using which such frequency iincurs to much cost. He SHOULD guard against open Handles with a tra...catch finally construction, but that construction should be the better solution. There are times where nominal best solution won't fit. This is probably such a time.
2

The using statement itself doesn't have any real overhead. However, opening and closing unmanaged resource may. If you're going to leave it open, rather than leveraging the using, then the class doing the logging needs to implement IDispoable. Further, in the Dispose method it needs to properly dispose of the stream. Finally, the consumer that leverages the logger, make sure it uses the using command while it's leveraging it.

Comments

1

as using throws in a try catch block:

using (StreamWriter writer = new StreamWriter(newFileName, true)) 
{
    //do stuff
}

is equivalent to:

StreamWriter writer = new StreamWriter(newFileName, true);
try
{
    //do stuff
}
finally
{
   if (writer!= null)
      ((IDisposable)writer).Dispose();
}

http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.110%29.aspx

It is faster to call the .Dispose() function manually, or as Tigran pointed out, don't dispose at all. But, be careful.

2 Comments

Yes I'm aware of that translation :) the probem is the continuous open/close/re-open of the stream that comes from this way of writing to file.
@Marco: If you decide to keep it open, you might want to checkout the flush .Flush() method. It might help you to sync the actual write operations. msdn.microsoft.com/en-us/library/… Be careful though, it is time consuming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.