问题
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 ?
回答1:
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.
回答2:
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
*/
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/4479461/elmah-log-files-deletion-manually-or-is-there-a-setting