如何多次向文件写入内容

3

我想为我的应用程序编写调试日志文件。

我不想使用NuGet,因为我已经安装了log4net并且它引起了问题。

我想要编写一个简单的.log文件,以便我可以通过进程添加内容。

我需要像这样的东西:

System.IO.File.WriteAllText(path + ".log", "<some text>,");

 // some lines of code here

System.IO.File.WriteAllText(path + ".log", "<more text>");

当我打开日志文件时,我希望看到:<some text>,<more text>
2个回答

6

如果你想进行追加(而不是重写),那么请使用Append代替Write

 ...

 System.IO.File.AppendAllText(path + ".log", "<more text>");

1
你是想简单地添加内容吗?如果是这样,你可以直接使用:
System.IO.File.WriteAllText(path + ".log", "<some text>,");

 // some lines of code here

System.IO.File.AppendAllText(path + ".log", "<more text>");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接