C# 写 小小文本日志--记录一下

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

 

 代码如下:

 /// <summary>
        /// MyServiceLogPth : D:\ChangeHisService\  最后的斜线不能少哦
        /// </summary>
        /// <param name="fileName">123.txt</param>
        /// <param name="content">写入的内容</param>
        private static void writeLog(string fileName, string content)
        {
            if (!Directory.Exists(MyServiceLogPth))
            {
                //新建文件夹
                Directory.CreateDirectory(MyServiceLogPth);
            }
            //创建文件
            var RealFileName = MyServiceLogPth + fileName;
            if (!File.Exists(RealFileName))
            {
                FileStream file = new FileStream(RealFileName, FileMode.Create);
                file.Dispose();
            }
            using (FileStream stream = new FileStream(RealFileName, FileMode.Append))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine(content);
            }
        }

调用如下:

 writeLog(@"123.txt", JsonConvert.SerializeObject(jobResult));

存储路径如下:

        <!--日志地址 -->
        <add key="MyServiceLog" value="D:\ChangeHisService\" />

@天才卧龙的博客

posted @ 2021-08-12 16:03  天才卧龙  阅读(88)  评论(0编辑  收藏  举报