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

  • Home
  • SEARCH
  • 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 3795566
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:06:12+00:00 2026-05-19T13:06:12+00:00

As an exercise in idle curiosity more than anything else, consider the following simple

  • 0

As an exercise in idle curiosity more than anything else, consider the following simple logging class:

internal static class Logging
{
    private static object threadlock;

    static Logging()
    {
        threadlock = new object(); 
    }

    internal static void WriteLog(string message)
    {
        try
        {
            lock (threadlock)
            {
                File.AppendAllText(@"C:\logfile.log", message);
            }
        }
        catch
        {
            ...handle logging errors...
        }
    }
}

Is the lock needed around File.AppendAllText(...) or is the method inherently thread-safe by its own implementation ?

Searching for information on this yields a lot of contradictory information, some say yes, some say no. MSDN says nothing.

  • 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-05-19T13:06:12+00:00Added an answer on May 19, 2026 at 1:06 pm

    File.AppendAllText is going to acquire an exclusive write-lock on the log file, which would cause any concurrent thread attempting to access the file to throw an exception. So yes, you need a static lock object to prevent multiple threads from trying to write to the log file at the same time and raising an IOException.

    If this is going to be an issue, I’d really suggest logging to a database table which will do a better job of handling concurrent log writers.

    Alternatively, you can use TextWriterTraceListener which is thread-safe (well, it’s going to do the locking for you; I’d rather write as little of my own multithreaded code as possible).

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

Sidebar

Related Questions

Consider the following simple exercise: package require Tclx keylset myArray v1.5 ready puts $myArray
The exercise says: Create a Text class that contains a string object to hold
Here is an exercise: Either prove the following or give a counterexample: (a) Is
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates
As part of an exercise, am implementing an ArrayList which will support Enumerations. Following
class Exercise < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :exercises end
Its a exercise that ask to indicate the class Big-Theta(g(n)) the functions belongs to
It's an exercise of EF code-first. There's a simple method. I want to get
The exercise I have is the following: In display() add a method drawLine. Probably,
Here is an exercise in the Algorithm Design Manual . Consider the problem of

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.