Last active
August 29, 2015 14:04
-
-
Save rostreim/9461bb88b097f36b34ce to your computer and use it in GitHub Desktop.
A text writer implementation that provides a Gel log writer
This file contains hidden or 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
// | |
// Copyright 2014, Desert Software Solutions Inc. | |
// LogWriter.cs: https://gist.github.com/rostreim/9461bb88b097f36b34ce | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Gel; | |
/// <summary> | |
/// A LogWriter class that provides a TextWriter abstraction to a Gel.ILogger instance. | |
/// </summary> | |
public class LogWriter : TextWriter | |
{ | |
private Encoding encoding = Encoding.Default; | |
// private IFormatProvider formatProvider = null; | |
private ILogger logger; | |
private LogLevel level = LogLevel.Info; | |
public enum LogLevel | |
{ | |
Debug, | |
Info, | |
Warn, | |
Error, | |
Fatal, | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="LogWriter"/> class. | |
/// </summary> | |
/// <param name="logger">The logger to log to.</param> | |
/// <param name="level">The level to log with.</param> | |
public LogWriter(ILogger logger, LogLevel level = LogLevel.Info) { | |
this.logger = logger; | |
this.level = level; | |
} | |
public LogLevel Level { | |
get { return this.level; } | |
set { this.level = value; } | |
} | |
public ILogger Logger { | |
get { return this.logger; } | |
set { this.logger = value; } | |
} | |
/// <summary> | |
/// When overridden in a derived class, returns the character encoding in which the output is written. | |
/// </summary> | |
/// <returns>The character encoding in which the output is written.</returns> | |
public override Encoding Encoding { | |
get { return this.encoding; } | |
} | |
// | |
// The write operators | |
// | |
private void WriteToLog(object value) { | |
switch (this.level) { | |
case LogLevel.Debug: | |
this.logger.Debug(value); | |
break; | |
case LogLevel.Error: | |
this.logger.Error(value); | |
break; | |
case LogLevel.Fatal: | |
this.logger.Fatal(value); | |
break; | |
case LogLevel.Info: | |
this.logger.Info(value); | |
break; | |
case LogLevel.Warn: | |
this.logger.Warn(value); | |
break; | |
} | |
} | |
private void WriteToLog(string format, params object[] arg) { | |
switch (this.level) { | |
case LogLevel.Debug: | |
this.logger.Debug(format, arg); | |
break; | |
case LogLevel.Error: | |
this.logger.Error(format, arg); | |
break; | |
case LogLevel.Fatal: | |
this.logger.Fatal(format, arg); | |
break; | |
case LogLevel.Info: | |
this.logger.Info(format, arg); | |
break; | |
case LogLevel.Warn: | |
this.logger.Warn(format, arg); | |
break; | |
} | |
} | |
/// <summary> | |
/// Writes the text representation of a Boolean value to the text string or stream. | |
/// </summary> | |
/// <param name="value">The Boolean value to write.</param> | |
public override void Write(bool value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes a character to the text string or stream. | |
/// </summary> | |
/// <param name="value">The character to write to the text stream.</param> | |
public override void Write(char value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes a character array to the text string or stream. | |
/// </summary> | |
/// <param name="buffer">The character array to write to the text stream.</param> | |
public override void Write(char[] buffer) { | |
WriteToLog(buffer); | |
} | |
/// <summary> | |
/// Writes the text representation of a decimal value to the text string or stream. | |
/// </summary> | |
/// <param name="value">The decimal value to write.</param> | |
public override void Write(decimal value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of an 8-byte floating-point value to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 8-byte floating-point value to write.</param> | |
public override void Write(double value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of a 4-byte floating-point value to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 4-byte floating-point value to write.</param> | |
public override void Write(float value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of a 4-byte signed integer to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 4-byte signed integer to write.</param> | |
public override void Write(int value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of an 8-byte signed integer to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 8-byte signed integer to write.</param> | |
public override void Write(long value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of an object to the text string or stream by calling the ToString method on that object. | |
/// </summary> | |
/// <param name="value">The object to write.</param> | |
public override void Write(object value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes a string to the text string or stream. | |
/// </summary> | |
/// <param name="value">The string to write.</param> | |
public override void Write(string value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of a 4-byte unsigned integer to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 4-byte unsigned integer to write.</param> | |
public override void Write(uint value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of an 8-byte unsigned integer to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 8-byte unsigned integer to write.</param> | |
public override void Write(ulong value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes a formatted string to the text string or stream, using the same semantics as the <see cref="M:System.String.Format(System.String,System.Object)" /> method. | |
/// </summary> | |
/// <param name="format">A composite format string (see Remarks).</param> | |
/// <param name="arg0">The object to format and write.</param> | |
public override void Write(string format, object arg0) { | |
WriteToLog(format, arg0); | |
} | |
/// <summary> | |
/// Writes a formatted string to the text string or stream, using the same semantics as the <see cref="M:System.String.Format(System.String,System.Object[])" /> method. | |
/// </summary> | |
/// <param name="format">A composite format string (see Remarks).</param> | |
/// <param name="arg">An object array that contains zero or more objects to format and write.</param> | |
public override void Write(string format, params object[] arg) { | |
WriteToLog(format, arg); | |
} | |
/// <summary> | |
/// Writes a subarray of characters to the text string or stream. | |
/// </summary> | |
/// <param name="buffer">The character array to write data from.</param> | |
/// <param name="index">The character position in the buffer at which to start retrieving data.</param> | |
/// <param name="count">The number of characters to write.</param> | |
public override void Write(char[] buffer, int index, int count) { | |
WriteToLog(buffer.Skip(index).Take(count)); | |
} | |
/// <summary> | |
/// Writes a formatted string to the text string or stream, using the same semantics as the <see cref="M:System.String.Format(System.String,System.Object,System.Object)" /> method. | |
/// </summary> | |
/// <param name="format">A composite format string (see Remarks).</param> | |
/// <param name="arg0">The first object to format and write.</param> | |
/// <param name="arg1">The second object to format and write.</param> | |
public override void Write(string format, object arg0, object arg1) { | |
WriteToLog(format, arg0, arg1); | |
} | |
/// <summary> | |
/// Writes a formatted string to the text string or stream, using the same semantics as the <see cref="M:System.String.Format(System.String,System.Object,System.Object,System.Object)" /> method. | |
/// </summary> | |
/// <param name="format">A composite format string (see Remarks).</param> | |
/// <param name="arg0">The first object to format and write.</param> | |
/// <param name="arg1">The second object to format and write.</param> | |
/// <param name="arg2">The third object to format and write.</param> | |
public override void Write(string format, object arg0, object arg1, object arg2) { | |
WriteToLog(format, arg0, arg1, arg2); | |
} | |
/// <summary> | |
/// Writes a line terminator to the text string or stream. | |
/// </summary> | |
public override void WriteLine() { | |
WriteToLog(""); | |
} | |
/// <summary> | |
/// Writes the text representation of a Boolean value followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The Boolean value to write.</param> | |
public override void WriteLine(bool value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes a character followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The character to write to the text stream.</param> | |
public override void WriteLine(char value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes an array of characters followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="buffer">The character array from which data is read.</param> | |
public override void WriteLine(char[] buffer) { | |
WriteToLog(buffer); | |
} | |
/// <summary> | |
/// Writes the text representation of a decimal value followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The decimal value to write.</param> | |
public override void WriteLine(decimal value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of a 8-byte floating-point value followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 8-byte floating-point value to write.</param> | |
public override void WriteLine(double value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of a 4-byte floating-point value followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 4-byte floating-point value to write.</param> | |
public override void WriteLine(float value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of a 4-byte signed integer followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 4-byte signed integer to write.</param> | |
public override void WriteLine(int value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of an 8-byte signed integer followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 8-byte signed integer to write.</param> | |
public override void WriteLine(long value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of an object by calling the ToString method on that object, followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The object to write. If <paramref name="value" /> is null, only the line terminator is written.</param> | |
public override void WriteLine(object value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes a string followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The string to write. If <paramref name="value" /> is null, only the line terminator is written.</param> | |
public override void WriteLine(string value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of a 4-byte unsigned integer followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 4-byte unsigned integer to write.</param> | |
public override void WriteLine(uint value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes the text representation of an 8-byte unsigned integer followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="value">The 8-byte unsigned integer to write.</param> | |
public override void WriteLine(ulong value) { | |
WriteToLog(value); | |
} | |
/// <summary> | |
/// Writes a formatted string and a new line to the text string or stream, using the same semantics as the <see cref="M:System.String.Format(System.String,System.Object)" /> method. | |
/// </summary> | |
/// <param name="format">A composite format string (see Remarks).</param> | |
/// <param name="arg0">The object to format and write.</param> | |
public override void WriteLine(string format, object arg0) { | |
WriteToLog(format, arg0); | |
} | |
/// <summary> | |
/// Writes out a formatted string and a new line, using the same semantics as <see cref="M:System.String.Format(System.String,System.Object)" />. | |
/// </summary> | |
/// <param name="format">A composite format string (see Remarks).</param> | |
/// <param name="arg">An object array that contains zero or more objects to format and write.</param> | |
public override void WriteLine(string format, params object[] arg) { | |
WriteToLog(format, arg); | |
} | |
/// <summary> | |
/// Writes a subarray of characters followed by a line terminator to the text string or stream. | |
/// </summary> | |
/// <param name="buffer">The character array from which data is read.</param> | |
/// <param name="index">The character position in <paramref name="buffer" /> at which to start reading data.</param> | |
/// <param name="count">The maximum number of characters to write.</param> | |
public override void WriteLine(char[] buffer, int index, int count) { | |
WriteToLog(buffer.Skip(index).Take(count)); | |
} | |
/// <summary> | |
/// Writes a formatted string and a new line to the text string or stream, using the same semantics as the <see cref="M:System.String.Format(System.String,System.Object,System.Object)" /> method. | |
/// </summary> | |
/// <param name="format">A composite format string (see Remarks).</param> | |
/// <param name="arg0">The first object to format and write.</param> | |
/// <param name="arg1">The second object to format and write.</param> | |
public override void WriteLine(string format, object arg0, object arg1) { | |
WriteToLog(format, arg0, arg1); | |
} | |
/// <summary> | |
/// Writes out a formatted string and a new line, using the same semantics as <see cref="M:System.String.Format(System.String,System.Object)" />. | |
/// </summary> | |
/// <param name="format">A composite format string (see Remarks).</param> | |
/// <param name="arg0">The first object to format and write.</param> | |
/// <param name="arg1">The second object to format and write.</param> | |
/// <param name="arg2">The third object to format and write.</param> | |
public override void WriteLine(string format, object arg0, object arg1, object arg2) { | |
WriteToLog(format, arg0, arg1, arg2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment