views:

276

answers:

3

I'm developing a time-critical ASMX web service and I'm currently using own class with static methods to write lines to shared log file. Logging is not guarded by locks/Monitors because application writes log quite a lot. I don't know what happens if a thread is scheduled out while in static method writing a line and another thread scheduled in also writes. Now I'm asking what is the best way to implement logging?

Thank you - matti

A: 

Id recommend using a third party solution like log4net or nlog instead of doing it yourself tbh, saves reinventing the wheel

AdaTheDev
A: 

The implementation we have chosen is to use the Microsoft Enterprise Library Logging Block and then we have a class that has some simplified static methods setup that we call which actually write the log message out the log file. We have not had any issues with using this in our multi-threaded environment.

Adam Gritt
A: 

I agree with @AdaTheDev. Using log4net you can create a customer Appender (the piece that takes a logged message and delivers it to some destination, such as file, email, Event Log, etc). This appender could write the log messages to MSMQ.

You could then have a small component that reads the queue (single threaded) and writes the messages to a log file. This would mean that to log a message it would take only as long as it takes to render the log message and write it to the queue. The act of writing the messages out to the file would be single threaded and completely decoupled from your critical business code on the ASP.NET side of things.

Jeremy Wiebe