old_flsystem/类库/Api.Framework/Tools/LogHelper.cs

162 lines
5.6 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Api.Framework.Tools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Api.Framework.Utils
{
/// <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>
public 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, SQLMAN = 4 };
/// <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)
{ }
}
public void SqlMan(string logName, string logContent)
{
try
{
WriteLog(new LogParameter() { LogGrade = (int)LogType.SQLMAN, LogType = nameof(LogType.SQLMAN), 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;
if (_logParameter.LogType == "SQLMAN")
filename = CsharpHttpHelper.HttpExtend.MapFile($"{DateTime.Now.ToString("yyyy-MM-dd HH")}.log", "Cache\\SQLMAN");
else
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);
}
}
}