tags:

views:

23

answers:

2

I am using log4net to log to a sql table. I'd like to be able to either only keep the most recent n days, or the most recent n entries in the table. Is this possible with log4net?

+1  A: 

Log4net do not have this capability built-in. But such a task is probably best placed as a job ,e.g. in SSIS (if you're running MS SQL Server) or similar tools.

Peter Lillevold
A: 

I figured it out, for the commandText for the AdoNetAppender I set the command text to:

<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception); DELETE FROM [Log] WHERE [Date] &lt; DATEADD(dd, -28, GETDATE())" />

It feels hacky, but it works. I'll post here if I find a neater solution.

ilivewithian
lol, thought about that but figured I wouldn't advise it. Since it will put more load into the logging process, I would do the deletion out-of-band in a separate process.
Peter Lillevold
As a general principle I think you're right, but since our logging is mostly going to be fairly low levels I think it's an acceptable overhead. If I'm wrong we can pull it out and use your suggestion.
ilivewithian