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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:22:20+00:00 2026-05-24T04:22:20+00:00

I just wrote a simple logging class for use among various threads, based on

  • 0

I just wrote a simple logging class for use among various threads, based on examples I found at MSDN and a few other sites. Problem is, it creates the file log file, but never actually writes anything to it (it writes to the console properly though).

public class Logger
{
    private static Logger instance;
    private Logger() { }
    private static StreamWriter writer;

    public static Logger GetInstance()
    {
        lock (typeof(Logger))
        {
            if (instance == null)
            {
                instance = new Logger();
            }

            return instance;
        }
    }

    public void OpenFile(String file)
    {
        // Open log file for writing and append to it
        writer = new StreamWriter(file, true);
    }

    public void LogMessage(String message)
    {
        string dt = DateTime.Now.ToString("yyyy-MM-dd H:mm:ss :: ");

        writer.WriteLine(dt + message);
        Console.WriteLine(dt + message);
    }

    public void LogError(String error)
    {
        string dt = DateTime.Now.ToString("yyyy-MM-dd H:mm:ss :: ");

        writer.WriteLine(dt + "ERROR: " + error);
        Console.WriteLine(dt + "ERROR: " + error);
    }
}

and I am doing the following in my Program.

static void Main(string[] args)
{
    Logger log = Logger.GetInstance();
    log.OpenFile("app.log");

    log.LogMessage("Starting App...");
  • 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-24T04:22:21+00:00Added an answer on May 24, 2026 at 4:22 am
    writer.WriteLine(dt + message);
    writer.Flush();
    

    or when you declared the writer set AutoFlush to true:

    writer = new StreamWriter(file, true);
    writer.AutoFlush = true;
    

    Edit: Also since your class will access from multi-thread so you should use lock before writing to the stream because the StreamWriter instance is not thread safe, so:

    private readonly object _writerLocker = new object();
    
    lock (_writerLocker)
    {
        writer.WriteLine(dt + "ERROR: " + error);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote a simple C++ program in Visual Studio 2010 and I use
I wrote a simple web service in C# using SharpDevelop (which I just got
I just wrote some new utility methods within a non-page class for an existing
I just wrote my first OpenMP program that parallelizes a simple for loop. I
I just wrote a very simple Expect script for wrapping around rsync, but it
I wrote a simple logging function which displays a timestamp and uses vprintf() to
I just wrote a simple DirectShow Filter (which inherits from CTransformFilter). But I want
I just wrote a simple app to learn multi-threading, and I'm missing something. I
I just wrote a Background class for a new project. The background is basically
I just wrote a simple game (if you're interested, each player should say a

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.