Created
February 11, 2019 10:54
-
-
Save chrisnas/1fedeabb5f76b853526f56a8296c26fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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