您好,我想将用户定义的异常写入日志文件。因此,我不想抛出我的异常,而是想将该消息记录到一个 txt 文件中。
我的异常的构造函数如下所示:
public OpenFileException(string pathToOpen, Exception innerexception)
: base("Couldn't find the path: " + pathToOpen, innerexception)
{
this.pathToOpen = pathToOpen;
}
这就是我目前记录异常的方式:
try
{
string data = Read(txtLocation.Text);
txtInfo.Text = data;
}
catch (Exception ex)
{
WriteLog("[" + DateTime.Now + "]" + " " + ex.Message);
MessageBox.Show(" ");
throw new OpenFileException(txtLocation.Text, ex);
}
所以我要问的是。如何将我的字符串“找不到路径:”记录到 txt 文件中?