Now Iam getting error "failed to find configuration section 'log4net' in the application's .config
file". This is my code in log4net.config
file:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<root>
<level Value="Info"/>
<level Value="Error"/>
<appender-ref ref="ColoredFileAppender" />
</root>
<appender name="ColoredFileAppender" type="log4net.Appender.RollingFileAppender" >
<file type="log4net.Util.PatternString"/>
<file value="C:\CL2.html" />
<encoding value="utf-8" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%CC" />
<converter>
<name value="CC" />
<type value="Cluster2.ColoredMessageConverter" />
</converter>
<mapping>
<level value="Info" />
<foreColor value="Green" />
</mapping>
<mapping>
<level value="ERROR" />
<backColor value="Red, HighIntensity" />
</mapping>
</layout>
</appender>
</configuration>
</log4net>
I have included this statement in the assembly:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
I have included this statement in my program:
protected void Application_start(Object Sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
I'm having no idea of what is the issue. Please help.
Contents. The log4net is a logging framework for . NET based on Apache log4j. It supports multiple logging targets, structured output, logging hierarchy, and everything a modern logging framework should support.
Log4net is a logging utility for . NET applications. It's based on log4j, which is for Java applications. Log4net is highly configurable, so you can use it in many scenarios.
Log4net has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.
You have to use custom pattern layout to achieve this, and then wire it up in your log4net.config file. Your code would look like:
public class ColoredMessageConverter : PatternLayoutConverter
{
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
string color = "";
switch (loggingEvent.Level.Name)
{
case "DEBUG":
color = "green";
break;
case "WARN":
case "INFO":
color = "white";
break;
case "ERROR":
color = "pink";
break;
case "FATAL":
color = "red";
break;
}
string logToRender = string.Format(" <p style='color:{0}'>{1}</p>", color, loggingEvent.RenderedMessage);
//Add logToRender to file
writer.Write(logToRender);
}
}
In your log4net.config, wire this converter as given below. Replace the 'Your namespace' with the correct one.
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="ColoredFileAppender" />
</root>
<appender name="ColoredFileAppender" type="log4net.Appender.RollingFileAppender" >
<file type="log4net.Util.PatternString" value="c:\test.html" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%CC" />
<converter>
<name value="CC" />
<type value="<<Your namespace>>.ColoredMessageConverter" />
</converter>
</layout>
</appender>
Adding to what above is suggested, here is the code to add date/level/method along with that information
public class LoggerColoredMessageConverter : PatternLayoutConverter
{
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
string color = "";
switch (loggingEvent.Level.Name)
{
case "DEBUG":
color = "#383d41";
break;
case "WARN":
color = "#856404";
break;
case "INFO":
color = "#0c5460;";
break;
case "ERROR":
case "FATAL":
color = "#721c24";
break;
}
string logToRender = string.Format(" <p style='color:{0}'>{1} {2} {3}" +
" - {4}</p>", color, loggingEvent.TimeStamp, loggingEvent.Level.Name,
loggingEvent.LoggerName,
loggingEvent.RenderedMessage);
//Add logToRender to file
writer.Write(logToRender);
}
}
to log it like this:
11/15/2019 1:57:16 PM ERROR ASP.demo_aspx - My Error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With