How to create an html log with log4net

Asked

Viewed 347 times

8

I have a project that already uses log4net, but I would like to show this log in HTML. I searched several sites and nobody implemented this solution.

Does anyone have any idea how to do that?

1 answer

3


Assuming you can save the log as XML and that HTML is also a markup language, you can try to take advantage of it.

You can rewrite the class Xmllayoutbase, as it was done by Darin:

public class MeuHtmlLayout : XmlLayoutBase
{
    protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
    {
        writer.WriteStartElement("div");
        writer.WriteStartElement("p");
        writer.WriteString(loggingEvent.RenderedMessage);
        writer.WriteEndElement();
        writer.WriteEndElement();
    }
}

In his app config.:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="log.html" /> <!-- nunca testei com .html, qualquer coisa volta .txt -->
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="SeuNamespace.MeuHtmlLayout" />
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>
</configuration>

To test:

class Program
{
    static void Main(string[] args)
    {
        XmlConfigurator.Configure();
        ILog log = log4net.LogManager.GetLogger(typeof(Program));
        log.Debug("Hello world!");
    }
}

It’s to produce a exit of the kind:

<div><p>Hello world</p></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.