Elmah log files deletion, manually or is there a setting?

て烟熏妆下的殇ゞ 提交于 2019-12-03 09:57:11

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.

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:

  • 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.

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
*/

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!