locked
C# program just closed / automatically restart up RRS feed

  • Question

  • I wrote a program and it was suppose to run all weekend. I have a logfile  we started pulling data at 08/07/2015 03:08:50 PM and the last timestamp we have is at 08/08/2015 04:00:01 AM. This morning the program had closed and we did not know why. I data log all exceptions but there  are none in the file. So I do not know why the program just close by its self.

    Is there a way to data-log every time a program closes and why?

    is there a way to automatically restart up a program that closes ? 

    Monday, August 10, 2015 3:09 PM

Answers

  • ok but I cant log it, because this is before I make my file. Also this is only called one time... so I do not see how it would help 


    Here is a simple way to data log an exception. 

    private static void WriteLog(Exception e)
    {
        var logFile = "log.txt";//this is the path to your log file
    
        using (FileStream stream = File.Open(logFile, FileMode.Append, FileAccess.Write, 
            FileShare.ReadWrite))
        {
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine("Date: " + DateTime.Now);
                writer.WriteLine("Message: " + e.Message);
                writer.WriteLine("StackTrace: " + e.StackTrace);
            }
        }
    }

    You can put it in your "program.cs" file, and then call it wherever you decided to handle your exceptions.

    If the log file doesn't exist, it creates it.  If the file is being used, it will still write to it.

    This method should rarely, if ever, be called, so it's safe to open and close the file in the method.  You don't need to keep a file open for logging exceptions.

    • Marked as answer by Kristin Xie Tuesday, August 18, 2015 2:54 AM
    Tuesday, August 11, 2015 2:36 AM

All replies

  • Do you have a generic try-catch in your Program.cs/Main() method? Does your log writer flush out the changes often enough?

    Monday, August 10, 2015 3:27 PM
  • No, I not have one there...

    ok I just added this :

    try
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
                catch (Exception)
                {
    
                }

    ok but I cant log it, because this is before I make my file. Also this is only called one time... so I do not see how it would help 

    Monday, August 10, 2015 3:46 PM
  • Swallowing an exception is the single worst thing you can do. You should not even catch exception in the first place.

    You need to read up on proper exception handling:

    Exception Handling Best Practices in .NET - CodeProject

    Vexing exceptions - Fabulous Adventures In Coding - Site Home - MSDN Blogs

    Monday, August 10, 2015 4:18 PM
  • How can I start the program ?
    Monday, August 10, 2015 6:09 PM
  • Just in case, here is how to handle unhandled exceptions in WinForms.

    It also includes a Restart method for restarting your application.

    static void Main()
    {
        Application.ThreadException += ApplicationOnThreadException;
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    
    private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        try
        {
            var ex = (Exception)e.ExceptionObject;
    
            LogException(ex);
        }
        catch (Exception exception)
        {
            LogException(exception);
        }
    }
    
    private static void ApplicationOnThreadException(object sender, ThreadExceptionEventArgs e)
    {
        LogException(e.Exception);
    }
    
    private static void LogException(Exception ex)
    {
        //put your data log code here
    }
    
    private static void Restart()
    {
        Process.Start(Application.ExecutablePath);
        Application.Exit();
    }

    This will restart your application if it crashes.


    • Proposed as answer by Christopher84 Monday, August 10, 2015 8:00 PM
    • Edited by Andy Stallard Tuesday, August 11, 2015 3:09 PM You don't need to call Restart() in LogException. It's not necessary. The app doesn't crash.
    Monday, August 10, 2015 6:24 PM
  • Just add an emergency logging here.
    Monday, August 10, 2015 7:24 PM
  • @Christopher: The problem is, that the existing error logging seems to have some gaps. Thus an general exception handler on the top level is okay. He just needs to log the error. Which he doesn't know how to do it..
    Monday, August 10, 2015 7:25 PM
  • ok but I cant log it, because this is before I make my file. Also this is only called one time... so I do not see how it would help 


    Here is a simple way to data log an exception. 

    private static void WriteLog(Exception e)
    {
        var logFile = "log.txt";//this is the path to your log file
    
        using (FileStream stream = File.Open(logFile, FileMode.Append, FileAccess.Write, 
            FileShare.ReadWrite))
        {
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine("Date: " + DateTime.Now);
                writer.WriteLine("Message: " + e.Message);
                writer.WriteLine("StackTrace: " + e.StackTrace);
            }
        }
    }

    You can put it in your "program.cs" file, and then call it wherever you decided to handle your exceptions.

    If the log file doesn't exist, it creates it.  If the file is being used, it will still write to it.

    This method should rarely, if ever, be called, so it's safe to open and close the file in the method.  You don't need to keep a file open for logging exceptions.

    • Marked as answer by Kristin Xie Tuesday, August 18, 2015 2:54 AM
    Tuesday, August 11, 2015 2:36 AM