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 6574031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:12:32+00:00 2026-05-25T15:12:32+00:00

I’m taking over a C# project, and when testing it out I’m getting errors.

  • 0

I’m taking over a C# project, and when testing it out I’m getting errors. The error is that the log file cannot be written to because it is in use by another process. Here’s the code:

    public void WriteToLog(string msg)
    {
        if (!_LogExists) 
        {
            this.VerifyOrCreateLogFile();  // Creates log file if it does not already exist.
        }

        // do the actual writing on its own thread so execution control can immediately return to the calling routine.
        Thread t = new Thread(new ParameterizedThreadStart(WriteToLog));
        t.Start((object)msg);
    }

    private void WriteToLog(object msg)
    {
        lock (_LogLock)
        {
            string message = msg as string;
            using (StreamWriter sw = File.AppendText(LogFile))
            {
                sw.Write(message);
                sw.Close();
            }
        }
    }

_LogLock is defined as a class variable:

    private object _LogLock = 0;

Based on my research and the fact that this has been working fine in a production system for a few years now, I don’t know what the problem could be. The lock should prevent another thread from attempting to write to the log file.

The changes I’ve made that need to be tested are a lot more log usage. We’re basically adding a debug mode to save much more info to the log than used to be saved.

Thanks for any help!

EDIT:

Thanks for the quick answers! The code for VerifyOrCreateLogFile() does use the _LogLock, so that shouldn’t be an issue. It does do some writing to the log before it errors out, so it gets past creating the file just fine.

What seems to be the problem is that previously only one class created an instance of the log class, and now I’ve added instances to other classes. It makes sense that this would create problems. Changing the _LogLock field to be static fixes the issue.

Thanks again!

  • 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-25T15:12:33+00:00Added an answer on May 25, 2026 at 3:12 pm

    The lock should prevent another thread from attempting to write to the log file.

    This is only true if you’re using a single instance of this class.

    If each (or even some) of the log requests use a separate instance, then the lock will not protect you.

    You can easily “correct” this by making the _LogLock field static:

    private static object _LogLock = 0;
    

    This way, all instances will share the same lock.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I have a reasonable size flat file database of text documents mostly saved in

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.