Stefan Valianu
Stefan Valianu

Reputation: 1410

Running Log4Net appenders on separate thread

Currently, I've got my own logging system, where the log is essentially a queue, with a separate thread listening to that queue and doing all of the actual write operations. The system processes a TON of log messages, files can easily exceed 10 MB sizes in minutes, so doing the actual logging in the calling thread is not feasible.

I can't find resources online that show how threading would work in log4net, if log4net supports this kind of message passing architecture already, or other similar features to work in a threaded environment. Are there any pre-existing features that would help me?

Is this possible without creating a log4net wrapper?

Upvotes: 5

Views: 5544

Answers (5)

raider33
raider33

Reputation: 1673

I've used ParallelForwardingAppender and AsyncForwardingAppender appenders with great success. These appenders are available from the following repository:

https://github.com/cjbhaines/Log4Net.Async

ParallelForwardingAppender utilizes BlockingCollection and other facets of the Task Parallel Library to implement a loss-less message queue.

AsyncForwardingAppender utilizes a ring buffer and a 10ms polling period on the background thread to prioritizes application performance over logging fidelity.

Upvotes: 0

tarn
tarn

Reputation: 556

If you use .NET Framework 4 you can use BlockingCollection to keep log sequence

Upvotes: 0

Tim Lloyd
Tim Lloyd

Reputation: 38434

You can always look at the source code for log4net to bottom out these sorts of questions. It is open source.

Upvotes: 1

Steven Sudit
Steven Sudit

Reputation: 19620

It's not a bad idea. The trick is to queue up the entries and process them in a single queue. Take a look at http://www.drdobbs.com/visualstudio/225700095 for a good example.

Upvotes: 1

Dr Herbie
Dr Herbie

Reputation: 3940

You may want to rethink the threading approach if your log data depends on being in a specific order -- threading may interfere with that and end up posting log entries out of sequence.

You could try using MSMQ (or some other queue technology) to quickly post the log messages off to some other process which will then do the physical writes to storage. This will guarantee that messages appear in the same order they were sent.

Upvotes: 1

Related Questions