1

I use ELMAH for my Asp.net MVC project exception logging, I've set it up so it log the exceptions to the database, but my problem is if an exception happen and user refresh the page for whatever reason (maybe thinking that by refreshing the page the page might become available) on every refresh, one record is inserted to the database, I'm afraid it could result it very large table (I've capped the ELMAH to only keep the last 90 days, but it still might get too big), I was wondering if there is a way to limit the number of logged exception coming form the same page in some time frame.

Deckard
  • 111
  • 12

1 Answers1

2

Eventually what I've ended up doing was querying the database and if the TimeUtc of the last exception was form less than 5 minutes ago, and its message was the same as currently happening exception, I've Dismissed the log, here's a walkthrough of what I did:

  1. First install This package that add the ELMAH_Errors table and entity for querying the ELMAH_Errors table using EF
  2. Than you can use entity framework for manipulating the ELMAH_Errors table

in Global.asax add this code:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
    {
        using (var context = new ElmahContext())
        {
            var lastErr = context.ELMAH_Errors.OrderByDescending(m => m.TimeUtc).Take(1).SingleOrDefault();

            if (lastErr != null &&
                (e.Exception.Message == lastErr.Message && lastErr.TimeUtc > DateTime.UtcNow.AddMinutes(-5)))
            {
                e.Dismiss();
            }
        }

You can also add this code for ErrorMail_Filtering, also here is another approach, but it do it in such a way that no matter what is the error and where it is originated, it hold the ELMAH form logging for specific time period, my approach is not the best but it still beat getting a record on every refresh.

Community
  • 1
  • 1
Deckard
  • 111
  • 12