c# - C#在文件中写入文本的非常奇怪的行为
问题描述
我在 C# 中使用以下代码在文件中记录一些文本...
class Logger{
public void log(String text)
{
System.IO.StreamWriter file = new System.IO.StreamWriter(System.IO.Directory.GetCurrentDirectory()+"\\log.txt", true);
file.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss: " + text.ToString()));
file.Close();
}
}
我这样称呼记录器:
Logger logger = new Logger();
logger.log("some text.");
一旦我运行程序,在控制台中似乎一切正常,但在文件中它看起来像:
- 2019-08-02 09:31:32: C:U32er32Danisourcerepo32Free+2e an2 RollouFree+2e an2 RolloubinDebud.C.Repo32ior19 1 Dev20190802_093132_56029iner7ace creae2!
- 2019-08-02 09:31:32: 7ree+2e au7 Dev bed.C.inn!
知道我必须在代码中更改什么吗?我希望文本是可读的:-)
谢谢您的帮助!
问候,丹尼尔
解决方案
这个:
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss: " + text.ToString())
需要是:
DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + ": " + text
您不想运行text
日期格式。
推荐阅读
- itext - 透明度组软蒙版和PNG图像蒙版的错误组合
- r - R中的正则表达式:在字符串开头提取一个单词直到一个特殊字符
- r - 使用`lapply`和`grepl`修剪变量内的嵌套列表
- php - PHP / Angular-为什么在用@if语句包围HTML之后我的视图中出现了意想不到的'<'?
- r - 如何将 data.table 转换为矩阵?
- javascript - PHP $$var 的 JS 等价物是什么?
- node.js - Webpack Typescript错误意外的令牌
- javascript - 如何在javascript中将相同的id对象添加到彼此中
- angular - 内容被加载两次 Angular
- javascript - 为什么我的 array.forEach() 没有中断迭代并返回“true”?