Tracks
/
C#
C#
/
Exercises
/
Log Levels
Log Levels

Log Levels

Learning Exercise

Introduction

Strings

A string in C# is an object that represents immutable text as a sequence of Unicode characters (letters, digits, punctuation, etc.). Double quotes are used to define a string instance:

string fruit = "Apple";

Methods

Strings are manipulated by calling the string's built-in methods. For example, to remove the whitespace from a string, you can use the Trim method like this:

string name = " Jane "
name.Trim()
// => "Jane"

Once a string has been constructed, its value can never change. Any methods that appear to modify a string will actually return a new string.

Concatenation

Multiple strings can be concatenated (added) together. The simplest way to achieve this is by using the + operator.

string name = "Jane";
"Hello " + name + "!";
// => "Hello Jane!"

Formatting

For any string formatting more complex than simple concatenation, string interpolation is preferred. To use interpolation in a string, prefix it with the dollar ($) symbol. Then you can use curly braces ({}) to access any variables inside your string.

string name = "Jane";
$"Hello {name}!";
// => "Hello Jane!"

Instructions

In this exercise you'll be processing log-lines.

Each log line is a string formatted as follows: "[<LEVEL>]: <MESSAGE>".

There are three different log levels:

  • INFO
  • WARNING
  • ERROR

You have three tasks, each of which will take a log line and ask you to do something with it.

1. Get message from a log line

Implement the (static) LogLine.Message() method to return a log line's message:

LogLine.Message("[ERROR]: Invalid operation")
// => "Invalid operation"

Any leading or trailing white space should be removed:

LogLine.Message("[WARNING]:  Disk almost full\r\n")
// => "Disk almost full"

2. Get log level from a log line

Implement the (static) LogLine.LogLevel() method to return a log line's log level, which should be returned in lowercase:

LogLine.LogLevel("[ERROR]: Invalid operation")
// => "error"

3. Reformat a log line

Implement the (static) LogLine.Reformat() method that reformats the log line, putting the message first and the log level after it in parentheses:

LogLine.Reformat("[INFO]: Operation completed")
// => "Operation completed (info)"
Edit via GitHub The link opens in a new window or tab
C# Exercism

Ready to start Log Levels?

Sign up to Exercism to learn and master C# with 62 concepts, 167 exercises, and real human mentoring, all for free.