5

I'm using log4net in C# Win Form App and I have start/stop toggle button -

When I click "start" the log4net config are loaded and the log file is created - That is working.

But now I want to add the option to click "stop" button, for stopping the logger and of course release the file so I could open it with openFileDialog that I have in my app for reading the log file text with StreamReader to show the log file on a multiline textbox.

Gal Barak
  • 65
  • 5

3 Answers3

1

You can modify the Threshold during runtime. To stop logging set it to Off. I have already shown how this works in this answer.

  • Hi. I stoped the logging as you wrote but the problem is that also after that, the last log file that was created is "readonly" and I cant open it with my openFileDialog – Gal Barak Mar 19 '19 at 08:48
  • Yes, the log-File is readonly, but you can read it with e.g. notepad.exe. What do you want tp do exactly with your file? – Jürgen Müller Mar 19 '19 at 11:25
  • i want the option to load the file after recording with openFileDialog and use StreamReader to show the log file on a multiline textbox – Gal Barak Mar 21 '19 at 12:47
0

Stopping log4net during runtime requires modifying its config file's:

<level value="ALL" />

to

<level value="INFO" />

If you can't change the config file you should define a proxy for log.debug():

static public class LogDebugProxy
{
    static public bool LogDebug = false;
    static public void debug(log4net.ILog log)
    {
        if (LogDebug)
            log.debug();
    }
}

Then replace log.debug() with LogDebugProxy.debug(log) in your entire project.

Now, you can Enable / Disable it by it's proxy.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
0

This works for me to read the log4net log-file:

            string text;
            var fileStream = new FileStream(@"C:\logs\yourLogFile.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                text = streamReader.ReadToEnd();
                MessageBox.Show(this, text);
            }