using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace ExtremelySimpleLogger {
///
/// Implementation of a that writes to a .
/// The log writer constructs a message based on calls to and its variations and then submits the message to the underlying when or is called. and its variations submit the message immediately.
///
public class LogWriter : TextWriter {
///
/// The log level that this log writer should write with.
///
public LogLevel Level {
get {
lock (this.logger)
return this.level;
}
set {
lock (this.logger)
this.level = value;
}
}
///
public override Encoding Encoding => Encoding.UTF8;
private readonly StringBuilder line = new StringBuilder();
private readonly Logger logger;
private LogLevel level;
///
/// Creates a new log writer with the given settings.
///
/// The logger to write to.
/// The log level to write with.
public LogWriter(Logger logger, LogLevel level = LogLevel.Info) {
this.logger = logger;
this.level = level;
}
///
public override void Write(char value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(char[] buffer, int index, int count) {
lock (this.logger)
this.line.Append(buffer, index, count);
}
///
public override void Write(string value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(char[] buffer) {
lock (this.logger)
this.line.Append(buffer);
}
///
public override void Write(bool value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(int value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(uint value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(long value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(ulong value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(float value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(double value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(decimal value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(object value) {
lock (this.logger)
this.line.Append(value);
}
///
public override void Write(string format, object arg0) {
lock (this.logger)
this.line.AppendFormat(this.FormatProvider, format, arg0);
}
///
public override void Write(string format, object arg0, object arg1) {
lock (this.logger)
this.line.AppendFormat(this.FormatProvider, format, arg0, arg1);
}
///
public override void Write(string format, object arg0, object arg1, object arg2) {
lock (this.logger)
this.line.AppendFormat(this.FormatProvider, format, arg0, arg1, arg2);
}
///
public override void Write(string format, params object[] arg) {
lock (this.logger)
this.line.AppendFormat(this.FormatProvider, format, arg);
}
///
public override void WriteLine(string value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(char value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(char[] buffer) {
this.Write(buffer);
this.WriteLine();
}
///
public override void WriteLine(char[] buffer, int index, int count) {
this.Write(buffer, index, count);
this.WriteLine();
}
///
public override void WriteLine(bool value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(int value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(uint value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(long value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(ulong value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(float value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(double value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(decimal value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(object value) {
this.Write(value);
this.WriteLine();
}
///
public override void WriteLine(string format, object arg0) {
this.Write(format, arg0);
this.WriteLine();
}
///
public override void WriteLine(string format, object arg0, object arg1) {
this.Write(format, arg0, arg1);
this.WriteLine();
}
///
public override void WriteLine(string format, object arg0, object arg1, object arg2) {
this.Write(format, arg0, arg1, arg2);
this.WriteLine();
}
///
public override void WriteLine(string format, params object[] arg) {
this.Write(format, arg);
this.WriteLine();
}
///
public override void WriteLine() {
this.Flush();
}
///
public override Task WriteAsync(char value) {
return Task.Run(() => this.Write(value));
}
///
public override Task WriteAsync(string value) {
return Task.Run(() => this.Write(value));
}
///
public override Task WriteAsync(char[] buffer, int index, int count) {
return Task.Run(() => this.Write(buffer, index, count));
}
///
public override Task WriteLineAsync(char value) {
return Task.Run(() => this.WriteLine(value));
}
///
public override Task WriteLineAsync(string value) {
return Task.Run(() => this.WriteLine(value));
}
///
public override Task WriteLineAsync(char[] buffer, int index, int count) {
return Task.Run(() => this.WriteLine(buffer, index, count));
}
///
public override Task WriteLineAsync() {
return Task.Run(this.WriteLine);
}
///
public override Task FlushAsync() {
return Task.Run(this.Flush);
}
///
public override void Flush() {
lock (this.logger) {
this.logger.Log(this.level, this.line);
this.line.Clear();
}
}
}
}