21

How do I delete the log files that Elmah generates on the server?

Is there a setting within Elmah that I can use to delete log files? I would prefer to specify some criteria (e.g. log files that are older than 30 days).

Or should I write my own code for that ?

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
danielovich
  • 9,217
  • 7
  • 26
  • 28

4 Answers4

27

You can set the maximum number of log entries, but there isn't a native function for clearing out logs older than a given date. It's a good feature request, though!

If you are storing your error logs in memory the maximum number stored is 500 by default and this requires no additional configuration. Alternatively, you can define the number using the size keyword:

<elmah>  
  <errorLog type="Elmah.MemoryErrorLog, Elmah" size="50" />  
</elmah>

Setting a fixed size is obviously more important for in-memory or XML-based logging, where resources need to be closely managed. You can define a fixed size for any log type though.

Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155
  • This is certainly an area in Elmah that is completely lacking, although it has been requested numerous times. It shouldn't be that hard to allow deletes of individual log entries as well as a clear all function. Unfortunately, you're on your own for that functionality. – Ed DeGagne Apr 02 '12 at 15:16
  • 9
    It's an open source project. If you have the time and the inclination, fork the code and add the feature. – Phil.Wheeler Apr 02 '12 at 18:10
  • 1
    And risk having to implement or merge changes into your forked code? No thanks brother. This is basic minimal functionality of any logging tool, IMO. It should be included in the project and has been requested since the project came to fruition. – Ed DeGagne Apr 02 '12 at 18:14
  • @Phil.Wheeler - Could you add to this answer how you limit the number of log entries or link to where that is documented? – Ben Anderson Jan 29 '13 at 19:25
  • 7
    @Ed.DeGagne why are you so averse to contributing back to a free tool? You don't have to fork, just contribute a patch back to the maintainers. – Aidan Ryan May 03 '13 at 19:25
  • 2
    does this apply to ELmah when used with Sql server, too? – DevDave Jul 29 '13 at 11:24
  • 2
    Just tried but doesn't seem to work. 29 files and climbing... :( – William T. Mallard Feb 25 '15 at 23:32
  • I'm not sure why, but my max default is 15 – desm May 29 '18 at 19:06
3

This SQL script deletes the rows older than the newest "size" rows:

declare @size INTEGER = 50;
declare @countno INTEGER;
SELECT @countno = count([ErrorId])  FROM [dbo].[ELMAH_Error]
/*
SELECT TOP (@countno-@size)
       [ErrorId]
      ,[TimeUtc]
      ,[Sequence]
  FROM [dbo].[ELMAH_Error]
  order by [Sequence] asc
GO
*/
DELETE FROM [dbo].[ELMAH_Error]
WHERE [ErrorId] IN (
    SELECT t.[ErrorId] FROM (
       SELECT TOP (@countno-@size)
               [ErrorId]
              ,[TimeUtc]
              ,[Sequence]
          FROM [dbo].[ELMAH_Error]
          order by [Sequence] asc
      ) t
    )
GO


/*
-- Print remaining rows
SELECT * FROM [dbo].[ELMAH_Error]
order by [Sequence] desc
*/
RaSor
  • 869
  • 10
  • 11
0

At least within a MVC project, you can go to the App_Data/Elmah.Errors directory to delete the error XML files that you want to remove.

J Hunt
  • 733
  • 1
  • 10
  • 21
0

In response to what Phil Wheeler mentioned above, on a current project I am doing in Mvc4, we have an Mvc area called SiteAdmin. This area will be responsible for all site administration duties, including Elmah.

To get over the lack of delete functionality, I implemented a function to delete all of the current log entries in Elmah (we're using the XML based version).

Here is an image of the SiteAdmin index view:

enter image description here

  • View Error Log - Opens the Elmah UI in a new window.
  • Clear Error Log - Presents a confirmation popup, then deletes all entries if the user confirms.

If anyone needs the code as an example, I'd be happy to send it along.

My mechanics could be modified pretty easily to provide a mechanism for selective deletions by criteria if needed (by date, by status code, etc...).

The point of my answer here is that you could provide the delete functionality on your own AND not fork the open source code of the Elmah project.

Ed DeGagne
  • 3,250
  • 1
  • 30
  • 46