trying to write to a log file in destructor
-
Hi, I have a c# console app and all i want to do is write to a log file when the program ends. I put my code in the destructor but it doesn't write to the log file. Any ideas why this is happening or how i can solve this?
-
Hi, I have a c# console app and all i want to do is write to a log file when the program ends. I put my code in the destructor but it doesn't write to the log file. Any ideas why this is happening or how i can solve this?
-
ok this is my destructor - ~TiffConverterClass() { Logger l = new Logger(@"c:\testLog.txt"); l.writeLine(Logger.logType.Info ,"Application has been disposed"); l.Dispose(); } i have a class variable in TiffConverterClass i.e private Logger log but i figured that maybe when I close the program it gets disposed before it gets to this destructor - thats why i declare it again in the destructor. This destructor is in the same class as the main method.
-
Hi, I have a c# console app and all i want to do is write to a log file when the program ends. I put my code in the destructor but it doesn't write to the log file. Any ideas why this is happening or how i can solve this?
I'm not certain, but this could be something to do with garbage collection (C# destructors don't work the same as C++ destructors - clicky[^]) - apparently there's no guarantee that a class's destructor will ever be called (so maybe this is your problem) -- Help me! I'm turning into a grapefruit! Phoenix Paint - back from DPaint's ashes!
-
ok this is my destructor - ~TiffConverterClass() { Logger l = new Logger(@"c:\testLog.txt"); l.writeLine(Logger.logType.Info ,"Application has been disposed"); l.Dispose(); } i have a class variable in TiffConverterClass i.e private Logger log but i figured that maybe when I close the program it gets disposed before it gets to this destructor - thats why i declare it again in the destructor. This destructor is in the same class as the main method.
The destructor (properly called a finalizer in .NET) should not be used for this kind of thing because it is non-deterministic and when your application ends it is obviously just not being called at all. (I'm not actually sure if it should have been called or not - I simply don't use finalizers. They aren't recommended anyway) gavinJeffrey wrote: This destructor is in the same class as the main method The Main method is static, it doesn't require an instance of the class to operate, so you would never get a finalizer called if that is all you were using.
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
-
The destructor (properly called a finalizer in .NET) should not be used for this kind of thing because it is non-deterministic and when your application ends it is obviously just not being called at all. (I'm not actually sure if it should have been called or not - I simply don't use finalizers. They aren't recommended anyway) gavinJeffrey wrote: This destructor is in the same class as the main method The Main method is static, it doesn't require an instance of the class to operate, so you would never get a finalizer called if that is all you were using.
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
Colin Angus Mackay wrote: The Main method is static, it doesn't require an instance of the class to operate, so you would never get a finalizer called if that is all you were using. I do declare a new object of the same class (TiffConverterClass) in the main method. So i definetely instantiate it. But i hear your advice on Finalizers so.... Are there any alternatives to actually get my program to write to a log file just before terminating??
-
Colin Angus Mackay wrote: The Main method is static, it doesn't require an instance of the class to operate, so you would never get a finalizer called if that is all you were using. I do declare a new object of the same class (TiffConverterClass) in the main method. So i definetely instantiate it. But i hear your advice on Finalizers so.... Are there any alternatives to actually get my program to write to a log file just before terminating??
How about at the end of your Main method?
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
-
How about at the end of your Main method?
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
I tried that but... the problem is that in my main method I instantiate the class and then call a method of that class that does all the work (this method sets up the timer) but I have to put in a Console.ReadLine() right at the end to stop the program from ending. If i put it before the Console.ReadLine() then it doesn't actually write it when the program ends and if i put it after it never gets there.
-
Hi, I have a c# console app and all i want to do is write to a log file when the program ends. I put my code in the destructor but it doesn't write to the log file. Any ideas why this is happening or how i can solve this?
Very easy.. replace delegate with anything you need
using System;
namespace ConsoleApplication1
{
/// /// Summary description for Class1.
///
class Class1
{
/// /// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
//
// TODO: Add code to start application here
//
}private static void CurrentDomain\_ProcessExit(object sender, EventArgs e) { Console.WriteLine("Bye"); } }
}
- Drew