2014-06-19 45 views
0

我有c#應用程序,它在查詢運行後在消息框上顯示消息。同時我希望它寫入日誌文件。這是我的嘗試,但沒有運氣。我的日誌文件是空的。 它創建了一個空文件。當backgroundWorker作業完成時寫入日誌文件

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; 
      }** 
+0

程序是否成功運行?你是否面臨任何錯誤? – LakshmiNarayanan

+0

它沒有運行錯誤,並顯示消息框成功和失敗的記錄數。我需要在日誌文件中顯示相同的信息。 – shruthi

+0

考慮在使用FileStream和StreamWriter時使用[using](http://msdn.microsoft.com/zh-cn/en-en/library/yh598w02.aspx)語句。 – EKrueger

回答

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


您需要真正地調用您的方法,這可能是一個問題,因爲我沒有在您的代碼中看到它。在以下更改中,我將string msg替換爲array,並添加了這些(您也可以使用列表和調用list.Add)。

private void backgroundWorker_Import_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    string[] msg = new string[] {}; 
    msg[0] = "Records successfully imported: " + successCount.ToString(); 
    msg[1] = "Records that failed to import: " + failedCount.ToString(); 
    msg[2] = "Records excluded from import (20 minute grace-period): " + (tblVehicles.Rows.Count - successCount - failedCount).ToString(); 

    // Write to log! 
    WriteToLogFile(msg); 

    // Show to 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); 
} 
+0

感謝Matthijs,但我收到了這個錯誤'找不到隱式類型數組的最佳類型[] msg = new [] {}; – shruthi

+0

@shruthi:現在看看它是否有效。 – Matthijs

+0

異常已被調用的目標引發.- Application.Run(new Form1()); – shruthi

0

它似乎與WriteToLogFile(字符串[]輸出)方法的問題。你正在傳遞單個字符串,而它正期待字符串。 catch塊正在悄然失敗。