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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:15:54+00:00 2026-06-05T03:15:54+00:00

I am trying to add a logfile to a program I am making, but

  • 0

I am trying to add a logfile to a program I am making, but when I tell the main thread to exit (using Application.ExitThread()), logstrmWriter is suddenly null before it ever gets there. This is a very simple script.

private static FileStream appLogStream;
internal static string logFile;
internal static StreamWriter logstrmWriter;
public static void Main()
{    
    logFile = Application.StartupPath + @"\Archiver.log";
    appLogStream = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.Read);
    TextWriter logtxtWriter = Console.Out;
    StreamWriter logstrmWriter = new StreamWriter(appLogStream);
    if(!console) Console.SetOut(logstrmWriter);

    Application.ApplicationExit += new EventHandler(OnApplicationExit);
    Application.Run();
}
internal static void OnApplicationExit(object sender, EventArgs e)
{
    active = false; Console.WriteLine("Main thread is shutting down. Sending Interrupt...");
    Archiver.Stop(); Console.WriteLine("Shutdown. Log and Exit");
    Console.WriteLine();
    logstrmWriter.Flush();
    logstrmWriter.Close();
    logstrmWriter.Dispose();
}
  • 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-05T03:15:56+00:00Added an answer on June 5, 2026 at 3:15 am

    You’re creating a local variable that hides the static variable in question. As such, you never initialize logstrmWriter at all.

    TextWriter logtxtWriter = Console.Out;
    /* StreamWriter */ logstrmWriter = new StreamWriter(appLogStream);  // Remove the redeclaration here!
    if(!console) Console.SetOut(logstrmWriter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to add a class object into a List using reflection, but when invoking
Im trying to add a directory foo to my repo, but there are some
I'm using the Microsoft Logging Application Block (version 5 beta 2) and I'm trying
I'm trying to add some extended error codes to the event log but I
I'm trying to add logging to my windows web service built with Quartz.net using
I'm trying to add log4net logging to a web application I'm writing. I've got
I am trying to add a jxmapviewer control in my form using netbeans and
i'm new to django and im trying add a user using django admin page.
I'm trying add a tab to my web page that looks like this: Using
I'm trying add vertical lines to my grid. I have found some examples but

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.