ECGPrint/1200Gxml心电图绘制/服务/LogHelper.cs
2024-12-25 17:24:22 +08:00

129 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace
{
/// <summary>
/// 日志类
/// </summary>
public sealed class LogHelper
{
public static int level = 3;
public static string path = AppDomain.CurrentDomain.BaseDirectory + "\\Content\\Log\\";
/// <summary>
/// 记录日志
/// </summary>
/// <param name="title"></param>
/// <param name="content"></param>
public static void Info(string title, string content)
{
Task.Factory.StartNew(() =>
{
new LogHelper().Log("info", title, content);
});
}
/// <summary>
/// 记录调试日志
/// </summary>
/// <param name="title"></param>
/// <param name="content"></param>
public static void Debug(string title, string content)
{
Task.Factory.StartNew(() =>
{
new LogHelper().Log("debug", title, content);
});
}
/// <summary>
/// 记录错误日志
/// </summary>
/// <param name="title"></param>
/// <param name="content"></param>
public static void Error(string title, string content)
{
Task.Factory.StartNew(() =>
{
new LogHelper().Log("error", title, content);
});
}
/// <summary>
/// 记录异常日志
/// </summary>
/// <param name="type"></param>
/// <param name="ex"></param>
public static void Error(string type, Exception ex)
{
Task.Factory.StartNew(() =>
{
new LogHelper().Log(type, ex);
});
}
/// <summary>
/// 记录一般日志
/// </summary>
/// <param name="type"></param>
/// <param name="title"></param>
/// <param name="content"></param>
private void Log(string type, string title, string content)
{
string name = string.Concat(DateTime.Now.ToString("yyyyMMdd"), ".TXT");
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
string write_content = $"[{time}][{type}][{title}][{content}]";
using (FileStream fs = new FileStream(path + name, FileMode.Append, FileAccess.Write))
{
using (StreamWriter w = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8")))
{
w.WriteLine(write_content);
}
}
}
/// <summary>
/// 记录异常错误日志
/// </summary>
/// <param name="type"></param>
/// <param name="ex"></param>
private void Log(string type, Exception ex)
{
string name = string.Concat(DateTime.Now.ToString("yyyyMMdd"), ".error.config");
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
using (FileStream fs = new FileStream(path + name, FileMode.Append, FileAccess.Write))
{
using (StreamWriter w = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8")))
{
w.WriteLine("------------------------------------------------------------------");
w.WriteLine(string.Concat("[1.位置:", type, "]"));
w.WriteLine(string.Concat("[2.时间:", time, "]"));
w.WriteLine(string.Concat("[4.实例:", ex.InnerException, "]"));
w.WriteLine(string.Concat("[5.来源:", ex.Source, "]"));
w.WriteLine(string.Concat("[6.方法:", ex.TargetSite, "]"));
w.WriteLine(string.Concat("[7.堆栈:", ex.StackTrace, "]"));
w.WriteLine(string.Concat("[8.提示:", ex.Message, "]"));
w.WriteLine("------------------------------------------------------------------");
}
}
}
}
}