You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by "Venkatasamy, Vanitha" <Va...@xpandcorp.com> on 2011/11/15 16:53:56 UTC

How to initialize Custom appender in log4net

Hi All,
   I am using custom appender to send an email if 20 errors are logged in Event Viewer within 5 minutes. I wrote the custom appender and placed the .cs file in the App_Code folder. The project is compiled and build successfully . But nothing is happening when the error is occurred in the application side. Please see the config settings and the custom appender. I think I am missing some config setting or assembly reference.

Config

      <configSections>
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
            <section name="MultiThresholdNotifyingAppender" type="Log4NetExtensions.MultiThresholdNotifyingAppender, MultiThresholdNotifyingAppender"/>
      </configSections>
       <log4net debug="true">
            <appender name="MultiThresholdNotifyingAppender" type="Log4NetExtensions.MultiThresholdNotifyingAppender,MultiThresholdNotifyingAppender">
                  <!--<evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="Error"/>
      </evaluator>
      <Level value="Error"/>
      <TimeSpan  value="00:05:00"/>-->
                  <!--<evaluator type="log4net.Core.LevelEvaluator,log4net">-->
      <LevelThreshold  value="ERROR"/>
                  <!--</evaluator>-->
                  <appender-ref ref="EventLogAppender"/>
            </appender>
            <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
                  <param name="LogName" value="PortalWebLogs"/>
                  <param name="ApplicationName" value="Portals"/>
                  <filter type="log4net.Filter.LevelRangeFilter">
                        <acceptOnMatch value="true"/>
                        <levelMin value="INFO"/>
                        <levelMax value="FATAL"/>
                  </filter>
                  <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
                  </layout>
            </appender>
            <root>
                  <level value="ALL"/>
                  <priority value="ERROR"/>
                  <appender-ref ref="MultiThresholdNotifyingAppender"/>
                  <!--<appender-ref ref="EventLogAppender"/>-->
            </root>
      </log4net>


MultiThresholdNotifyingAppender.cs in App_code folder

using System;
using System.Collections.Generic;
using System.Web;
using log4net;

using log4net.Core;
using System.Threading;
using log4net.Appender;
using log4net.Config;
using System.Web.Mail;
using System.Windows.Forms;


/// <summary>
/// Summary description for Class1
/// </summary>



namespace Log4NetExtensions
{
    public class MultiThresholdNotifyingAppender : AppenderSkeleton
    {
        public MultiThresholdNotifyingAppender()
        {
        }
        private Queue<LoggingEvent> loggingEventQueue = new Queue<LoggingEvent>();


        private DateTime windowStart = DateTime.Now;


        Level _levelthreshold;
        TimeSpan _windowLength = TimeSpan.FromMinutes(1);//its 5 min but 1 min is to test
        int _hitThreshold = 1;//its 20 but 1 is to test the code

        protected override void Append(LoggingEvent loggingEvent)
        {
            sendEmail(); //.to test whether is function is being called..But no its not fired.

            if (loggingEvent.Level < LevelThreshold)
            {

                if (loggingEventQueue.Count == 0)
                    return;
                if ((loggingEvent.TimeStamp - windowStart) >= WindowLength)
                {
                    while (loggingEventQueue.Count > 0 && loggingEventQueue.Peek().TimeStamp - windowStart >= WindowLength)
                    {
                        loggingEventQueue.Dequeue();
                    }
                    windowStart = loggingEventQueue.Peek().TimeStamp;
                    return;
                }
            }
            loggingEventQueue.Enqueue(loggingEvent);
            //If this is the first error in the queue, start the time window.
            if (loggingEventQueue.Count == 1)
                windowStart = loggingEvent.TimeStamp;
            //Too few messages to qualify for notification.
            if (loggingEventQueue.Count < HitThreshold - 1)
                return;
            if (loggingEvent.TimeStamp - windowStart >= WindowLength)
            {

                sendEmail();
            }
            //After sending the message, clear the LoggingEvents and reset the time window.
            loggingEventQueue.Clear();
            windowStart = loggingEvent.TimeStamp;
        }
        public void sendEmail()
        {
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.To = "cc@cc.com";
            myMail.From = "xx@xx.com";
            myMail.Subject = "Error";
            myMail.Body = "Hello World";
            SmtpMail.SmtpServer = "xx.x.xx.xx";

            SmtpMail.Send(myMail);

        }
        public Level LevelThreshold
        {
            get
            {
                return _levelthreshold;
            }
            set
            {
                _levelthreshold = value;
            }
        }


        public TimeSpan WindowLength
        {
            get
            {
                return _windowLength;
            }
            set
            {
                _windowLength = value;
            }
        }



        public int HitThreshold
        {
            get
            {
                return _hitThreshold;
            }
            set
            {
                _hitThreshold = value;
            }
        }
        public void Append(LoggingEvent[] loggingEvents)
        {
            foreach (LoggingEvent le in loggingEvents)
            {
                Append(le);
            }
        }
    }

}


Thanks,
Vanitha

________________________________

This message contains Devin Group confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail.
Please notify the sender immediately by e-mail if you have received this e-mail in error and delete this e-mail from your system. E-mail transmissions cannot be guaranteed secure, error-free and information could be intercepted, corrupted, lost, destroyed, arrive late, incomplete, or contain viruses. The sender therefore does not accept liability for errors or omissions in the contents of this message which may arise as result of transmission. If verification is required please request hard-copy version.