I'm coding in VC++.NET 2003. I have a class that needs a log file to log info and errors for debugging. The problem I have is that FileStream and StreamWriter can not be global. I get compiler error C3145 : cannot declare a global or static managed type object or a __gc pointer. A workaround was to open, write, and then close the log file in every function/event. During testing, I discovered that this won't work since other functions/events may be executed before the previous function/event has closed the file. Any suggestions?
FileStream* fs = new FileStream(S"c:\\Variables.txt", FileMode::Append, FileAccess::Write, FileShare::Write);
fs->Close();
StreamWriter* sw = new StreamWriter(S"c:\\Variables.txt", true, Encoding::ASCII);
String* NextLine=S"This is the appended line.";
sw->Write(NextLine);
sw->Close();