Monday, 15 August 2011

c# - writing a logfile when backgroundWorker job completed -



c# - writing a logfile when backgroundWorker job completed -

i have c# application displays message on messagebox after query run.at sametime want write logfile. tried no luck. logfile empty. had created empty file.

private void backgroundworker_import_dowork(object sender, doworkeventargs e) { //finally, loop through each row in dataview , execute insert statements against database int reccount = 0; successcount = 0; failedcount = 0; dv.rowfilter = "execute_bit in ('1')"; using (mysqlconnection connectionmysql = new mysqlconnection(connectionstringmysql)) { connectionmysql.open(); mysqlcommand commandmysql = new mysqlcommand(); commandmysql.connection = connectionmysql; foreach (datarowview rowview in dv) { reccount++; backgroundworker_import.reportprogress(reccount); commandmysql.commandtext = rowview["sql"].tostring(); seek { successcount = successcount + commandmysql.executenonquery(); //writetologfile(""); //writetologfile(""); **writetologfile(datetime.now.tostring() + ", " + reccount.tostring() + "," + successcount.tostring() + "," + failedcount.tostring()); }** grab (exception) { failedcount++; } } } } private void backgroundworker_import_runworkercompleted(object sender, runworkercompletedeventargs e) { string msg = ""; msg = msg + "records imported: " + successcount.tostring() + environment.newline; msg = msg + "records failed import: " + failedcount.tostring() + environment.newline + environment.newline; msg = msg + "records excluded import (20 min grace-period): " + (tblvehicles.rows.count - successcount - failedcount).tostring(); progressbar1.visible = false; messagebox.show( msg, "operation complete", messageboxbuttons.ok, messageboxicon.information); } **private void writetologfile(string[] output) { streamwriter sw = null; filestream fs = null; string logfilefilename = system.io.path.combine( "c:/luvi/logfile.txt"); fs = file.open(logfilefilename, filemode.append, fileaccess.write); sw = new streamwriter(fs, system.text.encoding.utf8); foreach (string line in output) { sw.writeline(line); } sw.close(); sw = null; }**

you utilize file.writealllines shown in this topic.

its' syntax follows:

public static void writealllines( string path, string[] contents )

in case utilize so:

string logfilefilename = @"c:/luvi/logfile.txt"; file.writealllines(logfilefilename, output);

note: overwrites file, if want append them utilize file.appendalllines.

you need phone call method aswell, may problem because not see in code. in next changes have replaced string msg array, , added (you utilize list , phone call list.add).

private void backgroundworker_import_runworkercompleted(object sender, runworkercompletedeventargs e) { string[] msg = new string[] {}; msg[0] = "records imported: " + successcount.tostring(); msg[1] = "records failed import: " + failedcount.tostring(); msg[2] = "records excluded import (20 min grace-period): " + (tblvehicles.rows.count - successcount - failedcount).tostring(); // write log! writetologfile(msg); // show messagebox. string showmsg = msg[0] + environment.newline + msg[1] + environment.newline + msg[2]; progressbar1.visible = false; messagebox.show(showmsg, "operation complete", messageboxbuttons.ok, messageboxicon.information); } private void writetologfile(string[] output) { string logfilefilename = "c:/luvi/logfile.txt"; file.appendalllines(logfilefilename, output); }

c#

No comments:

Post a Comment