How do I only keep the most recent n entries in a log4net sql table?
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?
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.
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] < DATEADD(dd, -28, GETDATE())" />
It feels hacky, but it works. I'll post here if I find a neater solution.
I know I'm late to the party... but looking at ilivewithian's solution I would agree with Peter Lillevold's observation that having it cause additional load in the logging process is undesirable.
Wouldn't it also be possible to use a trigger in the database to auto-delete the older items? Sure, you would need a DB that supports triggers, but it seems like most modern ones (including open source ones like SQLite and PostgreSQL) do.
精彩评论