Skip to content

Instantly share code, notes, and snippets.

@deepumi
Created August 13, 2019 14:23
Show Gist options
  • Save deepumi/7834dfdefed60301edfde3525f29f91d to your computer and use it in GitHub Desktop.
Save deepumi/7834dfdefed60301edfde3525f29f91d to your computer and use it in GitHub Desktop.
Logging class for CLI tools!
internal sealed class Logger : ILogger
{
private readonly FileStream _writer = new FileStream(Config.Configuration["LogFile"], FileMode.Create, FileAccess.Write,
FileShare.None, 1024, FileOptions.SequentialScan);
private readonly UTF8Encoding _utf = new UTF8Encoding(true);
private readonly byte[] _line;
internal Logger() => _line = _utf.GetBytes(Environment.NewLine);
void ILogger.Log(string message)
{
var info = _utf.GetBytes(message + "\n");
_writer.Write(info, 0, info.Length);
_writer.Write(_line, 0, _line.Length);
}
public void Dispose()
{
_writer.Flush();
_writer.Dispose();
}
}
internal interface ILogger : IDisposable
{
void Log(string message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment