目前,我有一个序列化程序,它将异常写入错误日志文件(.txt) . 我的代码如下:

CLASS TO BE SERIALIZED

public class ErrorLog
{
    private string error;

    public void SetError(string exception) 
    {
        var time = DateTime.Now;
        error = "\n" + time  + "\n" + exception;
    }
}

SERIALIZER METHOD (is in another class)

public void SerializeError(string fileName) 
{
    FileStream fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write);
    BinaryFormatter binForm = new BinaryFormatter();
    binForm.Serialize(fileStream, errorlog);
    fileStream.Close();
}

其中 errorlog 是对 ErrorLog 类的引用 .

但是,当我检查生成的.txt时,它会忽略 error 字符串中的 \n . 我试过把 \r\n 但它也没用 . 将这些新行添加到序列化信息的最佳方法是什么?

提前致谢!