Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a streamwriter is closed?

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.

like image 755
Melanie Avatar asked Jul 04 '12 06:07

Melanie


1 Answers

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

like image 180
leppie Avatar answered Oct 08 '22 22:10

leppie