Skip to content

Instantly share code, notes, and snippets.

@mizanRahman
Last active March 7, 2023 07:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mizanRahman/4484020 to your computer and use it in GitHub Desktop.
Save mizanRahman/4484020 to your computer and use it in GitHub Desktop.
Logging with log4net. Colored Console Logging and File Logging demonstrated.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<!--both colored-console-logging and file-logging is enabled-->
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
<!--log to file-->
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="F:\\log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
<!--colored log on console-->
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="INFO" />
<forecolor value="Green" />
</mapping>
<mapping>
<level value="ERROR" />
<forecolor value="Red" />
</mapping>
<mapping>
<level value="DEBUG" />
<forecolor value="Yellow" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%date [%thread] %-5level - %message%newline" />
</layout>
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
namespace LogForNetTest
{
class Program
{
protected static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
XmlConfigurator.Configure();
#if DEBUG
log.Debug("Hello from debug"); //put this log while running in debug mode only
#endif
log.Info("This is just to inform you");
log.Warn("Something you should consider. better check this out!!!");
log.Error("oops!!something wrong");
log.Fatal("you are dead, man!!!");
Console.ReadLine();
}
}
}
@mizanRahman
Copy link
Author

@prasanpro
Copy link

prasanpro commented Apr 22, 2019

When I modify my configuration, I get following error: ColoredConsoleAppender not found. I found that this appender is not available for dotnet core application.
I wanted to log error parts of my application as bold. I can't find any solution for formatting option in log4net.

@szeplaszloit
Copy link

Try log4net.Appender.ManagedColoredConsoleAppender

@gurdeepsinghAVB
Copy link

Hi,
I am using Log4net for logging my selenium C# tests. As I was able to print my Test Explorer logs in regular text but instead I want to print colorful output for different kind of log messages. So I have tried your above solution with log4net.Appender.ManagedColoredConsoleAppender but for some reason it doesn't show colorful output in Test Exlorer's output results window . Details are below:
my Log4net.config file
image

Logging.cs
image

Any help would be appreciated!

@oleksabor
Copy link

it would be nice to have WARN colored too

      <mapping>
        <level value="WARN" />
        <foreColor value="Yellow, HighIntensity" />
      </mapping>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment