Created
November 20, 2013 09:21
-
-
Save kjellski/7560202 to your computer and use it in GitHub Desktop.
Create a commons logging adapter for WPF TextBox
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
using System; | |
using System.Text; | |
using System.Windows; | |
using System.Windows.Controls; | |
using Common.Logging; | |
using Common.Logging.Simple; | |
namespace YourNamespace.Logging | |
{ | |
/// <summary> | |
/// Sends log messages to <see cref="Console.Out" />. | |
/// </summary> | |
/// <author>Gilles Bayon</author> | |
[Serializable] | |
internal class TextBoxLogger : AbstractSimpleLogger | |
{ | |
/// <summary> | |
/// Creates and initializes a logger that writes messages to <see cref="Console.Out" />. | |
/// </summary> | |
/// <param name="logName">The name, usually type name of the calling class, of the logger.</param> | |
/// <param name="logLevel"> | |
/// The current logging threshold. Messages recieved that are beneath this threshold will not be | |
/// logged. | |
/// </param> | |
/// <param name="showLevel">Include the current log level in the log message.</param> | |
/// <param name="showDateTime">Include the current time in the log message.</param> | |
/// <param name="showLogName">Include the instance name in the log message.</param> | |
/// <param name="dateTimeFormat">The date and time format to use in the log message.</param> | |
/// <param name="textBox">TextBox to write the log message to</param> | |
public TextBoxLogger(string logName, LogLevel logLevel, bool showLevel, bool showDateTime, bool showLogName, | |
string dateTimeFormat, TextBox textBox) | |
: base(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat) | |
{ | |
TextBox = textBox; | |
} | |
public TextBox TextBox { get; private set; } | |
/// <summary> | |
/// Do the actual logging by constructing the log message using a <see cref="StringBuilder" /> then | |
/// sending the output to <see cref="Console.Out" />. | |
/// </summary> | |
/// <param name="level">The <see cref="System.LogLevel" /> of the message.</param> | |
/// <param name="message">The log message.</param> | |
/// <param name="e">An optional <see cref="Exception" /> associated with the message.</param> | |
protected override void WriteInternal(LogLevel level, object message, Exception e) | |
{ | |
// Use a StringBuilder for better performance | |
var sb = new StringBuilder(); | |
FormatOutput(sb, level, message, e); | |
Application.Current.Dispatcher.Invoke(() => TextBox.AppendText(sb.ToString())); | |
} | |
} | |
} |
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
using System.Collections.Specialized; | |
using System.Windows.Controls; | |
using Common.Logging; | |
using Common.Logging.Simple; | |
namespace YourNamespace.Logging | |
{ | |
internal class TextBoxLoggerFactoryAdapter : AbstractSimpleLoggerFactoryAdapter | |
{ | |
public TextBoxLoggerFactoryAdapter(TextBox textBox) | |
: base(null) | |
{ | |
TextBox = textBox; | |
} | |
public TextBoxLoggerFactoryAdapter(NameValueCollection properties, TextBox textBox) | |
: base(properties) | |
{ | |
TextBox = textBox; | |
} | |
public TextBoxLoggerFactoryAdapter(LogLevel level, bool showDateTime, bool showLogName, bool showLevel, | |
string dateTimeFormat, TextBox textBox) | |
: base(level, showDateTime, showLogName, showLevel, dateTimeFormat) | |
{ | |
TextBox = textBox; | |
} | |
public TextBox TextBox { get; private set; } | |
protected override ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, | |
bool showLogName, | |
string dateTimeFormat) | |
{ | |
ILog log = new TextBoxLogger(name, level, showLevel, showDateTime, showLogName, dateTimeFormat, TextBox); | |
return log; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment