Class ColoredConsoleAppender
Appends logging events to the console.
public class ColoredConsoleAppender : AppenderSkeleton, IBulkAppender, IAppender, IOptionHandler, IFlushable
- Inheritance
-
ColoredConsoleAppender
- Implements
- Inherited Members
Remarks
ColoredConsoleAppender appends log events to the standard output stream or the error output stream using a layout specified by the user. It also allows the color of a specific type of message to be set.
By default, all output is written to the console's standard output stream. The Target property can be set to direct the output to the error stream.
NOTE: This appender writes directly to the application's attached console
not to the System.Console.Out or System.Console.Error TextWriter.
The System.Console.Out and System.Console.Error streams can be
programmatically redirected (for example NUnit does this to capture program output).
This appender will ignore these redirections because it needs to use Win32
API calls to colorize the output. To respect these redirections the ConsoleAppender
must be used.
When configuring the colored console appender, mapping should be specified to map a logging level to a color. For example:
The Level is the standard log4net logging level and ForeColor and BackColor can be any combination of the following values:
- Blue
- Green
- Red
- White
- Yellow
- Purple
- Cyan
- HighIntensity
Constructors
ColoredConsoleAppender()
Initializes a new instance of the ColoredConsoleAppender class.
public ColoredConsoleAppender()
Remarks
The instance of the ColoredConsoleAppender class is set up to write to the standard output stream.
ColoredConsoleAppender(ILayout)
Initializes a new instance of the ColoredConsoleAppender class with the specified layout.
[Obsolete("Instead use the default constructor and set the Layout property")]
public ColoredConsoleAppender(ILayout layout)
Parameters
layoutILayoutthe layout to use for this appender
Remarks
The instance of the ColoredConsoleAppender class is set up to write to the standard output stream.
ColoredConsoleAppender(ILayout, bool)
Initializes a new instance of the ColoredConsoleAppender class with the specified layout.
[Obsolete("Instead use the default constructor and set the Layout & Target properties")]
public ColoredConsoleAppender(ILayout layout, bool writeToErrorStream)
Parameters
layoutILayoutthe layout to use for this appender
writeToErrorStreamboolflag set to
trueto write to the console error stream
Remarks
When writeToErrorStream is set to true, output is written to
the standard error output stream. Otherwise, output is written to the standard
output stream.
Fields
ConsoleError
The Target to use when writing to the Console standard error output stream.
public const string ConsoleError = "Console.Error"
Field Value
Remarks
The Target to use when writing to the Console standard error output stream.
ConsoleOut
The Target to use when writing to the Console standard output stream.
public const string ConsoleOut = "Console.Out"
Field Value
Remarks
The Target to use when writing to the Console standard output stream.
Properties
RequiresLayout
This appender requires a log4net.Layout to be set.
protected override bool RequiresLayout { get; }
Property Value
- bool
true
Remarks
This appender requires a log4net.Layout to be set.
Target
Target is the value of the console output stream.
This is either "Console.Out" or "Console.Error".
public virtual string Target { get; set; }
Property Value
- string
Target is the value of the console output stream. This is either
"Console.Out"or"Console.Error".
Remarks
Target is the value of the console output stream.
This is either "Console.Out" or "Console.Error".
Methods
ActivateOptions()
Initialize the options for this appender
public override void ActivateOptions()
Remarks
Initialize the level to color mappings set on this appender.
AddMapping(LevelColors)
Add a mapping of level to color - done by the config file
public void AddMapping(ColoredConsoleAppender.LevelColors mapping)
Parameters
mappingColoredConsoleAppender.LevelColorsThe mapping to add
Remarks
Add a ColoredConsoleAppender.LevelColors mapping to this appender. Each mapping defines the foreground and background colors for a level.
Append(LoggingEvent)
This method is called by the AppenderSkeleton.DoAppend(log4net.Core.LoggingEvent) method.
protected override void Append(LoggingEvent loggingEvent)
Parameters
loggingEventLoggingEventThe event to log.
Remarks
Writes the event to the console.
The format of the output will depend on the appender's layout.