简体   繁体   中英

c# how to log unexpected program exit

I have a windows c# program (.net 3.5) that runs in the background on a real time controller (Windows 7 Embedded).

After a few days the process apparantly terminates. Now I am thoroughly logging the actions and exceptions of my program. Everything that can throw an exception is surrounded by try/catch and is being writting to a log file, and if the logger has an exception that is written to the windows event log.

But there is nothing in the logs that hints to an exception that could cause a program termination.

Are there other techniques to catch and log program termination?

So far I couldn't reproduce the bug in an online debug session.

The program itself is very simple. It uses two stopwatches and spawns a thread to communicate with a real time program. It writes an 'alive' boolean, an 'error' integer and reads another integer. The log file size is limited to 10 MBytes. If the size is reached, the file is renamed with a date and a new file is opened. Files older than 30days will be deleted.

I have checked the windows event logs and couldn't find anything that directly relates to the program.

What would you do?

The best approach for you would be to consider the global exceptions. You can Catching the First Chance Exceptions which will catch every exception that occurs in your application which can be done as simply as adding the following piece of code:

AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
{
    Debug.WriteLine(eventArgs.Exception.ToString());
};

Also for more details about handling the global exception you can refer to this really helpful article.

I could my answer my own question.

Unfortunately I left out a detail in my question which I deemed irrelevant. The application is started by the windows task scheduler at system boot before any user logs in. There is a checkbox at the task properties that terminates the task if it takes longer than 3 days. I forgot to uncheck it :(.

Thank you for your contributions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM