using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaterialTraceability.Common
{
///
/// 统一日志工具类
///
public class LogHelper
{
public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
public static readonly log4net.ILog logView = log4net.LogManager.GetLogger("viewlog");
// 日志优化,四个,调用地方注释掉
public static readonly log4net.ILog sqllog = log4net.LogManager.GetLogger("sqllog");
public static readonly log4net.ILog semaphorelog = log4net.LogManager.GetLogger("semaphorelog");
public static readonly log4net.ILog logPlc = log4net.LogManager.GetLogger("plclog");
public static readonly log4net.ILog logRfid = log4net.LogManager.GetLogger("rfidlog");
///
/// 记录Info日志
///
///
///
public static void Info(string msg)
{
if (loginfo.IsInfoEnabled)
{
loginfo.Info(msg);
}
}
///
/// 记录PLC日志
///
///
public static void PlcLog(string msg)
{
if (logPlc.IsInfoEnabled)
{
logPlc.Info(msg);
}
}
///
/// 记录Rfid日志
///
///
public static void RfidLog(string msg)
{
if (logRfid.IsInfoEnabled)
{
logRfid.Info(msg);
}
}
///
/// 界面日志
///
///
public static void ViewLog(string msg)
{
/*if (logView.IsInfoEnabled)
{
logView.Info(msg);
}*/
}
public static void SqlLog(string msg)
{
if (sqllog.IsInfoEnabled)
{
sqllog.Info(msg);
}
}
public static void SemaphoreLog(string msg)
{
/*if (semaphorelog.IsInfoEnabled)
{
semaphorelog.Info(msg);
}*/
}
///
/// 记录Error日志
///
///
///
public static void Error(string info, Exception ex = null)
{
if (!string.IsNullOrEmpty(info) && ex == null)
{
logerror.ErrorFormat("【附加信息】 : {0}
", new object[] { info });
}
else if (!string.IsNullOrEmpty(info) && ex != null)
{
string errorMsg = BeautyErrorMsg(ex);
logerror.ErrorFormat("【附加信息】 : {0}
{1}", new object[] { info, errorMsg });
}
else if (string.IsNullOrEmpty(info) && ex != null)
{
string errorMsg = BeautyErrorMsg(ex);
logerror.Error(errorMsg);
}
}
///
/// 美化错误信息
///
/// 异常
/// 错误信息
private static string BeautyErrorMsg(Exception ex)
{
string errorMsg = string.Format("【异常类型】:{0}
【异常信息】:{1}
【堆栈调用】:{2}", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace });
errorMsg = errorMsg.Replace("\r\n", "
");
errorMsg = errorMsg.Replace("位置", "位置");
return errorMsg;
}
}
}