log4net custom appender not working

8k Views Asked by At

i have create a custom appender using log4net for my project. i created the SampleLogAppender class using AppenderSkeleton interface.

namespace Sample.Integrations.Utilities {

public class SampleLogAppender : AppenderSkeleton
{
    public static IContext Context { get; set; }

    protected override void Append(log4net.Core.LoggingEvent loggingEvent)
    {
        if (Context != null)
        {
            Context.LogMessage(RenderLoggingEvent( loggingEvent ));
        }
    }


    protected override bool RequiresLayout
    {
        get { return true; }
    }
}

}

if i configure this class in config file, it is not working. below configuration i am using.

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

   <log4net>
     <appender name="LogAppender" type="Sample.Integrations.Utilities.SampleLogAppender ">
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="[%date{MM/dd/yyyy hh:mm:ss tt}] [%-5level] [%class]-[%method] - [%message] %newline" />
         </layout>
      </appender>
       <logger name="LogRoot">
         <level value="ALL" />
          <appender-ref ref="LogAppender" />
      </logger>
  </log4net>

</configuration>

above config file i am using, but it is not working. below i am using the log4net configuration.

 log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(ConfigLocation));

         Log = log4net.LogManager.GetLogger("LogRoot");

i am having Logger class also.

 public class Logger
{

    public static ILog Log;

    public static string ConfigLocation = string.Empty;

    private static readonly string ConversionPattern = "[%date{MM/dd/yyyy hh:mm:ss tt}] [%-5level] [%class]-[%method] - [%message] %newline";



    public static void ConfigureLog(IContext context)
    {

        SampleLogAppender .Context = context;

        log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(ConfigLocation));

        Log = log4net.LogManager.GetLogger("LogRoot");
    } }

i initialize Logger like below in startup project.

Logger.ConfigureLog(_context);

i use the logger like below.

Logger.Log.Debug("Test");

when i call this , the Append() method doesn't call.

please give some solutions.

1

There are 1 best solutions below

0
On

I have tried working with your configuration file and after a few fixes it worked, see the log4net section:

<log4net>
  <root>
    <level value="TRACE" />
    <appender-ref ref="LogAppender" />
  </root>
  <appender name="LogAppender" type="Sample.Integrations.Utilities.SampleLogAppender">
   <layout type="log4net.Layout.PatternLayout">
   <conversionPattern value="[%date{MM/dd/yyyy hh:mm:ss tt}] [%-5level] [%class]-[%method] -                      [%message] %newline" />
   </layout>
  </appender>
</log4net>

Something strange that was in your code and didn't work was a space in the type, in the end before the quotation mark.

<appender name="LogAppender" type="Sample.Integrations.Utilities.SampleLogAppender ">