Please feel free to include this code in the next build. (By the way, when will that be?)
Tom Padilla
#region Copyright // // This framework is based on log4j see http://jakarta.apache.org/log4j // Copyright (C) The Apache Software Foundation. All rights reserved. // // This software is published under the terms of the Apache Software // License version 1.1, a copy of which has been included with this // distribution in the LICENSE.txt file. // // Extended by Tom Padilla // #endregion
using System; using System.Globalization; using System.Windows.Forms;
using log4net.Layout; using log4net.spi;
namespace log4net.Appender
{
/// <summary>
/// Appends logging events to the console.
/// </summary>
/// <remarks>
/// <para>
/// MessageBoxAppender sends log events to the standard MessageBox output stream
/// using a layout specified by the user. This adds support for [title] and [text]
/// labels for formatting the MessageBox.
/// </para>
/// </remarks>
public class MessageBoxAppender : AppenderSkeleton
{
#region Public Instance Constructors
/// <summary>
/// Initializes a new instance of the <see cref="MessageBoxAppender" /> class.
/// </summary>
/// <remarks>
/// The instance of the <see cref="MessageBoxAppender" /> class is set up to write
/// to the standard MessageBox output stream.
/// </remarks>
public MessageBoxAppender()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MessageBoxAppender" /> class
/// with the specified layout.
/// </summary>
/// <param name="layout">the layout to use for this appender</param>
/// <remarks>
/// The instance of the <see cref="MessageBoxAppender" /> class is set up to write
/// to the standard MessageBox output stream.
/// </remarks>
public MessageBoxAppender(ILayout layout)
{
Layout = layout;
}
#endregion Public Instance Constructors
#region Public Instance Properties
//
// None
//
#endregion Public Instance Properties#region Override implementation of AppenderSkeleton
/// <summary>
/// This method determines if there is a sense in attempting to
append.
/// </summary>
/// <remarks>
/// <para>
/// This method checked if a layout has been set.
/// </para>
/// </remarks>
/// <returns><c>false</c> if any of the preconditions
fail.</returns>
override protected bool PreAppendCheck()
{
if (!base.PreAppendCheck())
{
return false;
} return true;
}/// <summary>
/// This method is called by the <see cref="AppenderSkeleton.DoAppend"/> method.
/// </summary>
/// <param name="loggingEvent">The event to log.</param>
/// <remarks>
/// <para>
/// Writes the event to a MessageBox. Uses [title] and [text] labels to format the message.
/// Will default to Application.ProductName + " " + Application.ProductVersion.
/// </para>
/// <para>
/// The format of the output will depend on the appender's layout.
/// </para>
/// </remarks>
override protected void Append(LoggingEvent loggingEvent)
{
string strEventString;
string strTitleString;
string strTextString;
int iTitleIndex;
int iTextIndex;
strEventString = RenderLoggingEvent(loggingEvent);
iTitleIndex = strEventString.IndexOf("[title]");
iTextIndex = strEventString.IndexOf("[text]");if ((iTitleIndex == -1) || (iTextIndex == -1))
{
MessageBox.Show(strEventString, Application.ProductName + " " + Application.ProductVersion );
return;
}
if (iTitleIndex < iTextIndex)
{
strTitleString = strEventString.Substring(iTitleIndex + 7, iTextIndex - 7);
strTextString = strEventString.Substring(iTextIndex + 6, strEventString.Length - (iTextIndex + 6));
}
else
{
strTitleString = strEventString.Substring(iTitleIndex + 7, strEventString.Length - (iTitleIndex + 7));
strTextString = strEventString.Substring(iTextIndex + 6, iTitleIndex);
}
MessageBox.Show(strTextString, strTitleString);
} /// <summary>
/// This appender requires a <see cref="Layout"/> to be set.
/// </summary>
/// <value><c>true</c></value>
override protected bool RequiresLayout
{
get { return true; }
}#endregion Override implementation of AppenderSkeleton
#region Public Static Fields
//
// None
//
#endregion Public Static Fields #region Private Instances Fields
//
// None
//
#endregion Private Instances Fields
}
}