NLog: highlighting words with regex

NLog is a simple little logging framework for .NET that I’ve come to prefer as my logging component in many situations.
However, I ran into a situation using it that I´d like to share with you since I didn’t find any great resources on the Internet about it (yet).

note: the complete code is up on my github repo

Problem

Let’s imagine that we’re building a little program that writes out a message to the console every time it executes a task.

        private static void ExecuteTask(string task, int seed, int total)
        {
            m_Logger.Info(" - now running deployment task {0}/{1} with name='{2}'", seed, total, task);
        }

To make things look a bit nicer and to provide some better visibility we’d like some parts of the log message to be in a certain colors.

To accomplish that you could use one of the targets that NLog provides, the colored console target.
Using that target we could configure that an information message should be in green foreground color and an error should be red.

            var configuration = new LoggingConfiguration();

            var target = new ColoredConsoleTarget();
            configuration.AddTarget("console", target);

            target.Layout = "${message}";

            target.RowHighlightingRules.Add(
                new ConsoleRowHighlightingRule
                {
                    Condition = "level == LogLevel.Info",
                    ForegroundColor = ConsoleOutputColor.Green
                });
            target.RowHighlightingRules.Add(
                new ConsoleRowHighlightingRule
                {
                    Condition = "level == LogLevel.Error",
                    ForegroundColor = ConsoleOutputColor.Red
                });

            LoggingRule rule1 = new LoggingRule("*", LogLevel.Debug, target);
            configuration.LoggingRules.Add(rule1);

            LogManager.Configuration = configuration;

That was pretty easy…right?!

…and here’s how that would look once it executes:

image

Now, we’d like name=’backup’ (which could also say name=’updating’ or something like that) to show up in yellow instead to provide even better visibility.

How to accomplish that?

Solution

Fortunately NLog supports highlighting certain words using the ConsoleWordHighlightingRule class. With the help of that class and its ability for regular expressions we can easily add the following to the logging configuration code:

            target.WordHighlightingRules.Add(
                new ConsoleWordHighlightingRule
                {
                    Regex = "name='[^']*'",
                    ForegroundColor = ConsoleOutputColor.Yellow
                });

And with that:

image

….now, ain’t  that pretty?!

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: