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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:42:46+00:00 2026-06-15T18:42:46+00:00

Hey so I have a logger class that is called frequently, sometimes when its

  • 0

Hey so I have a logger class that is called frequently, sometimes when its called fast repeatedly it will throw an exception that the file is already open being used by another application. The only way we found a way around this was to catch the exception then try to open it again… I’m not sure how to handle this properly.

    /// <summary>
    /// Open a log file based on a date
    /// </summary>
    /// <param name="date"> Date of the file to open </param>
    public static void OpenLogFile(DateTime date)
    {
        while (true)
        {
            try
            {
                logStream = File.Open("Logs/ems." + date.ToString("yyyy-MM-dd") + ".log", FileMode.Append);
                break;
            }
            catch
            {
                continue;
            }
        }
    }




    /// <summary>
    /// Writes to a log file the specified input
    /// </summary>
    /// <param name="input"> Content to write to the log file </param>
    public static void Log(string className, string methodName, string input)
    {
            OpenLogFile(DateTime.Now);

            using (StreamWriter s = new StreamWriter(logStream))
            {
                s.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + '[' + className + '.' + methodName + "] " + input);
            }

            CloseLogFile(); 
    }




    /// <summary>
    /// Closes the current log file
    /// </summary>
    public static void CloseLogFile()
    {
        if (logStream != null)
        {
            logStream.Close();
            logStream = null;
        }
    }
  • 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-15T18:42:47+00:00Added an answer on June 15, 2026 at 6:42 pm

    Since this is a log file, I think the obvious (and correct) solution, is to leave the file open for the duration of your program’s execution.

    If you don’t want to use an already-implemented 3rd party logger, like Apache’s log4net, you could write your own little logger class that uses thread-safe mechanisms for writing lines to the file.

    It would probably be a static class, or singleton. Generally, execution at the beginning and end of your program is quite predictable, so it should be clear where to initialize, and close out the object/class.

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

Sidebar

Related Questions

Hey we have a library class (lib/Mixpanel) that calls delayed job as follows: class
Hey I have an abstract class named Partition which is a functor, and its
Hey have method called voted? that sees if someone voted. Where would I put
Hey i have a iframe that i am sizing to its content with the
Hey I have a really annoying IE7 bug that I am trying to work
Hey guys i am currently working on an android app that will allow me
Hey I have seen sites that instead of an int show you a string.
hey i have a requirement , where i have to compare a file name
Hey I have a list that has values in it, I want to print
Hey StackOverflow Community, Regarding throwing exceptions. Generally, when do I throw and exception, and

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.