Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. trying to write to a log file in destructor

trying to write to a log file in destructor

Scheduled Pinned Locked Moved C#
csharpquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gavin Jeffrey
    wrote on last edited by
    #1

    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?

    A B A 3 Replies Last reply
    0
    • G Gavin Jeffrey

      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?

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      post some code

      G 1 Reply Last reply
      0
      • A Anonymous

        post some code

        G Offline
        G Offline
        Gavin Jeffrey
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • G Gavin Jeffrey

          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?

          B Offline
          B Offline
          benjymous
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • G Gavin Jeffrey

            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.

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            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.


            Do you want to know more?


            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.

            G 1 Reply Last reply
            0
            • C Colin Angus Mackay

              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.


              Do you want to know more?


              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.

              G Offline
              G Offline
              Gavin Jeffrey
              wrote on last edited by
              #6

              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??

              C 1 Reply Last reply
              0
              • G Gavin Jeffrey

                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??

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                How about at the end of your Main method?


                Do you want to know more?


                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.

                G 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  How about at the end of your Main method?


                  Do you want to know more?


                  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.

                  G Offline
                  G Offline
                  Gavin Jeffrey
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • G Gavin Jeffrey

                    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?

                    A Offline
                    A Offline
                    afinnell
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups