12

I'm using a streamwriter in combination with a background worker, for logging.

As such, I have

System::Void
MyUI::execBWorker_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {

String^ outputPath = _clr::Settings::ApplicationLogPath("_log.txt", true, false);
logfile_ = gcnew StreamWriter(outputPath,true);

DoStuff();
logfile_->Close();
}

Things in the DoStuff() method raise the Progress event.

System::Void
MyUI::execBWorker_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
logfile_->WriteLine("something");
}

I think this really smells. How can I make it better, or at least how can I check the logfile hasn't been closed? There are a lot of messages, so I'm concerned about opening and closing the logfile continuously.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
Melanie
  • 1,349
  • 2
  • 17
  • 27

1 Answers1

26

If the StreamWriter is closed, the BaseStream property will return null.

leppie
  • 115,091
  • 17
  • 196
  • 297