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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it would be nice to have WARN colored too