17

We use the log4net to log the winform application's event and error. Our customer want check the log file during the application running. But I can't find out when and how the log4net do the write(commit) operation. And how to meet the customer's requirement, except creating another logger by myself. Any help? Thanks.

Iain Holder
  • 14,172
  • 10
  • 66
  • 86
Jollian
  • 309
  • 2
  • 3
  • 8
  • We are using log4net in our Windows Forms applications and we have no issues about this. There are some latency between event and file update, but we talking about seconds. Maybe the problem is in some another place? – Sergey Teplyakov Mar 16 '10 at 08:01
  • 1
    Yes, maybe our custormer's problem is the second situation. And we'll solve this problem by MemoryAppender as Peter said. Thank you all, especially Peter. Your answer is much more helpful. – Jollian Mar 17 '10 at 07:14

4 Answers4

10

If you're using the FileAppender, this appender inherits the TextWriterAppender, which in turn exposes the ImmediateFlush property. The value of this property is true by default, and forces the appender to do a Flush() on the underlying stream for each Append operation.

Depending on how you envision the customer "monitoring" the log file, an idea could be to enable monitoring from within your application. This can be done by in addition to appending to a file, using the MemoryAppender and reading events from that appender.

Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
1

One thing I did notice when I was using log4net in a SharePoint instance is that, depending on your security configuration and the implementation of your configuration loading, the user that spins up the application pool may not have rights to write to the local file system to create the log file.

Because of this you need to ensure that your configuration is invoked with the right credentials, in SharePoint it would be in an elevated security context. Not entirely related, but it might be an issue you run across.

Mandrake
  • 1,161
  • 1
  • 10
  • 11
0

see this SO post for howto programmatically flush a log4net log. Joe's answer while slightly inaccurate is roughly the same code and so would presumably work for flushing all buffered appenders.

Community
  • 1
  • 1
Dead.Rabit
  • 1,965
  • 1
  • 28
  • 46
-1

You talk about a log file, so presumably you're using FileAppender or a derived class. This will buffer output by default. Buffered output is much more efficient, so to meet your requirement, I'd suggest you provide some mechanism to flush the log before viewing it, rather than forcing a commit after each write operation.

You can do this with code like the following:

foreach (IAppender appender in LogManager.GetRepository().GetAppenders())
{
    BufferingAppenderSkeleton b = appender as BufferingAppenderSkeleton;
    if (b != null) b.Flush();
}
Joe
  • 122,218
  • 32
  • 205
  • 338
  • 1
    "This will buffer output by default." This is not true! FileAppender (more specifically its base class TextWriterAppender) flushes after every Append operation! See http://logging.apache.org/log4net/release/sdk/log4net.Appender.TextWriterAppender.ImmediateFlush.html Therefore, you do not need to do anything because the latest log entries will always be written to the file immediately, so that your customer can simply open the file. – gehho Mar 16 '10 at 07:38
  • 1
    @gehho is right. FileAppender does NOT buffer. The built-in appenders that does buffering are AdoNet-, BufferingForwarding-,Remoting- and the SmtpXXX appenders. – Peter Lillevold Mar 16 '10 at 08:37