Dapfor Logger and log4net
Pinned FeaturedHas someone found a way to automatically integrate the Dapfor Logger with the log4net package?
Our application already uses log4net extensively. It would be great if all that logging could be automatically redirected to the Dapfor logger.
-
Hello Phillip,
It's quite simple. It is only necessary to add a custom appender to the project and redirect calls to the dapfor logging system:
- Сreate a new custom class library, or use existing one.
- Add log4net as new references to the new library
-
Create a new Appeder and inherit it from log4net.Appender.AppenderSkeleton
namespace YourNamespace
{
public class DapforAppender : log4net.Appender.AppenderSkeleton
{
protected override void Append(LoggingEvent loggingEvent)
{
if(Equals(loggingEvent.Level, Level.Info))
{
Logger.Info(loggingEvent.RenderedMessage);
}
else if (Equals(loggingEvent.Level, Level.Fatal))
{
Logger.Fatal(loggingEvent.RenderedMessage);
}
else if (Equals(loggingEvent.Level, Level.Error))
{
Logger.Error(loggingEvent.RenderedMessage);
}
else if (Equals(loggingEvent.Level, Level.Debug))
{
Logger.Debug(loggingEvent.RenderedMessage);
}
}
}
}4. In the application, create a new config file and call it something like Log.config, and set it up so that log4net knows where/how to instantiate your new appender:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<!-- Appenders -->
<appender name="DapforAppender" type="YourNamespace.DapforAppender" />
<!-- Logger hierarchy -->
<root>
<appender-ref ref="DapforAppender" />
</root>
</log4net>5. Initialize the log4net in Program.Main() method as following:
FileInfo fi = new FileInfo("Log.config");
if(fi.Exists)
{
XmlConfigurator.ConfigureAndWatch(fi);
}6. Put Dapfor.Net.Diagnostics.LogViewer control somewhere you want.
Best regards,
The Dapfor Team
0
Please sign in to leave a comment.
Comments
1 comment