.net - How to handle intermittent errors in Console output to network file -
i've written console application typically run via task scheduler no user present. if appropriate, redirects console output file (with app-start timestamp in name) happens on network.
i've seen few cases -- rare -- seems file contents ends prematurely (that is, later messages generated program not there). output third-party logging tool (gibraltar loupe) has shown me calls console.writeline can fail if there network glitch causing output file unavailable temporarily, causing unhandled exception.
(once, normal app's normal exception handling -- remarkably -- able write message unhandled exception resulted when console.writeline failed due "the specified network name no longer available" -- must have been temporary glitch!)
i can't think of approach other replace calls console.writeline calls of own traps such errors, waits until network again available, , tries again. (or possibly redirect output local file rest of run, other things fail if network stays unavailable.)
replacing calls console.writeline in app hundreds of them lead major source-code-changes explosion, forcing changes in files haven't changed in many months. have choice?
does have different suggestion how handle (other have abstracted away console.writeline beginning of project)?
thank suggestions.
not greatest idea try tackle flaky hardware problem in software. consider:
- redirect local file, use scheduled task move files.
- write little guard app uses process.start() start app. , uses process.exitcode see how fared. can restart app, multiple times if necessary, delaying longer between each attempt.
- reassign console.error , console.out own textwriter derived class. override write(char) , call original error/out.write(char) method, wrapped in try/catch. try multiple times on failure, delaying longer between attempts.
you think last 1 attractive. sample code:
using system.io; class mywriter : textwriter { private textwriter writer; public mywriter(textwriter writer) { this.writer = writer; } public override encoding encoding { { return writer.encoding; } } public override void write(char value) { (int delay = 1; ; delay *= 2) { try { writer.write(value); return; } catch { if (delay > 3600) throw; } system.threading.thread.sleep(1000*delay); } } } class program { static void main(string[] args) { console.setout(new mywriter(console.out)); console.seterror(new mywriter(console.error)); // etc... } }
Comments
Post a Comment