First of all, is it all about logging? If so, don't do those streams. Use the class
System.Diagnostics.EventLog
; please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[
^].
For advanced uses (including uses of a file), please see my past answers:
MsBuild OutPut to the TextBox on the fly in Windows Application[
^],
How to create event log under a folder[
^].
Now, about the problem with the stream you have. Your code is not quite correct (you are not using exception handling which could be done via the
using
statement, as Dean have demonstrated), but the problem is not in the code you have shown. The problem is the actual run-time type and mode of the stream you had to create before calling your method. To see the problem, I would need to see how you construct this object. If you can use with file, the answer by Dean provides a perfect code sample.
—SA