Implement log output for console applications, supporting custom text/background colors per log level, which delivers great visibility during debugging.
- Create a new Console Application in Visual Studio
- Add the
App.configconfiguration file to your project - Install log4net (via NuGet / reference log4net.dll)
- Write log4net settings inside
App.config - Initialize log4net in code and print logs
You can set foreground and background colors for different log levels in console logs to distinguish log types.
App.config Configuration
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<!-- Colored console output appender ColoredConsoleAppender -->
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<!-- ERROR level color scheme -->
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<!-- DEBUG level color scheme -->
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<!-- INFO level color scheme -->
<mapping>
<level value="Info" />
<backColor value="Yellow" />
</mapping>
<!-- Add custom mapping colors for WARN, FATAL as needed -->
<!-- Log output format template -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Root logger, enable console output -->
<root>
<level value="All" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
</log4net>
</configuration>Code language: HTML, XML (xml)
Format Placeholder Explanation
%date: Log timestamp[%thread]: Thread ID%-5level: Log level (aligned with 5‑character width)%logger: Class name of the logger%message: Log content body%newline: Automatic line break
Program.cs
using System;
using log4net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Load and initialize log4net configuration
log4net.Config.XmlConfigurator.Configure();
// Get logger instance, pass in current class type for easier log source tracing
ILog logger = LogManager.GetLogger(typeof(Program));
// Output logs of all severity levels
logger.Info("General log");
logger.Error("Error log");
logger.Debug("Debug message");
logger.Fatal("Fatal error");
logger.Warn("Warning message");
Console.ReadKey();
}
}
}Code language: JavaScript (javascript)
Running Result
Different log levels render distinct background colors automatically in console:
- INFO: Yellow background
- DEBUG: Green background
- ERROR / FATAL: Bright red background
- WARN: Yellow background
Sample output:
2017-03-21 20:35:31,550 [9] INFO ConsoleApplication1.Program [(null)] - General log
2017-03-21 20:35:31,592 [9] ERROR ConsoleApplication1.Program [(null)] - Error log
2017-03-21 20:35:31,592 [9] DEBUG ConsoleApplication1.Program [(null)] - Debug message
2017-03-21 20:35:31,593 [9] FATAL ConsoleApplication1.Program [(null)] - Fatal error
2017-03-21 20:35:31,594 [9] WARN ConsoleApplication1.Program [(null)] - Warning messageCode language: CSS (css)

Console log output