Last active
March 7, 2023 07:32
-
-
Save mizanRahman/4484020 to your computer and use it in GitHub Desktop.
Logging with log4net. Colored Console Logging and File Logging demonstrated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
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
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
Logging.cs

Any help would be appreciated!