123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- using System.IO;
- using System.Threading.Tasks;
- namespace ZLPlugin_LisPacs_MR.Domain.Units
- {
- /// <summary>
- /// 自定义日志类
- /// </summary>
- class Log
- {
- /// <summary>
- /// 日志路径
- /// </summary>
- static string Path { get; set; }
- /// <summary>
- /// 记录日志
- /// </summary>
- /// <param name="info"></param>
- /// <param name="isNewline">是否换行(目前体现排版的空行)</param>
- public static void Info(string info, bool isNewline = false)
- {
- if (string.IsNullOrEmpty(Path) || Path?.Length == 0)
- GetPath();
- var file = Path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".Log";
- using (var sw = new StreamWriter(file, true, Encoding.UTF8))
- {
- sw.WriteLine(DateTime.Now.ToString() + "=====>" + info);
- if (isNewline)
- sw.WriteLine();
- sw.Flush();
- }
- }
- /// <summary>
- /// 获取日志路径(以部件名称为文件夹名称)
- /// </summary>
- /// <returns></returns>
- public static string GetPath()
- {
- var file = "C:\\zlPlugIn_调试信息";
- if (!Directory.Exists(file))
- Directory.CreateDirectory(file);
- Path = file + "\\" + Assembly.GetExecutingAssembly().GetName().Name;
- if (!Directory.Exists(Path))
- Directory.CreateDirectory(Path);
- return Path;
- }
- /// <summary>
- /// 异步删除日志任务
- /// </summary>
- /// <returns></returns>
- public static Task ClearLogAsyn(int Day = 20)
- {
- return new Task(() =>
- {
- var files = new DirectoryInfo(GetPath()).GetFiles("*.Log");
- files.ToList().ForEach(n =>
- {
- if ((DateTime.Now - n.LastAccessTime).TotalDays > Day)
- n.Delete();
- });
- });
- }
- }
- }
|