Shimrod
Shimrod

Reputation: 3205

Log4net - How to know when a file is rolled?

I have a log file created by log4net which changes every hour. Is there a way to know when this roll occurs? Like an event in the log4net lib to which I could bind, and perform an action when this event is fired ? I'm not looking for code samples, just to know if this event exists, and in that case his name.

Thanks in advance !

PS: the time is configurable in my app, so I can't simply use a timer...

Upvotes: 1

Views: 2074

Answers (2)

fqdb
fqdb

Reputation: 5

You need to create a class overriding RollingFileAppender

While most of the methods called on rollover cannot be overridden, one that is virtual is WriteFooterAndCloseWriter()

Make sure you call the base method first in your override, otherwise the file will still be locked.

EventHandler<EventArgs> LogRolledOver;

protected override void WriteFooterAndCloseWriter()
        {
            base.WriteFooterAndCloseWriter();
            LogRolledOver?.Invoke(this, EventArgs.Empty);
        }

Upvotes: 0

weismat
weismat

Reputation: 7411

I guess you are using RollingFileAppender.
In this case you would need to use your own appender inheriting from RollingFileAppender and overwrite RollOverRenameFiles to do your logic plus the original implementation.
RollOverTime would be the time. As these are protected, you need to create your own implementation using inheritance.
See RollingFileAppender documentation for the details.

Upvotes: 4

Related Questions