Is there any efficient logger library or code segment on Metro C++? RRS feed

  • General discussion

  • There have been C# examples, such as http://code.msdn.microsoft.com/windowsapps/Logging-Sample-for-Windows-0b9dffd7. But I am not sure whether this library is efficient enough. I am now writing a logger wrapper which will be used by several threads. Originally I was using Windows::Storage::FileIO::AppendTextAsync. This function is thread safe (http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/6435e117-351a-4b96-8583-cd5587d1da2b/#6435e117-351a-4b96-8583-cd5587d1da2b). But from task manager I found that if it is called a little frequently, the disk read/write will be very high (maybe over 20MB/s).

    So would you please give me some advice? Thank you all very much.

    Tuesday, March 5, 2013 8:00 AM

All replies

  • Hi Borden,

    I've switched this to a discussion since you're looking for 3rd party recommendations.

    Part of "efficiency" will be determining what exactly you want to log and how. You can probably improve your disk access by batching entries in memory rather than opening and closing the file constantly.


    Tuesday, March 5, 2013 11:14 PM
  • Hello, Rob, really thank you very much for the reply. Yes, currently I've cached the logs until it reaches a certain length (such as 20K), and it works well. But just as what you've mentioned, it depends on the amount of the logs and how frequently it needs to be logged (we are still working on this). On the other hand, this way needs some synchronization techniques (such as mutex) and in my code UI also needs to write to the same log file, I am worried that UI might be blocked. Please kindly tell me if you have other info. Thanks!
    Wednesday, March 6, 2013 1:41 AM
  • I'd probably do all of the log writing from a single background thread. Other threads should be able to queue entries fairly quickly with minimal synchronization.


    Thursday, March 7, 2013 10:55 PM
  • Just put a CriticalSection inside your logging API. However, if you're really logging 20MB/sec then you'll have to do some serious tricks to prevent stalling the UI. You can't simply write the buffer to disk in your logging API when it reaches a certain size because that'll eventually stall. My first guess would be to have the API part write to a buffer then have a background thread occasionally grab that buffer and provide a new one to the API while writing the old one to disk. Another alternative is have your logging API functions, when the current output buffer is full, place it in a queue for the background writing thread then allocate and start writing to a new buffer.

    Note: I would use the C runtime functions (fopen, etc.) or Win32 APIs for logging since you're probably writing to local app storage.
    • Edited by henador Friday, March 8, 2013 3:51 AM
    Friday, March 8, 2013 3:49 AM