Skip to content

Should StreamLogger be TextWriterLogger? #2400

@nietras

Description

@nietras

StreamLogger is for some reason (other than name) tied to only being useable with StreamWriter, which is just a TextWriter, as far as I can tell there is no reason for the implementation to tie into it being a StreamWriter and one could easily refactor to TextWriter or extracting a base class TextWriterLogger with all code, but which allows using a TextWriter instead. This would allow exporting to in-memory StringWriter instead of having to go through a MemoryStream.

public class StreamLogger : ILogger, IDisposable
{
private readonly StreamWriter writer;
public StreamLogger(StreamWriter writer) => this.writer = writer;
public void Dispose() => writer.Dispose();
[PublicAPI]
public StreamLogger(string filePath, bool append = false) => writer = new StreamWriter(filePath, append);
public string Id => nameof(StreamLogger);
public int Priority => 0;
public void Write(LogKind logKind, string text) => writer.Write(text);
public void WriteLine() => writer.WriteLine();
public void WriteLine(LogKind logKind, string text) => writer.WriteLine(text);
public void Flush() => writer.Flush();
}
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions