I've written a Console application that is typically run via Task Scheduler with no user present. If appropriate, it redirects Console output to a file (with the app-start timestamp in the name) that happens to be on a network.
I've seen a few cases -- they are rare -- where it seems that the file contents ends prematurely (that is, later messages generated by the program are not there). Output from a third-party logging tool (Gibraltar Loupe) has shown me that calls to Console.WriteLine can fail if there is a network glitch causing the output file to be unavailable temporarily, causing an unhandled exception.
(Once, the normal app's normal exception handling was -- somewhat remarkably -- able to write the message about the unhandled exception that resulted when Console.WriteLine failed due to "The specified network name is no longer available" -- that must have been a very temporary glitch!)
I can't think of an approach other than to replace all calls to Console.WriteLine with calls to something of my own that traps such errors, waits until the network is again available, and tries again. (Or possibly it could redirect output to a local file for the rest of the run, but other things would likely fail if the network stays unavailable.)
Replacing all calls to Console.WriteLine in an app with hundreds of them will lead to a major source-code-changes explosion, forcing changes in files that haven't changed in many months. Do I have a choice?
Does anyone have a different suggestion for how to handle this (other than to have abstracted away Console.WriteLine from the beginning of the project)?
Thank you for any suggestions.
Not the greatest idea to try to tackle a flaky hardware problem in software. But consider:
You probably think the last one is attractive. Sample code: