none
Error logging with time interval inside Thread RRS feed

  • Question

  •  

    Hi,

     

    I have existing code as below.

     

    A thread is calling a method which in turn calls some other inner methods.

    Whenever the error occurs in inner most method, error is logged in the catch block and throw the same. 

    The log is happening at 3 levels atleast.

     

    The frequency of thread is every milli second.

    Due to this, if any error occurs, the log table is flooded by messages.

     

    Instaed of logging the error on every millisecond, need to log every 5 mintues (say any given time interval).

     

    Please suggest me how to achieve this with out chnaging the existing functionality.

     

    Thanks,

    Karhtik

    Tuesday, April 15, 2008 11:12 AM

Answers

  • Try something like this:

        private const int MinTicksBetweenExceptions = 5 * 60 * 1000;
        private int lastExceptionTick;
        private System.Timers.Timer scheduler;

        private void startWorker() {
          lastExceptionTick = Environment.TickCount - MinTicksBetweenExceptions;
          // Do something repeatedly
          scheduler = new System.Timers.Timer(42.0);
          scheduler.Elapsed += worker;
        }

        private void worker(object sender, System.Timers.ElapsedEventArgs e) {
          try {
            dosomething();
          }
          catch (Exception ex) {
            int diff = Environment.TickCount - lastExceptionTick;
            if (diff >= MinTicksBetweenExceptions) {
              // Log the exception
              //...
              lastExceptionTick = Environment.TickCount;
            }
          }
        }
    Wednesday, April 16, 2008 1:21 AM
    Moderator

All replies

  • The problem here is that you don't necessarily know if the next time you see an error if it was caused by the same exception  or if it was logged already.  Is it desired functionality to log it at every level?  If so then by preventing it from being logged at every level for 5 minutes will be changing the existing functionality. 

    I would suggest that since you are catching the exception, logging it, and then re-throwing it, that you not do that in all of the different levels. Instead just have a try catch at the top most level where you can get the full stack track and log it once.  You will reduce the amount of times any one specific error is logged to once and it will be much cleaner and more intuitive. 
    Tuesday, April 15, 2008 6:13 PM
  • Try something like this:

        private const int MinTicksBetweenExceptions = 5 * 60 * 1000;
        private int lastExceptionTick;
        private System.Timers.Timer scheduler;

        private void startWorker() {
          lastExceptionTick = Environment.TickCount - MinTicksBetweenExceptions;
          // Do something repeatedly
          scheduler = new System.Timers.Timer(42.0);
          scheduler.Elapsed += worker;
        }

        private void worker(object sender, System.Timers.ElapsedEventArgs e) {
          try {
            dosomething();
          }
          catch (Exception ex) {
            int diff = Environment.TickCount - lastExceptionTick;
            if (diff >= MinTicksBetweenExceptions) {
              // Log the exception
              //...
              lastExceptionTick = Environment.TickCount;
            }
          }
        }
    Wednesday, April 16, 2008 1:21 AM
    Moderator