|
|
|
|
using log4net.Config;
|
|
|
|
|
using log4net;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
#region << 版 本 注 释 >>
|
|
|
|
|
/*--------------------------------------------------------------------
|
|
|
|
|
* 版权所有 (c) 2024 WenJY 保留所有权利。
|
|
|
|
|
* CLR版本:4.0.30319.42000
|
|
|
|
|
* 机器名称:T14-GEN3-7895
|
|
|
|
|
* 命名空间:SlnMesnac.RfidUpload.NLog
|
|
|
|
|
* 唯一标识:63646c4f-5bfd-4936-a480-7c23460ba2c0
|
|
|
|
|
*
|
|
|
|
|
* 创建者:WenJY
|
|
|
|
|
* 电子邮箱:
|
|
|
|
|
* 创建时间:2024-11-05 10:34:55
|
|
|
|
|
* 版本:V1.0.0
|
|
|
|
|
* 描述:
|
|
|
|
|
*
|
|
|
|
|
*--------------------------------------------------------------------
|
|
|
|
|
* 修改人:
|
|
|
|
|
* 时间:
|
|
|
|
|
* 修改说明:
|
|
|
|
|
*
|
|
|
|
|
* 版本:V1.0.0
|
|
|
|
|
*--------------------------------------------------------------------*/
|
|
|
|
|
#endregion << 版 本 注 释 >>
|
|
|
|
|
namespace SlnMesnac.RfidUpload.NLog
|
|
|
|
|
{
|
|
|
|
|
public class LogHelper
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private static readonly Lazy<LogHelper> _lazy = new Lazy<LogHelper>(() => new LogHelper());
|
|
|
|
|
|
|
|
|
|
public static LogHelper Instance => _lazy.Value;
|
|
|
|
|
|
|
|
|
|
private LogHelper()
|
|
|
|
|
{
|
|
|
|
|
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
|
|
|
|
|
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
|
|
|
|
|
}
|
|
|
|
|
public readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
|
|
|
|
|
public readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记录Info日志
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msg"></param>
|
|
|
|
|
public void Info(string msg)
|
|
|
|
|
{
|
|
|
|
|
if (loginfo.IsInfoEnabled)
|
|
|
|
|
{
|
|
|
|
|
loginfo.Info(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记录Error日志
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info"></param>
|
|
|
|
|
/// <param name="ex"></param>
|
|
|
|
|
public void Error(string info, Exception? ex = null)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(info) && ex == null)
|
|
|
|
|
{
|
|
|
|
|
logerror.ErrorFormat("【附加信息】 : {0}<br>", new object[] { info });
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(info) && ex != null)
|
|
|
|
|
{
|
|
|
|
|
string errorMsg = BeautyErrorMsg(ex);
|
|
|
|
|
logerror.ErrorFormat("【附加信息】 : {0}<br>{1}", new object[] { info, errorMsg });
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrEmpty(info) && ex != null)
|
|
|
|
|
{
|
|
|
|
|
string errorMsg = BeautyErrorMsg(ex);
|
|
|
|
|
logerror.Error(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 美化错误信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ex">异常</param>
|
|
|
|
|
/// <returns>错误信息</returns>
|
|
|
|
|
private string BeautyErrorMsg(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
string errorMsg = string.Format("【异常类型】:{0} <br>【异常信息】:{1} <br>【堆栈调用】:{2}", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace });
|
|
|
|
|
errorMsg = errorMsg.Replace("\r\n", "<br>");
|
|
|
|
|
errorMsg = errorMsg.Replace("位置", "<strong style=\"color:red\">位置</strong>");
|
|
|
|
|
return errorMsg;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|