I want to make a program which times the ammount of time my computer is switched on for. I want it to log this in a text file like this

30/12/02
12:50->15:00

I know how to make a program which gets the time and logs it in a text file. My only problem is I don't know how to make it turn on automaticly when my computer is turned on and close when my computer is turned off