十年河东,十年河西,莫欺少年穷
学无止境,精益求精
代码如下:
/// <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\" />