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 ?
Description. ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
Build the application, run it in the browser, and navigate to http://www.yoursite.com/elmah.axd. You are prompted to log in before you see the content. After a successful authentication, you see a web page to remotely view the entire log of recorded exceptions.
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.
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 */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With