|
|
|
|
using log4net.Config;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace HighWayIot.Log4net
|
|
|
|
|
{
|
|
|
|
|
public class LogHelper
|
|
|
|
|
{
|
|
|
|
|
private static readonly Lazy<LogHelper> lazy = new Lazy<LogHelper>(() => new LogHelper());
|
|
|
|
|
public static LogHelper Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return lazy.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
|
|
|
|
|
private readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
|
|
|
|
|
private readonly log4net.ILog logView = log4net.LogManager.GetLogger("viewlog");
|
|
|
|
|
private readonly log4net.ILog sqllog = log4net.LogManager.GetLogger("sqllog");
|
|
|
|
|
private readonly log4net.ILog semaphorelog = log4net.LogManager.GetLogger("semaphorelog");
|
|
|
|
|
private readonly log4net.ILog logPlc = log4net.LogManager.GetLogger("plclog");
|
|
|
|
|
private readonly log4net.ILog logRfid = log4net.LogManager.GetLogger("rfidlog");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 配置文件路径
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
//private readonly string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
|
|
|
|
|
private readonly string fileName = "Z:\\Desktop\\日常代码\\HighWayIot\\HighWayIot.Log4net\\config\\log4net.config";
|
|
|
|
|
|
|
|
|
|
private LogHelper()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(fileName))
|
|
|
|
|
{
|
|
|
|
|
XmlConfigurator.Configure(new FileInfo(fileName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记录Info日志
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msg"></param>
|
|
|
|
|
/// <param name="ex"></param>
|
|
|
|
|
public void Info(string msg)
|
|
|
|
|
{
|
|
|
|
|
if (loginfo.IsInfoEnabled)
|
|
|
|
|
{
|
|
|
|
|
loginfo.Info(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记录PLC日志
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msg"></param>
|
|
|
|
|
public void PlcLog(string msg)
|
|
|
|
|
{
|
|
|
|
|
if (logPlc.IsInfoEnabled)
|
|
|
|
|
{
|
|
|
|
|
logPlc.Info(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记录Rfid日志
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msg"></param>
|
|
|
|
|
public void RfidLog(string msg)
|
|
|
|
|
{
|
|
|
|
|
if (logRfid.IsInfoEnabled)
|
|
|
|
|
{
|
|
|
|
|
logRfid.Info(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 界面日志
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msg"></param>
|
|
|
|
|
public void ViewLog(string msg)
|
|
|
|
|
{
|
|
|
|
|
if (logView.IsInfoEnabled)
|
|
|
|
|
{
|
|
|
|
|
logView.Info(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SqlLog(string msg)
|
|
|
|
|
{
|
|
|
|
|
if (sqllog.IsInfoEnabled)
|
|
|
|
|
{
|
|
|
|
|
sqllog.Info(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SemaphoreLog(string msg)
|
|
|
|
|
{
|
|
|
|
|
if (semaphorelog.IsInfoEnabled)
|
|
|
|
|
{
|
|
|
|
|
semaphorelog.Info(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 记录Error日志
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="errorMsg"></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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|