Dapfor Logger and log4net

Pinned Featured

Comments

1 comment

  • Dapfor Team

    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:

    1. Сreate a new custom class library, or use existing one.
    2. Add log4net as new references to the new library
    3. 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
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk