Skip to content

Instantly share code, notes, and snippets.

@chrisnas
Created February 11, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisnas/1fedeabb5f76b853526f56a8296c26fa to your computer and use it in GitHub Desktop.
Save chrisnas/1fedeabb5f76b853526f56a8296c26fa to your computer and use it in GitHub Desktop.
public abstract class GcLogBase : IGcLog
{
protected string Filename;
private StreamWriter _fileWriter;
public void Start(string filename)
{
if (string.IsNullOrEmpty(filename))
throw new ArgumentNullException(nameof(filename));
if (_fileWriter != null)
throw new InvalidOperationException("Start can't be called twice: Stop must be called first.");
_fileWriter = new StreamWriter(filename);
Filename = filename;
OnStart();
}
public void Stop()
{
if (string.IsNullOrEmpty(Filename))
return;
OnStop();
Filename = null;
_fileWriter.Flush();
_fileWriter.Dispose();
_fileWriter = null;
}
protected bool WriteLine(string line)
{
if (_fileWriter == null)
return false; // just in case the method is called AFTER Stop
_fileWriter.WriteLine(line);
return true;
}
protected abstract void OnStart();
protected abstract void OnStop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment