在backgroundWorker作业完成时编写日志文件

时间:2014-06-19 10:20:08

标签: c#

我有c#应用程序,它在运行查询后在messagebox上显示一条消息。同时我希望它写一个日志文件。这是我尝试但没有运气。我的日志文件是空的。 它创建了一个空文件。

 private void backgroundWorker_Import_DoWork(object sender, DoWorkEventArgs e)
        {
            //Finally, loop through each row in the dataView and 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();
                    try
                    {
                        successCount = successCount + commandMySql.ExecuteNonQuery();
                        //WriteToLogFile("");
                        //WriteToLogFile("");
                        **WriteToLogFile(DateTime.Now.ToString() + ", " + recCount.ToString() + "," + successCount.ToString() + "," + failedCount.ToString());
                    }**
                    catch (Exception)
                    {
                        failedCount++;
                    }   
                }
            }
        }

        private void backgroundWorker_Import_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            string msg = "";
            msg = msg + "Records successfully imported: " + successCount.ToString() + Environment.NewLine;
            msg = msg + "Records that failed to import: " + failedCount.ToString() + Environment.NewLine + Environment.NewLine;
            msg = msg + "Records excluded from import (20 minute 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;
            }**

2 个答案:

答案 0 :(得分:1)

您可以使用this topic中显示的File.WriteAllLines

及其'语法如下:

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

在你的情况下你会像这样使用它:

string logfileFileName = @"C:/luvi/logfile.txt";
File.WriteAllLines(logfileFileName, output);

注意:如果要使用File.AppendAllLines附加文件,则会覆盖该文件。


你需要实际调用你的方法,这可能是一个问题,因为我没有在你的代码中看到它。在以下更改中,我已将[{1}}替换为string msg,并添加了这些更改(您还可以使用列表和调用list.Add)。

array

答案 1 :(得分:0)

WriteToLogFile(string [] output)方法似乎有问题。你正在传递单个字符串,而它正在期待字符串的arrary。 catch block正在默默地失败。