Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:06:01+00:00 2026-06-09T09:06:01+00:00

I have implemented a logging method as follows: ThreadPool.QueueUserWorkItem((state) => { lock (appendLock) {

  • 0

I have implemented a logging method as follows:

 ThreadPool.QueueUserWorkItem((state) => {
    lock (appendLock) {
       using (StreamWriter log = File.AppendText(_logFile)) {
          log.WriteLine(message);
       }
    }
 }, null);

1: Is the lock necessary? I wanted to thread the logging and found the lock already in place. So rather than alter that code, I simply wrapped into a worker delegate.

2: assuming the lock is required: is this the correct implementation to enqueue the delegate containing a lock? The potential is reasonably high that multiple threads could be requesting log-writes. By enqueueing the delegate to a worker thread, the length of a File I/O execution should not affect the application itself.

3: assume several logWriteDelegate workers have been enqueued: Will the delegates be invoked in the order they were received? i.e., Now serving #32 …Now serving #33

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:06:02+00:00Added an answer on June 9, 2026 at 9:06 am

    1: yes, lock it is required. It could be accessed by multiple threads, especially since you’re using the pool.

    2: well, it’ll work. But it will keep the file locked making it hard to read. There are pre-existing logging frameworks that have worked a lot on this problem – it may be worth using me of them.

    3: no; with the lock and the pool, that’s two separate reasons not to expect strict ordering in the final file. In fact, becuase of the pool, a single thread’s messages could appear out of order. If you want ordering, you’ll need to write to a (synchronized) queue, and have a dedicated worker that pulls data (synchronised) back out of the queue and writes to the log file. Again, existing logging frameworks will have solved this for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a HttpModule in ASP.NET (framework 2.0). From my logging on the
I have implemented a test method with Jersey to run on my Google AppEngine
I have implemented clean URLs using the following in my .htaccess RewriteEngine on RewriteCond
I have implemented a very basic sign up using email address+name, although I would
I have an abstract Logger class with a virtual Log(std::string const&) method. That way
I am using II6. I have a Method(LogMessageToSIFDB) that I assume had an exception,
I have strange issue with logging in and out. I've implemented container-based security. I
I have a website running now. I have to implement some logging routines as
I have implemented correctly bump's api, and added this code: - (void) configureBump {
I have implemented pagination to my data, but the problem is I only have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.