8

I am working with log4net. I wants to add a button in my GUI that when the user click on that button, the log file will cleared. How can I do that?

Thanks alot

Amir Brand
  • 313
  • 1
  • 3
  • 14

5 Answers5

10

I had this issue, too.

You need this in your config:

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
chrismead
  • 2,163
  • 3
  • 24
  • 36
  • Perfect! Simple, I like it – Samuel Poirier Mar 10 '14 at 14:25
  • I suspect that this comes with a cost, perhaps someone can comment here. In order for this to work Log4Net likely closes the file and releases the file handle. This is a costly operation because the next write will require allocating a new file handle and also opening the file. While this may see "perfect", it very likely causes a performance hit. – Jazimov Dec 11 '16 at 17:43
  • Someone elaborated below (I saw as I read additional answers). At least the comment here will alert others who may not take the time to read this entire thread... – Jazimov Dec 11 '16 at 17:44
7

It is not supported "out of the box" from log4net. However, you could using the RollingFileAppender and create a manual class/method to clean up/delete the log file.

For reference, Log4Net: set Max backup files on RollingFileAppender with rolling Date

Another approach to avoid the file being locked would be to set the minimal locking level on the log file via:

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 
Seany84
  • 5,526
  • 5
  • 42
  • 67
  • Thanks! but when I try to delete the file, I get an exception because the log4net catch the file. How can I free the log file from the log4net? – Amir Brand Jul 29 '12 at 13:27
  • 1
    You could Use CloseFile() method before you call delete. – Darek Jul 29 '12 at 13:35
  • I suppose that is a separate question but do as Darek has suggested. Let us know how you get on and don't forget to mark as answered if this works for you. – Seany84 Jul 29 '12 at 14:13
5

If you don't want to suffer the performance implications of having a minimal lock (see answer), then you can temporarily configure the appenders to use a minimal lock, delete the files, and then bring the default behavior back.

Example when you're only using RollingFileAppenders:

// Release the lock on the log4net log files
var appenders = log4net.LogManager.GetRepository().GetAppenders();
foreach (var appender in appenders)
{
    var rollingFileAppender = appender as log4net.Appender.RollingFileAppender;
    if (rollingFileAppender != null)
    {
        rollingFileAppender.ImmediateFlush = true;
        rollingFileAppender.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
        rollingFileAppender.ActivateOptions();
    }
}

The files are now free to be deleted without problems.

Community
  • 1
  • 1
Gabriel G. Roy
  • 2,552
  • 2
  • 27
  • 39
2

Thanks guys I succeed clear the log. In the appender block of the log configuration file I added that line:

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 

The code that clear the log file is:

        RollingFileAppender fileAppender = LogManager.GetRepository()
                        .GetAppenders().FirstOrDefault(appender => appender is RollingFileAppender) as RollingFileAppender;


        if (fileAppender != null && File.Exists(((RollingFileAppender)fileAppender).File))
        {
            string path = ((RollingFileAppender)fileAppender).File;
            log4net.Appender.FileAppender curAppender = fileAppender as log4net.Appender.FileAppender;
            curAppender.File = path;

            FileStream fs = null;
            try
            {
                fs = new FileStream(path, FileMode.Create);
            }
            catch(Exception ex)
            {
                (log4net.LogManager.GetLogger(this.GetType())).Error("Could not clear the file log", ex);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }

            }
        }

Thank you all

Amir Brand
  • 313
  • 1
  • 3
  • 14
-2

Simply use the command

 File.WriteAllText("C:/Users.../log.xml", "");

at the start of the application.

EluciusFTW
  • 2,565
  • 6
  • 42
  • 59