3

What would be a simple way to effectively limit the size of the table Log4Net logs in to?

Something like deleting old records, or deleting when the total number of records reaches a predefined limit, or when the table (or DB) size reaches a size limit

(edit) since we have customers with both types of tagged DB (MySQL / MSSQL) a single point solution would be better from a maintenance POV.

we thought about using some code (with NHibernate) to periodically do what @samy suggested. but a performance-effective Log4Net solution is always better.

Pingi
  • 342
  • 3
  • 11

1 Answers1

4

I'm going to assume you want to do it entirely from log4net; if you don't, then either

  • use a cron task that will delete data older than x days at your desired interval
  • use a trigger that can react to the insertion of new data by log4net

These methods would be much cleaner than the following.

Since the AdoNetAppender lets you specify the command text you could set up a second AdoNetAppender that would be triggered by the logging event along with your original appender. This second appender could then delete the data you don't want any more:

CommandText="DELETE FROM Logs WHERE [date] < DATEADD(Hour,
-6, GETDATE())"

I think the logging framework should not be handling database maintenance so please consider letting MySQL do the work instead.

samy
  • 14,832
  • 2
  • 54
  • 82
  • Thanks @samy, i was indeed hoping for a Log4Net solution, preferably something already implemented in Log4Net itself or an extension to add to its appender.. question is edited. – Pingi Feb 02 '15 at 12:26
  • 1
    @Pingi then you can use the CommandText property as pointed out. It is true that I wrapped the proposed solution between disclaimers :)but you can call a delete from the appender. If you need to hit both databases, set two appenders that will delete a db each – samy Feb 02 '15 at 13:02
  • the down side of letting the DB handle its maintenance is having to maintain 2 seperate scripts in 2 seperate environments, which is also harder to maintain in CVS – Pingi Feb 02 '15 at 16:26
  • In a way setting up maintenance in log4net also needs as many requests, it's just that they are in the configuration file. Anyway, did you set up the appenders in log4net? Does it work as intended? – samy Feb 03 '15 at 07:47