簡體   English   中英

如何在VS2013中從WinForm生成日志(C#)

[英]How to generate logs from a winform in VS2013(c#)

我正在Visual Studio 2013(c#)中使用Winforms。 我有一個記錄所有事件的列表框,我需要將此日志也單獨存儲在記事本中,該怎么辦。 當我單擊一個按鈕時,該消息應分別存儲在列表框和記事本中。

private void button1_Click(object sender, EventArgs e)  
 {  
     if (button1.Text.Equals("DOOR OPEN"))  
        {  
            if (true == DescendingTime.Checked)  
          {  
            ListDataBox.Items.Insert(0, DateTime.Now + " Door Opened ...");  
                button1.Text = "DOOR CLOSE";  
                var path = "C:/Users/AP140563/Documents/Visual Studio2013/Projects/AMSimulator/AMSimulator/bin/Debug/log";    
               var message = "Door Opened ...";  
                System.IO.File.AppendAllLines(path, new string[]{message});  

            }  
            else  
            {
                ListDataBox.Items.Add(DateTime.Now + " Door Opened ...");
                button1.Text = "DOOR CLOSE";
            }
        }
        else if (button1.Text.Equals("DOOR CLOSE"))
        {
            if (true == DescendingTime.Checked)
            {
                ListDataBox.Items.Insert(0, DateTime.Now + " Door Closed ...");
                button1.Text = "DOOR OPEN";
            }
            else
            {
                ListDataBox.Items.Add(DateTime.Now + " Door Closed ...");
                button1.Text = "DOOR OPEN";
            }
        }
    }

在您的Log方法中,不要將項目添加到ListBox中,而是這樣做:

var path = "Path to your log file";
var message = "Message to log"
System.IO.File.AppendAllLines(path, new string[]{message});

路徑的示例可能是這樣的:

var path= System.IO.Path.Combine(Application.StartupPath, "Log.txt");

請記住,這只是如何將一行文本附加到文件的示例,而根本不是日志記錄系統。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM