I have a project that already uses log4net, but I would like to display this log in HTML. I searched through multiple sites and no one implemented this solution.
Does anyone have any idea how to do this?
I have a project that already uses log4net, but I would like to display this log in HTML. I searched through multiple sites and no one implemented this solution.
Does anyone have any idea how to do this?
Assuming that 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 XmlLayoutBase class as :
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 your 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 is to produce an output of type:
<div><p>Hello world</p></div>