Files
6098/Cowain.Bake.Common/Core/LogHelper.cs

170 lines
5.3 KiB
C#
Raw Normal View History

using Cowain.Bake.Common.Enums;
using NLog;
using NLog.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace Cowain.Bake.Common.Core
{
public class LogHelper
{
private static LogHelper instance;
public static LogHelper Instance
{
get
{
if (instance == null)
{
instance = new LogHelper();
}
return instance;
}
}
private Logger logger; //初始化日志类
/// <summary>
/// 事件定义
/// </summary>
public Action<string> OnComplated;
public Action<string, bool, E_LogType> OnShowInvoke { set; get; }
/// <summary>
/// 时间触发
/// </summary>
/// <param name="msg">自定义消息</param>
/// <param name="isUI">是否触发事件通知</param>
private void Notice(string msg, bool isShow, E_LogType logType)
{
if (OnShowInvoke != null)
{
OnShowInvoke?.Invoke(msg, isShow, logType);
}
}
/// <summary>
/// 静态构造函数
/// </summary>
private LogHelper()
{
logger = NLog.LogManager.GetCurrentClassLogger(); //初始化日志类
//string rootPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//string fileName = string.Format(@"{0}NLog.config", rootPath);
////初始化配置日志
//NLog.LogManager.Configuration = new XmlLoggingConfiguration(fileName);
}
public void Trace(string msg)
{
To(msg, E_LogType.Trace, false);
}
public void Debug(string msg, bool isShow = false)
{
To(msg, E_LogType.Debug, isShow);
}
public void Info(string msg, bool isShow = false)
{
To(msg, E_LogType.Info, isShow);
}
public void Warn(string msg, bool isShow = false)
{
To(msg, E_LogType.Warn, isShow);
}
public void Error(string msg, bool isShow = false)
{
To(msg, E_LogType.Error, isShow);
}
public void Fatal(string msg, bool isShow = false)
{
To(msg, E_LogType.Fatal, isShow);
}
#region
public void GetCurrentClassDebug(string msg, bool isShow = false,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
msg = $"File:{sourceFilePath},Fun:{memberName},LineNum:{sourceLineNumber},{msg}";
To(msg, E_LogType.Debug, isShow);
}
public void GetCurrentClassInfo(string msg, bool isShow = false,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
msg = $"File:{sourceFilePath},Fun:{memberName},LineNum:{sourceLineNumber},{msg}";
To(msg, E_LogType.Info, isShow);
}
public void GetCurrentClassWarn(string msg, bool isShow = false,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
msg = $"File:{sourceFilePath},Fun:{memberName},LineNum:{sourceLineNumber},{msg}";
To(msg, E_LogType.Warn, isShow);
}
public void GetCurrentClassError(string msg, bool isShow = false,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
msg = $"File:{sourceFilePath},Fun:{memberName},LineNum:{sourceLineNumber},{msg}";
To(msg, E_LogType.Error, isShow);
}
public void GetCurrentClassFatal(string msg,bool isShow = false,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
msg = $"File:{sourceFilePath},Fun:{memberName},LineNum:{sourceLineNumber},{msg}";
To(msg, E_LogType.Fatal, isShow);
}
#endregion
private void To(string msg, E_LogType logType, bool isShow = false)
{
switch (logType)
{
case E_LogType.Debug:
logger.Debug(msg);
break;
case E_LogType.Info:
logger.Info(msg);
break;
case E_LogType.Warn:
logger.Warn(msg);
break;
case E_LogType.Error:
logger.Error(msg);
break;
case E_LogType.Fatal:
logger.Fatal(msg);
break;
default:
logger.Trace(msg);
break;
}
Notice(msg, isShow, logType);
}
}
}