Scott on Writing

Musings on technical writing...

Keeping ELMAH's Error Log Size In Check

Error Logging Modules And Handlers (ELMAH) is a free, open source error logging library for ASP.NET applications that provides automated error logging and notification and, unlike health monitoring, offers a built-in log viewer web page along with a host of other nifty features. If you're not using ELMAH or have never heard of it I highly recommend that you check it out. It's the first thing I add to any new ASP.NET project I start.

As with any sort of logging service is is important that ELMAH's log be periodically pruned. If you let ELMAH's log grow unchecked it can reduce performance when querying the log and suck up disk space, which is especially important in hosted environments where there are typically hard disk quote limits for each user on the database server. The good news is that there are a number of techniques you can employ to help ensure that your ELMAH error log stays a reasonable size.

  • Use error filtering. ELMAH offers a rich set of error filtering rules that you can use to instruct ELMAH not to log certain types of errors, which can help keep ELMAH's log size down. I use error filtering to filter out 404 errors. While logging 404 errors can alert you to broken URLs on your site or others, if you've ever managed an Internet-facing website you know that it's not uncommon to receive a deluge of requests from bots searching for security holes.
  • Setup a weekly job in SQL Server. If your website is hosted in a dedicated environment you can (likely) setup a job on SQL Server. In past projects I've used a job that ran once a week on Sunday at 2:00 AM that would delete all ELMAH log entries older than three months.
  • Update the ELMAH_LogError stored procedure to delete old log entries. This approach works well for applications in a shared environment as you do not need permissions to create SQL jobs, but rather just permission to create/alter a stored procedure. The ELMAH_LogError stored procedure is the stored procedure used by the SQL Server provider for logging errors; it inserts a record into the ELMAH_Error table. Update this table by adding a DELETE statement that deletes all log records older than a certain threshold. For example, the following DELETE statement removes all log entries more than 90 days old.

    DELETE FROM [ELMAH_Error]
    WHERE TimeUtc < DATEADD(d, -90, getdate())


    Add the above statement after the INSERT statement in the stored procedure. Doing so will cause the error log to be pruned of entries older than 90 days anytime a new error is logged. (I've never used an ELMAH log provider other than the SQL Server provider, so I'm not certain what stored procedures or queries or other things would need to be changed to implement such log trimming using an alternate provider.)

Keep in mind that trimming the error log brings with it a tradeoff: you are removing error log entries that might be important for analysis later in time. If this is the case, if you think you might need to review that error log from more than 90 days in the past, then before deleting records from the error log you should archive them somewhere.

Happy Programming!

posted on Friday, July 10, 2009 8:52 AM

Feedback

# Daily Links for Saturday, July 11th, 2009 7/11/2009 3:41 AM Pingback/TrackBack

Daily Links for Saturday, July 11th, 2009

# Dew Drop (Weekend Edition) &#8211; July 11-12, 2009 | Alvin Ashcraft's Morning Dew 7/11/2009 5:28 PM Pingback/TrackBack

Dew Drop (Weekend Edition) &#8211; July 11-12, 2009 | Alvin Ashcraft's Morning Dew

# Daily tech links for .net and related technologies - July 9-13, 2009 7/13/2009 12:15 AM Sanjeev Agarwal

Daily tech links for .net and related technologies - July 9-13, 2009 Web Development Using Custom T4

# Deleting All Records In a Table EXCEPT For the N Most Recently Added Records 10/1/2009 8:50 AM Scott on Writing

# Deleting All Records In a Table EXCEPT For the N Most Recently Added Records 10/1/2009 9:16 AM Community Blogs

I recently ran into a situation where I needed to delete all records from a table except for the 1,000

# Deleting All Records In a Table EXCEPT For the N Most Recently Added Records 10/1/2009 9:30 AM BusinessRx Reading List

I recently ran into a situation where I needed to delete all records from a table except for the 1,000

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<December 2009>
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Comment Stats

DayTotal% of Total
Sunday 2046.8%
Monday 42514.1%
Tuesday 51317.1%
Wednesday 55218.4%
Thursday 57919.3%
Friday 54418.1%
Saturday 1876.2%
Total 3004100.0%

Hour1Total% of Total
12:00 AM 762.5%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 682.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1785.9%
8:00 AM 1926.4%
9:00 AM 1575.2%
10:00 AM 1866.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1685.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1063.5%
6:00 PM 1013.4%
7:00 PM 1063.5%
8:00 PM 923.1%
9:00 PM 872.9%
10:00 PM 893.0%
11:00 PM 953.2%
Total 3004100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.94158
Monday 4.80384
Tuesday 4.07476
Wednesday 7.46679
Thursday 6.24674
Friday 5.02457
Saturday 4.76176
Total 5.403004

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06291
10:00 AM 5.61275
11:00 AM 4.17192
12:00 PM 6.14350
1:00 PM 3.17133
2:00 PM 5.00230
3:00 PM 7.62320
4:00 PM 3.96107
5:00 PM 6.00168
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.58163
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403004

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles