Commit a8d6bd04 LN

日志打印修改

1 个父辈 7f50a6a4
...@@ -177,7 +177,7 @@ namespace OnlineStore.Common ...@@ -177,7 +177,7 @@ namespace OnlineStore.Common
} }
catch (Exception Ex) catch (Exception Ex)
{ {
LogUtil.error(LOGGER, Ex.ToString()); LogUtil.error(Ex.ToString());
//throw Ex; //throw Ex;
} }
return ok; return ok;
...@@ -268,7 +268,7 @@ namespace OnlineStore.Common ...@@ -268,7 +268,7 @@ namespace OnlineStore.Common
// catch (Exception ex) // catch (Exception ex)
// { // {
// _serialPort.DiscardOutBuffer(); // _serialPort.DiscardOutBuffer();
// LogUtil.error(LOGGER, "SendData ERROR:" + ex.ToString(), 21); // LogUtil.error( "SendData ERROR:" + ex.ToString(), 21);
// } // }
// } // }
...@@ -323,7 +323,7 @@ namespace OnlineStore.Common ...@@ -323,7 +323,7 @@ namespace OnlineStore.Common
catch (Exception ex) catch (Exception ex)
{ {
isOk = false; isOk = false;
LogUtil.error(LOGGER, "SendCommand ERROR:" + ex.ToString(), 20); LogUtil.error( "SendCommand ERROR:" + ex.ToString(), 20);
} }
finally finally
{ {
......
...@@ -85,11 +85,11 @@ namespace OnlineStore.Common ...@@ -85,11 +85,11 @@ namespace OnlineStore.Common
catch (WebException ex) catch (WebException ex)
{ {
IsTimeOut = true; IsTimeOut = true;
LogUtil.error(LOGGER, "POST WebException :" + ex.ToString(), 101); LogUtil.error( "POST WebException :" + ex.ToString(), 101);
} }
catch (Exception e) catch (Exception e)
{ {
LogUtil.error(LOGGER, "POST ERROR:" + e.ToString(), 1); LogUtil.error( "POST ERROR:" + e.ToString(), 1);
} }
if (isLog == 1) if (isLog == 1)
{ {
...@@ -151,7 +151,7 @@ namespace OnlineStore.Common ...@@ -151,7 +151,7 @@ namespace OnlineStore.Common
} }
if (!result.Contains("null") && result.Length != 0) if (!result.Contains("null") && result.Length != 0)
{ {
//LogUtil.debug(LOGGER,"receive << " + result); //LogUtil.debug( "receive << " + result);
} }
if (isLog == 1) if (isLog == 1)
{ {
......
...@@ -6,6 +6,7 @@ using log4net; ...@@ -6,6 +6,7 @@ using log4net;
using System.Reflection; using System.Reflection;
using System.Drawing; using System.Drawing;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Threading;
namespace OnlineStore.Common namespace OnlineStore.Common
{ {
...@@ -14,38 +15,35 @@ namespace OnlineStore.Common ...@@ -14,38 +15,35 @@ namespace OnlineStore.Common
public static readonly ILog AIOLog = LogManager.GetLogger("AIOBOXLog"); public static readonly ILog AIOLog = LogManager.GetLogger("AIOBOXLog");
private static LogUtil instance = new LogUtil(); private static LogUtil instance = new LogUtil();
public delegate void ShowLog(string msg, Color color); public delegate void ShowLog(string msg, Color color);
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public static readonly ILog LOGGER = LogManager.GetLogger("RollingLogFileAppender");
public static readonly ILog rfidLog = LogManager.GetLogger("RfidLog");
public static Dictionary<int, DateTime> lastErrorLogTime = new Dictionary<int, DateTime>(); public static Dictionary<int, DateTime> lastErrorLogTime = new Dictionary<int, DateTime>();
public static System.Windows.Forms.RichTextBox logBox = null; public static System.Windows.Forms.RichTextBox logBox = null;
public static int showCount = 10; public static int showCount = 15;
public static bool debug_opened = false; public static bool debug_opened = false;
public static void info(ILog log,string msg ) public static void info(ILog log, string msg)
{ {
if (log == null) if (log == null)
{ {
return; return;
} }
log.Info(msg);
log.Info( msg);
AddToBox(msg, Color.Black); AddToBox(msg, Color.Black);
//clear(); //clear();
} }
public static void info(ILog log,string msg, Color color) public static void info(ILog log, string msg, Color color)
{ {
log.Info( msg); log.Info(msg);
AddToBox(msg, color); AddToBox(msg, color);
} }
public static void debug(ILog log, string msg, Color color) public static void debug(ILog log, string msg, Color color)
{ {
log.Debug( msg); log.Debug(msg);
if (debug_opened) if (debug_opened)
{ {
AddToBox(msg, color); AddToBox(msg, color);
...@@ -53,72 +51,85 @@ namespace OnlineStore.Common ...@@ -53,72 +51,85 @@ namespace OnlineStore.Common
} }
public static void debug(ILog log, string msg) public static void debug(ILog log, string msg)
{ {
log.Debug( msg); log.Debug(msg);
if (debug_opened) if (debug_opened)
{ {
AddToBox(msg, Color.Gray); AddToBox(msg, Color.Gray);
} }
} }
private static List<string> lasErrorLogList = new List<string>();
private static int errCount = 5;
public static void error(ILog log, string errorMsg,int type,int spanSeconds=10) public static void error(string errorMsg, int type, int seconds = 10)
{
if (lastErrorLogTime.ContainsKey(type))
{ {
try try
{ {
if (lastErrorLogTime.ContainsKey(type))
{
TimeSpan span = DateTime.Now - lastErrorLogTime[type]; TimeSpan span = DateTime.Now - lastErrorLogTime[type];
if (span.TotalSeconds < spanSeconds) if (span.TotalSeconds > seconds)
{ {
return; lastErrorLogTime.Remove(type);
lastErrorLogTime.Add(type, DateTime.Now);
error(LOGGER, errorMsg);
}
} }
else else
{ {
lastErrorLogTime.Remove(type);
lastErrorLogTime.Add(type, DateTime.Now); lastErrorLogTime.Add(type, DateTime.Now);
error(log, errorMsg); error(LOGGER, errorMsg);
}
} }
}catch(Exception ex) catch (Exception ex)
{ {
LOGGER.Error(" 打印日志【" + type + "-" + errorMsg + "】出错:" + ex.ToString());
} }
} }
else public static void error(ILog log, string errorMsg, Exception ex = null)
{ {
lastErrorLogTime.Add(type, DateTime.Now); if (errorMsg.Trim().Equals("") && (ex == null))
error(log, errorMsg); {
return;
} }
if (ex == null)
{
log.Error(errorMsg);
} }
public static void error(ILog log, string errorMsg) else
{ {
log.Error( errorMsg); log.Error(errorMsg, ex);
}
AddToBox(errorMsg, Color.Red); AddToBox(errorMsg, Color.Red);
} }
private static object lockObj = "";
private static void AddToBox(string msg, Color color) private static void AddToBox(string msg, Color color)
{ {
if (Monitor.TryEnter(lockObj, 2))
{
try try
{ {
ShowLogPro(msg, color); ShowLogPro(msg, color);
} }
catch (Exception ex) catch (Exception ex)
{ {
LOGGER.Error("出错:" + ex.StackTrace); LOGGER.Error("出错:", ex);
}
finally
{
Monitor.Exit(lockObj);
}
}
else
{
LOGGER.Debug("ShowLogPro【" + msg + "】失败,未得到锁");
} }
} }
private static List<string> logList = new List<string>(); private static List<string> logList = new List<string>();
public static string LastText = ""; public static string LastText = "";
[HandleProcessCorruptedStateExceptions] [HandleProcessCorruptedStateExceptions]
private static void ShowLogPro(string msg, Color color) private static void ShowLogPro(string msg, Color color)
{ {
try try
{ {
if (logList.Count > 0)
{
// logList.RemoveAt(0);
}
if (logList.Count >= showCount) if (logList.Count >= showCount)
{ {
logList.RemoveAt(0); logList.RemoveAt(0);
...@@ -143,7 +154,7 @@ namespace OnlineStore.Common ...@@ -143,7 +154,7 @@ namespace OnlineStore.Common
logBox.AppendText(now.ToLongTimeString() + " " + msg + Environment.NewLine); //增加文本 logBox.AppendText(now.ToLongTimeString() + " " + msg + Environment.NewLine); //增加文本
TimeSpan span = DateTime.Now - lastTime; TimeSpan span = DateTime.Now - lastTime;
if (span.TotalSeconds > 1000) if (span.TotalSeconds > 3000)
{ {
lastTime = DateTime.Now; lastTime = DateTime.Now;
logBox.Select(logBox.Text.Length, 0); //设置光标的位置到文本尾 logBox.Select(logBox.Text.Length, 0); //设置光标的位置到文本尾
...@@ -153,7 +164,7 @@ namespace OnlineStore.Common ...@@ -153,7 +164,7 @@ namespace OnlineStore.Common
} }
catch (AccessViolationException e) catch (AccessViolationException e)
{ {
LOGGER.Error( " ShowLogPro 出错:" + e.ToString()); LOGGER.Error(" ShowLogPro 出错:" + e.ToString());
} }
catch (Exception ex) catch (Exception ex)
{ {
...@@ -166,6 +177,14 @@ namespace OnlineStore.Common ...@@ -166,6 +177,14 @@ namespace OnlineStore.Common
if (logBox != null && logBox.Visible) if (logBox != null && logBox.Visible)
{ {
logBox.Text = LastText; logBox.Text = LastText;
TimeSpan span = DateTime.Now - lastTime;
if (span.TotalSeconds > 3000000)
{
lastTime = DateTime.Now;
logBox.Select(logBox.Text.Length, 0); //设置光标的位置到文本尾
logBox.ScrollToCaret(); //滚动到控件光标处
}
} }
} }
public static void ClearLog() public static void ClearLog()
...@@ -182,16 +201,13 @@ namespace OnlineStore.Common ...@@ -182,16 +201,13 @@ namespace OnlineStore.Common
{ {
debug(LOGGER, msg); debug(LOGGER, msg);
} }
public static void error(string errorMsg) public static void error(string errorMsg, Exception ex = null)
{ {
error( LOGGER,errorMsg); error(LOGGER, errorMsg, ex);
} }
public static void info(string msg) public static void info(string msg)
{ {
info(LOGGER,msg ); info(LOGGER, msg);
} }
} }
......
...@@ -123,7 +123,7 @@ namespace OnlineStore.Common ...@@ -123,7 +123,7 @@ namespace OnlineStore.Common
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(LOGGER,"Connect to " + serverIP + ":" + serverPort + " fail!" + ex.ToString(),3); LogUtil.error("Connect to " + serverIP + ":" + serverPort + " fail!" + ex.ToString(),3);
m_clientSocket = null; m_clientSocket = null;
} }
...@@ -176,7 +176,7 @@ namespace OnlineStore.Common ...@@ -176,7 +176,7 @@ namespace OnlineStore.Common
catch (Exception ex) catch (Exception ex)
{ {
isInProcess = false; isInProcess = false;
LogUtil.error(LOGGER, "重连处理出错:" + ex.ToString(),9,180); LogUtil.error( "重连处理出错:" + ex.ToString(),9,180);
} }
} }
...@@ -227,7 +227,7 @@ namespace OnlineStore.Common ...@@ -227,7 +227,7 @@ namespace OnlineStore.Common
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(LOGGER,"socket received error:" + ex.ToString(),4); LogUtil.error("socket received error:" + ex.ToString(),4);
} }
} }
} }
......
...@@ -39,7 +39,7 @@ namespace OnlineStore.Common ...@@ -39,7 +39,7 @@ namespace OnlineStore.Common
} }
} }
} }
LogUtil.info(LOGGER, iplist + "]"); LogUtil.info( iplist + "]");
} }
/// <summary> /// <summary>
...@@ -59,7 +59,7 @@ namespace OnlineStore.Common ...@@ -59,7 +59,7 @@ namespace OnlineStore.Common
m_serverThread = new Thread(new ThreadStart(ReceiveAccept)); m_serverThread = new Thread(new ThreadStart(ReceiveAccept));
m_serverThread.Start(); m_serverThread.Start();
LogUtil.info(LOGGER, " Server start listen : " + m_serverPort); LogUtil.info( " Server start listen : " + m_serverPort);
logLocalIp(); logLocalIp();
//this.AddRunningInfo(">> " + DateTime.Now.ToString() + " Server started."); //this.AddRunningInfo(">> " + DateTime.Now.ToString() + " Server started.");
...@@ -101,14 +101,14 @@ namespace OnlineStore.Common ...@@ -101,14 +101,14 @@ namespace OnlineStore.Common
client.ClientSocket = m_serverSocket.Accept(); client.ClientSocket = m_serverSocket.Accept();
IPEndPoint clientipe = (IPEndPoint)client.ClientSocket.RemoteEndPoint; IPEndPoint clientipe = (IPEndPoint)client.ClientSocket.RemoteEndPoint;
client.ipAdd = clientipe.Address; client.ipAdd = clientipe.Address;
LogUtil.info(LOGGER, "有新的客户端链接上:" + client.ipAdd.ToString()); LogUtil.info( "有新的客户端链接上:" + client.ipAdd.ToString());
receiveMessageDelegate = new ReceiveMessageDelegate(ReceiveMessages); receiveMessageDelegate = new ReceiveMessageDelegate(ReceiveMessages);
receiveMessageDelegate.BeginInvoke(client, ReceiveMessagesCallback, ""); receiveMessageDelegate.BeginInvoke(client, ReceiveMessagesCallback, "");
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(LOGGER, ex.Message); LogUtil.error( ex.Message);
//throw new Exception(ex.Message); //throw new Exception(ex.Message);
} }
} }
...@@ -158,11 +158,11 @@ namespace OnlineStore.Common ...@@ -158,11 +158,11 @@ namespace OnlineStore.Common
} }
catch (SocketException e) catch (SocketException e)
{ {
LogUtil.error(LOGGER, e.ToString(), 6); LogUtil.error( e.ToString(), 6);
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(LOGGER, ex.ToString(), 5); LogUtil.error( ex.ToString(), 5);
} }
} }
//private void ReceiveMessages(Client client) //private void ReceiveMessages(Client client)
...@@ -180,10 +180,10 @@ namespace OnlineStore.Common ...@@ -180,10 +180,10 @@ namespace OnlineStore.Common
// if (!string.IsNullOrEmpty(strReceiveData)) // if (!string.IsNullOrEmpty(strReceiveData))
// { // {
// LogUtil.info(LOGGER, "收到数据:" + strReceiveData); // LogUtil.info( "收到数据:" + strReceiveData);
// // this.AddRunningInfo(">> Receive data from [" + client.ClientSocket.RemoteEndPoint.ToString()+ "]:" + strReceiveData); // // this.AddRunningInfo(">> Receive data from [" + client.ClientSocket.RemoteEndPoint.ToString()+ "]:" + strReceiveData);
// string strSendData = "OK. The content is:" + strReceiveData; // string strSendData = "OK. The content is:" + strReceiveData;
// //LogUtil.info(LOGGER, "OK. The content is:" + strReceiveData); // //LogUtil.info( "OK. The content is:" + strReceiveData);
// int sendBufferSize = Encoding.Unicode.GetByteCount(strSendData); // int sendBufferSize = Encoding.Unicode.GetByteCount(strSendData);
// byte[] sendBuffer = new byte[sendBufferSize]; // byte[] sendBuffer = new byte[sendBufferSize];
// sendBuffer = Encoding.Unicode.GetBytes(strSendData); // sendBuffer = Encoding.Unicode.GetBytes(strSendData);
...@@ -194,7 +194,7 @@ namespace OnlineStore.Common ...@@ -194,7 +194,7 @@ namespace OnlineStore.Common
// } // }
// catch (SocketException e) // catch (SocketException e)
// { // {
// LogUtil.error(LOGGER, e.ToString()); // LogUtil.error( e.ToString());
// } // }
// catch (Exception ex) // catch (Exception ex)
// { // {
......
...@@ -50,7 +50,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -50,7 +50,7 @@ namespace OnlineStore.DeviceLibrary
ACStorePosition position = param.GetACPosition(); ACStorePosition position = param.GetACPosition();
if (position == null) if (position == null)
{ {
LogUtil.error(LOGGER, StoreName + "出入库时发现param中取到的Position=null,没有库位不能执行出入库"); LogUtil.error( StoreName + "出入库时发现param中取到的Position=null,没有库位不能执行出入库");
return false; return false;
} }
...@@ -91,7 +91,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -91,7 +91,7 @@ namespace OnlineStore.DeviceLibrary
ACStorePosition position = param.GetACPosition(); ACStorePosition position = param.GetACPosition();
if (position == null) if (position == null)
{ {
LogUtil.error(LOGGER, StoreName + "出入库时发现param中取到的Position=null,没有库位不能执行出入库"); LogUtil.error( StoreName + "出入库时发现param中取到的Position=null,没有库位不能执行出入库");
return false; return false;
} }
if (param.PosInfo.PlateH <= 0) if (param.PosInfo.PlateH <= 0)
...@@ -195,7 +195,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -195,7 +195,7 @@ namespace OnlineStore.DeviceLibrary
} }
Alarm(StoreAlarmType.IoSingleTimeOut, io.ElectricalDefinition, WarnMsg, StoreMove.MoveType); Alarm(StoreAlarmType.IoSingleTimeOut, io.ElectricalDefinition, WarnMsg, StoreMove.MoveType);
LogUtil.error(LOGGER, StoreName + wait.IoType + "等待信号(" + io.DisplayStr + "=" + wait.IoValue + ") 超时", 14); LogUtil.error( StoreName + wait.IoType + "等待信号(" + io.DisplayStr + "=" + wait.IoValue + ") 超时", 14);
isOk = false; isOk = false;
break; break;
} }
...@@ -271,7 +271,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -271,7 +271,7 @@ namespace OnlineStore.DeviceLibrary
return; return;
} }
LogUtil.error(LOGGER, WarnMsg,100); LogUtil.error( WarnMsg,100);
Alarm(StoreAlarmType.IoSingleTimeOut, "", WarnMsg, StoreMove.MoveType); Alarm(StoreAlarmType.IoSingleTimeOut, "", WarnMsg, StoreMove.MoveType);
} }
} }
...@@ -295,12 +295,12 @@ namespace OnlineStore.DeviceLibrary ...@@ -295,12 +295,12 @@ namespace OnlineStore.DeviceLibrary
{ {
if (!LoadParamPosition(param)) if (!LoadParamPosition(param))
{ {
LogUtil.error(LOGGER, StoreName + " 启动入库【" + posId + "】出错,找不到库位信息"); LogUtil.error( StoreName + " 启动入库【" + posId + "】出错,找不到库位信息");
return; return;
} }
if (IOManager.IOValue(IO_Type.TrayCheck_Fixture).Equals(IO_VALUE.HIGH)) if (IOManager.IOValue(IO_Type.TrayCheck_Fixture).Equals(IO_VALUE.HIGH))
{ {
LogUtil.error(LOGGER, StoreName + " 启动入库【" + posId + "】出错,叉子料盘检测有料"); LogUtil.error( StoreName + " 启动入库【" + posId + "】出错,叉子料盘检测有料");
return; return;
} }
LogUtil.info(LOGGER, StoreName + " 启动入库【" + posId + "】", storeMoveColor); LogUtil.info(LOGGER, StoreName + " 启动入库【" + posId + "】", storeMoveColor);
...@@ -329,7 +329,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -329,7 +329,7 @@ namespace OnlineStore.DeviceLibrary
} }
else else
{ {
LogUtil.error(LOGGER, StoreName + " 启动【" + posId + "】入库出错,当前状态,storeStatus=" + storeRunStatus); LogUtil.error( StoreName + " 启动【" + posId + "】入库出错,当前状态,storeStatus=" + storeRunStatus);
} }
} }
...@@ -407,7 +407,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -407,7 +407,7 @@ namespace OnlineStore.DeviceLibrary
} }
WarnMsg = StoreName + "入库 等待料仓门口检测到料盘 [" + StoreMove.MoveParam.PosInfo.barcode + "] [" + StoreMove.MoveParam.PosInfo.PosId + "] 超时 [" + Math.Round(StoreMove.StepSpan().TotalSeconds, 1) + "]秒 "; WarnMsg = StoreName + "入库 等待料仓门口检测到料盘 [" + StoreMove.MoveParam.PosInfo.barcode + "] [" + StoreMove.MoveParam.PosInfo.PosId + "] 超时 [" + Math.Round(StoreMove.StepSpan().TotalSeconds, 1) + "]秒 ";
LogUtil.error(LOGGER, WarnMsg, 100); LogUtil.error( WarnMsg, 100);
Alarm(StoreAlarmType.IoSingleTimeOut, "", WarnMsg, StoreMove.MoveType); Alarm(StoreAlarmType.IoSingleTimeOut, "", WarnMsg, StoreMove.MoveType);
} }
} }
...@@ -591,12 +591,12 @@ namespace OnlineStore.DeviceLibrary ...@@ -591,12 +591,12 @@ namespace OnlineStore.DeviceLibrary
{ {
if (!LoadParamPosition(param)) if (!LoadParamPosition(param))
{ {
LogUtil.error(LOGGER, StoreName + " 启动出库【" + posId + "】出错,找不到库位信息"); LogUtil.error( StoreName + " 启动出库【" + posId + "】出错,找不到库位信息");
return false; return false;
} }
if (IOManager.IOValue(IO_Type.TrayCheck_Fixture).Equals(IO_VALUE.HIGH)) if (IOManager.IOValue(IO_Type.TrayCheck_Fixture).Equals(IO_VALUE.HIGH))
{ {
LogUtil.error(LOGGER, StoreName + " 启动出库【" + posId + "】出错,叉子料盘检测有料"); LogUtil.error( StoreName + " 启动出库【" + posId + "】出错,叉子料盘检测有料");
return false; return false;
} }
storeStatus = StoreStatus.OutStoreExecute; storeStatus = StoreStatus.OutStoreExecute;
...@@ -612,7 +612,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -612,7 +612,7 @@ namespace OnlineStore.DeviceLibrary
} }
else else
{ {
LogUtil.error(LOGGER, StoreName + " 启动出库出错,当前状态,storeStatus=" + storeRunStatus); LogUtil.error( StoreName + " 启动出库出错,当前状态,storeStatus=" + storeRunStatus);
} }
return false; return false;
} }
...@@ -873,7 +873,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -873,7 +873,7 @@ namespace OnlineStore.DeviceLibrary
} }
else else
{ {
LogUtil.error(LOGGER, StoreName + " 出库处理,moveStatus=" + StoreMove.MoveStep + ",没有对应的处理!"); LogUtil.error( StoreName + " 出库处理,moveStatus=" + StoreMove.MoveStep + ",没有对应的处理!");
} }
} }
...@@ -1115,7 +1115,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -1115,7 +1115,7 @@ namespace OnlineStore.DeviceLibrary
} }
catch (Exception ex) catch (Exception ex)
{ {
LogUtil.error(LOGGER, ex.ToString()); LogUtil.error( ex.ToString());
} }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!