Post­Sharp Documentation / Logging / Configuring specific logging frameworks / Logging to the system console

Logging to the system console

This article shows how to target the output of PostSharp Logging to the console and how to configure it.

Configuring PostSharp Logging to use the console

To use target PostSharp Logging to the console:

  1. Add PostSharp logging to your codebase as described in Adding Detailed Logging to your Solution.

  2. In the application startup file, include the following namespace imports:

    C#
    using PostSharp.Patterns.Diagnostics;
    using PostSharp.Patterns.Diagnostics.Backends.Console;

    In the application startup method, include the following code:

    C#
    LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
Theming the console

Selecting a console theme

Console themes define the colors used when rendering the log messages. The ConsoleThemes class exposes a few themes. The default theme is ConsoleThemes.Classic, which copies the colors used by MSBuild. If you do not want any color, use ConsoleThemes.None.

A nicer and more sophisticated theme is Dark.

You can change the current theme by setting the ConsoleLoggingBackendOptions.Theme as demonstrated in the following example:

C#
var backend = new ConsoleLoggingBackend();
backend.Options.Theme = ConsoleThemes.Dark;
LoggingServices.DefaultBackend = backend;

Customizing a theme

The ConsoleTheme class provides a convenient implementation of the low-level IConsoleTheme interface. The dark theme is an instance of the ConsoleTheme.

The ConsoleTheme class simplifies the programming of a theme by allowing only two colors for each log level: a normal color used for the message template, and a highlighted color used for variable parts of the message (typically parameter values). See the methods and properties of the ConsoleTheme class for details.

The easiest way to customize the dark theme is to take a copy of the theme by calling the Clone() method and then configure the new object.

For instance, the following code changes the colors of critical error messages:

C#
ConsoleTheme myTheme = ConsoleThemes.Dark.Clone();
myTheme.NormalStyles[LogLevel.Critical] = new ConsoleThemeStyle("\x1b[38;5;0199m\x1b[49m", ConsoleColor.Magenta );
myTheme.HightlightedStyles[LogLevel.Critical] = new ConsoleThemeStyle("\x1b[38;5;0201m\x1b[48;5;0236m", ConsoleColor.Magenta);              

var backend = new ConsoleLoggingBackend();
backend.Options.Theme = myTheme;
LoggingServices.DefaultBackend = backend;

When the simplifications of the ConsoleTheme class are too restrictive for your needs, you can directly implement the IConsoleTheme or override the GetStyle(String, LogLevel, LogRecordKind, ConsoleThemeItem) method of the ConsoleTheme class.

The following code example displays the message time in a special color:

C#
class CustomTheme : ConsoleTheme
{
    readonly ConsoleThemeStyle timeStyle = new ConsoleThemeStyle(" \x1b[38;5;0006m", ConsoleColor.Cyan);
    readonly ConsoleThemeStyle delimiterStyle = new ConsoleThemeStyle("\x1b[38;5;0255m", ConsoleColor.Gray);

    public CustomTheme() : base(ConsoleThemes.Dark)
    {

    }

    public override ConsoleThemeStyle GetStyle(string role, LogLevel level, LogRecordKind kind, ConsoleThemeItem item)
    {
        if ( item == ConsoleThemeItem.Time )
        {
            return timeStyle;
        }
        else if ( item == ConsoleThemeItem.Delimiter )
        {
            return delimiterStyle;
        }

        return base.GetStyle(role, level, kind, item);

  }
}

To include the message time, you need the following code:

C#
var backend = new ConsoleLoggingBackend();
backend.Options.Theme = new CustomTheme();
backend.Options.IncludeTimestamp = true;
backend.Options.TimestampFormat = "HH:mm:ss";

LoggingServices.DefaultBackend = backend;
See Also