#region Copyright & License
//
// Copyright 2001-2005 The Apache Software Foundation
//
// 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.
//
#endregion
using System;
using System.Globalization;
using log4net.Layout;
using log4net.Core;
namespace log4net.Appender
{
///
/// Appends logging events to the console.
///
///
///
/// ConsoleAppender appends log events to the standard output stream
/// or the error output stream using a layout specified by the
/// user.
///
///
/// By default, all output is written to the console's standard output stream.
/// The property can be set to direct the output to the
/// error stream.
///
///
/// NOTE: This appender writes each message to the System.Console.Out or
/// System.Console.Error that is set at the time the event is appended.
/// Therefore it is possible to programmatically redirect the output of this appender
/// (for example NUnit does this to capture program output). While this is the desired
/// behavior of this appender it may have security implications in your application.
///
///
/// Nicko Cadell
/// Gert Driesen
public class ConsoleAppender : AppenderSkeleton
{
#region Public Instance Constructors
///
/// Initializes a new instance of the class.
///
///
/// The instance of the class is set up to write
/// to the standard output stream.
///
public ConsoleAppender()
{
}
///
/// Initializes a new instance of the class
/// with the specified layout.
///
/// the layout to use for this appender
///
/// The instance of the class is set up to write
/// to the standard output stream.
///
[Obsolete("Instead use the default constructor and set the Layout property")]
public ConsoleAppender(ILayout layout) : this(layout, false)
{
}
///
/// Initializes a new instance of the class
/// with the specified layout.
///
/// the layout to use for this appender
/// flag set to true to write to the console error stream
///
/// When is set to true, output is written to
/// the standard error output stream. Otherwise, output is written to the standard
/// output stream.
///
[Obsolete("Instead use the default constructor and set the Layout & Target properties")]
public ConsoleAppender(ILayout layout, bool writeToErrorStream)
{
Layout = layout;
m_writeToErrorStream = writeToErrorStream;
}
#endregion Public Instance Constructors
#region Public Instance Properties
///
/// Target is the value of the console output stream.
/// This is either "Console.Out" or "Console.Error".
///
///
/// Target is the value of the console output stream.
/// This is either "Console.Out" or "Console.Error".
///
///
///
/// Target is the value of the console output stream.
/// This is either "Console.Out" or "Console.Error".
///
///
virtual public string Target
{
get { return m_writeToErrorStream ? ConsoleError : ConsoleOut; }
set
{
string v = value.Trim();
if (string.Compare(ConsoleError, v, true, CultureInfo.InvariantCulture) == 0)
{
m_writeToErrorStream = true;
}
else
{
m_writeToErrorStream = false;
}
}
}
#endregion Public Instance Properties
#region Override implementation of AppenderSkeleton
///
/// This method is called by the method.
///
/// The event to log.
///
///
/// Writes the event to the console.
///
///
/// The format of the output will depend on the appender's layout.
///
///
override protected void Append(LoggingEvent loggingEvent)
{
#if NETCF
// Write to the output stream
Console.Write(RenderLoggingEvent(loggingEvent));
#else
if (m_writeToErrorStream)
{
// Write to the error stream
Console.Error.Write(RenderLoggingEvent(loggingEvent));
}
else
{
// Write to the output stream
Console.Write(RenderLoggingEvent(loggingEvent));
}
#endif
}
///
/// This appender requires a to be set.
///
/// true
///
///
/// This appender requires a to be set.
///
///
override protected bool RequiresLayout
{
get { return true; }
}
#endregion Override implementation of AppenderSkeleton
#region Public Static Fields
///
/// The to use when writing to the Console
/// standard output stream.
///
///
///
/// The to use when writing to the Console
/// standard output stream.
///
///
public const string ConsoleOut = "Console.Out";
///
/// The to use when writing to the Console
/// standard error output stream.
///
///
///
/// The to use when writing to the Console
/// standard error output stream.
///
///
public const string ConsoleError = "Console.Error";
#endregion Public Static Fields
#region Private Instances Fields
private bool m_writeToErrorStream = false;
#endregion Private Instances Fields
}
}