c# - Windows service keeps on failing -
i have windows service designed continuously retrieve messages azure service bus queue , pass other queues.i have deployed service 1 of server computer unfortunately service keeps failing @ random time interval.
my application handles exceptions , writes them file.the main purpose of application hook queue , listen messages continuously , never move application stop stage.i'm using timer in application , don't think causing problem.i'd know best approach handle errors , make application stable, below code. in advance.
public partial class scheduler : servicebase { private timer scheduletimer = null; private string servicenamespace; private string issuesecretkey; private string sourcequeue; private string destinationqueue; public scheduler() { initializecomponent(); } protected override void onstart(string[] args) { scheduletimer = new timer(); this.scheduletimer.interval = 1000;//1 sec this.scheduletimer.elapsed += new system.timers.elapsedeventhandler(this.timer1_tick); scheduletimer.enabled = true; writetofile("application started : "+datetime.now.tostring()); } protected void timer1_tick(object sender, elapsedeventargs e) { scheduletimer.enabled = false; writetofile("business logic started : " + datetime.now.tostring()); //business logic code goes here } protected override void onstop() { scheduletimer.enabled = false; writetofile("application stoped : "+datetime.now.tostring()); } public void writetofile(string text) { string directory = appdomain.currentdomain.basedirectory; string logfilepath = directory + "logfile.txt"; using (streamwriter writer = new streamwriter(logfilepath, true)) { writer.writeline(text); writer.close(); } } public void writeerrorstofile(exception ex) { string directory = appdomain.currentdomain.basedirectory; string errorlogfilepath = directory + "errorlogfile.txt"; using (streamwriter writer = new streamwriter(errorlogfilepath, true)) { writer.writeline("time occured: " + datetime.now.tostring()); writer.writeline(ex.message +" "+ datetime.now.tostring()); writer.close(); } }
Comments
Post a Comment