148 lines
4.9 KiB
C#
148 lines
4.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace PCRobot
|
||
{
|
||
/// <summary>
|
||
/// 日志参数类
|
||
/// </summary>
|
||
internal sealed class LogParameter
|
||
{
|
||
/// <summary>
|
||
/// 日志等级
|
||
/// </summary>
|
||
public int LogGrade { get; set; }
|
||
|
||
/// <summary>
|
||
/// 日志类型
|
||
/// </summary>
|
||
public string LogType { get; set; }
|
||
|
||
/// <summary>
|
||
/// 日志记录类名和方法名(className/methodName)
|
||
/// </summary>
|
||
public string LogName { get; set; }
|
||
|
||
/// <summary>
|
||
/// 日志记录文本内容
|
||
/// </summary>
|
||
public string LogContent { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 自定义之日助手
|
||
/// </summary>
|
||
internal sealed class LogHelper
|
||
{
|
||
#region 单利模式
|
||
//创建私有静态字段,接收类的实例化对象
|
||
private static readonly LogHelper _LogHelper = null;
|
||
//构造函数私有化
|
||
internal LogHelper() { }
|
||
//静态构造函数,创建单利对象资源
|
||
static LogHelper()
|
||
{
|
||
_LogHelper = new LogHelper();
|
||
}
|
||
|
||
//获取单利对象资源
|
||
public static LogHelper GetSingleObj()
|
||
{
|
||
return _LogHelper;
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 日志等级
|
||
/// </summary>
|
||
private enum LogType { ERROR = 1, INFO = 2, DEBUG = 3 };
|
||
|
||
/// <summary>
|
||
/// 指定日志文件夹(项目跟路劲文件夹)
|
||
/// </summary>
|
||
public string Path { get; set; }
|
||
|
||
/// <summary>
|
||
/// 向日志文件写入调试信息
|
||
/// </summary>
|
||
/// <param name="logName">类名/方法名</param>
|
||
/// <param name="logContent">日志记录内容</param>
|
||
public void Debug(string logName, string logContent)
|
||
{
|
||
try
|
||
{
|
||
WriteLog(new LogParameter() { LogGrade = (int)LogType.DEBUG, LogType = nameof(LogType.DEBUG), LogName = logName, LogContent = logContent });
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 向日志文件写入运行时信息
|
||
/// </summary>
|
||
/// <param name="logName">类名/方法名</param>
|
||
/// <param name="logContent">日志记录内容</param>
|
||
public void Info(string logName, string logContent)
|
||
{
|
||
WriteLog(new LogParameter() { LogGrade = (int)LogType.INFO, LogType = nameof(LogType.INFO), LogName = logName, LogContent = logContent });
|
||
}
|
||
|
||
/// <summary>
|
||
/// 向日志文件写入出错信息
|
||
/// </summary>
|
||
/// <param name="logName">类名/方法名</param>
|
||
/// <param name="logContent">日志记录内容</param>
|
||
public void Error(string logName, string logContent)
|
||
{
|
||
WriteLog(new LogParameter() { LogGrade = (int)LogType.ERROR, LogType = nameof(LogType.ERROR), LogName = logName, LogContent = logContent });
|
||
}
|
||
|
||
/// <summary>
|
||
/// 实际的写日志操作
|
||
/// </summary>
|
||
/// <param name="logParameter">日志参数model</param>
|
||
private void WriteLog(LogParameter _logParameter)
|
||
{
|
||
var logParameter = _logParameter;
|
||
//var m = new Action(delegate ()
|
||
//Task.Factory.StartNew(delegate ()
|
||
//{
|
||
try
|
||
{
|
||
string filename = string.Empty;
|
||
filename = CsharpHttpHelper.HttpExtend.MapFile($"{DateTime.Now.ToString("yyyy-MM-dd HH")}.log", $"Cache\\{_logParameter.LogType}");
|
||
|
||
#region 原始写法
|
||
//创建或打开日志文件,向日志文件末尾追加记录
|
||
//StreamWriter mySw = File.AppendText(filename);
|
||
|
||
//向日志文件写入内容
|
||
//string writeContent = time + "|" + typeGrade + ":" + type + "|" + className + ":" + content;
|
||
//mySw.WriteLine(writeContent);
|
||
|
||
//关闭日志文件
|
||
//mySw.Close();
|
||
#endregion
|
||
|
||
//(优化写法)创建或打开日志文件,向日志文件末尾追加记录,关闭日志文件
|
||
using (StreamWriter mySw = File.AppendText(filename))
|
||
{
|
||
string writeContent = $"{DateTime.Now.ToString("HH:mm:ss")}---{logParameter.LogGrade}|{logParameter.LogType}|{logParameter.LogName}---{logParameter.LogContent}";
|
||
mySw.WriteLine(writeContent);//向日志文件写入内容
|
||
mySw.Close(); //关闭日志文件
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
//});
|
||
//m.BeginInvoke(null, null);
|
||
}
|
||
}
|
||
|
||
}
|