Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Achieving coloured logs using Log4net

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.

like image 278
Vysakh Avatar asked Apr 29 '13 06:04

Vysakh


People also ask

Does log4net support structured logging?

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.

Is log4net dependent on log4j?

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.

What are the main components of log4net briefly describe how loggers work in log4net?

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.


2 Answers

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>

like image 132
aquaraga Avatar answered Oct 22 '22 20:10

aquaraga


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

like image 24
Raghav Avatar answered Oct 22 '22 21:10

Raghav