using System;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Threading.Tasks;
namespace ZLPlugin_LisPacs_MR.Domain.Units
{
///
/// 自定义日志类
///
class Log
{
///
/// 日志路径
///
static string Path { get; set; }
///
/// 记录日志
///
///
/// 是否换行(目前体现排版的空行)
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();
}
}
///
/// 获取日志路径(以部件名称为文件夹名称)
///
///
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;
}
///
/// 异步删除日志任务
///
///
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();
});
});
}
}
}