diff --git a/SlnMesnac.Rfid/ApiServer.cs b/SlnMesnac.Rfid/ApiServer.cs index f48d206..3c70ab9 100644 --- a/SlnMesnac.Rfid/ApiServer.cs +++ b/SlnMesnac.Rfid/ApiServer.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Models; using Nancy; +using Serilog; using SlnMesnac.Model.AirportApiEntity; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; @@ -126,7 +127,7 @@ namespace SlnMesnac.TouchSocket // 设置标志位为 true,表示方法正在执行 iFlag = true; _RefreshLogMessageAction?.Invoke("接收MES主动读取指令" + JsonSerializer.Serialize(mesReadPara)); - _logger.Info("接收MES主动读取指令"+ JsonSerializer.Serialize(mesReadPara)); + Log.Information("接收MES主动读取指令"+ JsonSerializer.Serialize(mesReadPara)); string FilterData = rfidAbsractFactory.Find(x => x.ConfigKey == mesReadPara.EQUID).FilterData; List rfid = await rfidAbsractFactory.Find(x => x.ConfigKey == mesReadPara.EQUID).GetRFIDAsync(); @@ -179,7 +180,7 @@ namespace SlnMesnac.TouchSocket Msg = Msg, }; _RefreshLogMessageAction?.Invoke("发送MES主动读取指令" + JsonSerializer.Serialize(response)); - _logger.Info("发送MES主动读取指令"+ JsonSerializer.Serialize(response)); + Log.Information("发送MES主动读取指令"+ JsonSerializer.Serialize(response)); Real_mesdata real_Mesdata = new Real_mesdata() { Combineid = mesReadPara.EQUID.ToString(), @@ -199,7 +200,7 @@ namespace SlnMesnac.TouchSocket } catch (Exception ex) { - _logger.Error("read接口异常" + ex.ToString()); + Log.Information("read接口异常" + ex.ToString()); return response; } finally @@ -228,7 +229,7 @@ namespace SlnMesnac.TouchSocket ApiResponse response = new ApiResponse(); _RefreshLogMessageAction?.Invoke("接收MES控制报警灯指令" + JsonSerializer.Serialize(mesAlarmLightPara)); - _logger.Info("接收MES控制报警灯指令"+ JsonSerializer.Serialize(mesAlarmLightPara)); + Log.Information("接收MES控制报警灯指令"+ JsonSerializer.Serialize(mesAlarmLightPara)); if (mesAlarmLightPara.Code == "0") { @@ -253,7 +254,7 @@ namespace SlnMesnac.TouchSocket Msg = "sucess", }; _RefreshLogMessageAction?.Invoke("发送MES控制报警灯指令" + JsonSerializer.Serialize(response)); - _logger.Info("发送MES控制报警灯指令"+ JsonSerializer.Serialize(response)); + Log.Information("发送MES控制报警灯指令"+ JsonSerializer.Serialize(response)); Real_mesdata real_Mesdata = new Real_mesdata() { Combineid = mesAlarmLightPara.EQUID.ToString(), @@ -279,7 +280,7 @@ namespace SlnMesnac.TouchSocket { _RefreshLogMessageAction?.Invoke("接收Web下发频段设置指令" + Deviceid + ","+ Para); - _logger.Info("接收Web下发频段设置指令" + Deviceid + "," + Para); + Log.Information("接收Web下发频段设置指令" + Deviceid + "," + Para); ApiResponse response = new ApiResponse(); if (!rfidAbsractFactory.Exists(x => x.deviceid == Deviceid)) { @@ -322,7 +323,7 @@ namespace SlnMesnac.TouchSocket { _RefreshLogMessageAction?.Invoke("接收Web下发获取频段指令" + Deviceid); - _logger.Info("接收Web下发获取频段指令" + Deviceid); + Log.Information("接收Web下发获取频段指令" + Deviceid); string Frequency = "China1"; ApiResponse response = new ApiResponse(); //设备编号验证 @@ -368,7 +369,7 @@ namespace SlnMesnac.TouchSocket string Return = ""; _RefreshLogMessageAction?.Invoke("接收Web下发获取过滤数据指令" + Deviceid); - _logger.Info("接收Web下发获取过滤数据指令" + Deviceid); + Log.Information("接收Web下发获取过滤数据指令" + Deviceid); ApiResponse response = new ApiResponse(); //设备编号验证 if (!rfidAbsractFactory.Exists(x => x.deviceid == Deviceid)) @@ -413,7 +414,7 @@ namespace SlnMesnac.TouchSocket { _RefreshLogMessageAction?.Invoke("接收Web下发设置过滤数据指令" + Deviceid + "," + Para); - _logger.Info("接收Web下发设置过滤数据指令" + Deviceid + "," + Para); + Log.Information("接收Web下发设置过滤数据指令" + Deviceid + "," + Para); ApiResponse response = new ApiResponse(); if (!rfidAbsractFactory.Exists(x => x.deviceid == Deviceid)) { @@ -457,7 +458,7 @@ namespace SlnMesnac.TouchSocket string Return = ""; _RefreshLogMessageAction?.Invoke("接收Web下发获取设备功率指令" + Deviceid); - _logger.Info("接收Web下发获取设备功率指令" + Deviceid); + Log.Information("接收Web下发获取设备功率指令" + Deviceid); ApiResponse response = new ApiResponse(); //设备编号验证 if (!rfidAbsractFactory.Exists(x => x.deviceid == Deviceid)) @@ -502,7 +503,7 @@ namespace SlnMesnac.TouchSocket { _RefreshLogMessageAction?.Invoke("接收Web下发设置设备功率指令" + Deviceid + "," + Para); - _logger.Info("接收Web下发设置设备功率指令" + Deviceid + "," + Para); + Log.Information("接收Web下发设置设备功率指令" + Deviceid + "," + Para); ApiResponse response = new ApiResponse(); if (!rfidAbsractFactory.Exists(x => x.deviceid == Deviceid)) { diff --git a/SlnMesnac.Rfid/Factory/FuchsFactory.cs b/SlnMesnac.Rfid/Factory/FuchsFactory.cs index 2b8e86c..eeeacd2 100644 --- a/SlnMesnac.Rfid/Factory/FuchsFactory.cs +++ b/SlnMesnac.Rfid/Factory/FuchsFactory.cs @@ -1,4 +1,5 @@ -using SlnMesnac.Common; +using Serilog; +using SlnMesnac.Common; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; using SlnMesnac.Rfid.Dto; @@ -70,7 +71,7 @@ namespace SlnMesnac.Rfid.Factory if (e.Memory.Span.ToArray()[3] == 0x02) { - _logger.Info($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}"); + Log.Information($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}"); byte[] resultBuffer = PareReceiveBufferData(receivedBuffer, receivedBuffer.Length); List tagInfoList = Device_DealTagInfoList(resultBuffer); string info = ""; @@ -87,7 +88,7 @@ namespace SlnMesnac.Rfid.Factory } else { - _logger.Info($"{m_deviceID},IP:{m_strIP}接收原始报文{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}"); + Log.Information($"{m_deviceID},IP:{m_strIP}接收原始报文{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}"); } //MesParaData mesParaData = new MesParaData() @@ -171,17 +172,17 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - //_logger.Info($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + //Log.Information($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); Filter = Filter + "\r\n"; //byte[] reciveBuffer = await waitClient.SendThenReturnAsync(Filter + "\r\n", 2000); using (var responsedData = await waitClient.SendThenResponseAsync(Filter, 2000)) { var memory = responsedData.Memory; string result = Encoding.GetEncoding("GBK").GetString(memory.ToArray(), 0, memory.Length); - _logger.Info($"{m_deviceID}接收设置过滤指令{result}"); + Log.Information($"{m_deviceID}接收设置过滤指令{result}"); return true; } - //_logger.Info($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + //Log.Information($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); } return iflag; } @@ -210,7 +211,7 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - //_logger.Info($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + //Log.Information($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); //byte[] reciveBuffer = await waitClient.SendThenReturnAsync("filter?\r\n", 2000); @@ -218,7 +219,7 @@ namespace SlnMesnac.Rfid.Factory { var memory = responsedData.Memory; string result = Encoding.GetEncoding("GBK").GetString(memory.ToArray(), 0, memory.Length); - _logger.Info($"{m_deviceID}接收设置过滤指令{result}"); + Log.Information($"{m_deviceID}接收设置过滤指令{result}"); return result; } return iflag; @@ -265,11 +266,11 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - _logger.Info($"{m_deviceID}发送设置频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送设置频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收设置频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收设置频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x44) { iflag = true; @@ -318,11 +319,11 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - _logger.Info($"{m_deviceID}发送获取频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送获取频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收获取频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收获取频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x74) { byte bDB = reciveBuffer[5]; @@ -692,7 +693,7 @@ namespace SlnMesnac.Rfid.Factory iFirstPC = iFirstAnt + 1; iFirstLeftBarcketPos = iFirstLeftBarcketPos + iBarcodeLength + 5; _RefreshLogMessageAction?.Invoke($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",\r\n读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); - _logger.Info($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); + Log.Information($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); iCommonSecondFlag++; if (iCommonSecondFlag == iBarcodeGroupCount) { diff --git a/SlnMesnac.Rfid/Factory/RflyFactory.cs b/SlnMesnac.Rfid/Factory/RflyFactory.cs index 0e842e8..8fbbbe1 100644 --- a/SlnMesnac.Rfid/Factory/RflyFactory.cs +++ b/SlnMesnac.Rfid/Factory/RflyFactory.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Models; +using Serilog; using SlnMesnac.Common; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; @@ -127,7 +128,7 @@ namespace SlnMesnac.Rfid.Factory { DealMessageData(receivedBuffer); } - //_logger.Info($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}"); + //Log.Information($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}"); //byte[] resultBuffer = PareReceiveBufferData(receivedBuffer, receivedBuffer.Length); //List tagInfoList = Device_DealTagInfoList(resultBuffer); //string info = ""; @@ -144,7 +145,7 @@ namespace SlnMesnac.Rfid.Factory } else { - _logger.Info($"{m_deviceID},IP:{m_strIP}接收原始报文{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}"); + Log.Information($"{m_deviceID},IP:{m_strIP}接收原始报文{_stringChange.bytesToHexStr(receivedBuffer, receivedBuffer.Length)}"); } } @@ -160,7 +161,7 @@ namespace SlnMesnac.Rfid.Factory } catch (Exception e) { - _logger.Error($"{m_deviceID},IP:{m_strIP}数据接收异常" + e.Message); + Log.Information($"{m_deviceID},IP:{m_strIP}数据接收异常" + e.Message); return false; } } @@ -218,10 +219,10 @@ namespace SlnMesnac.Rfid.Factory { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}发送读取指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送读取指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length); - _logger.Info($"{m_deviceID}接收读取指令{_stringChange.bytesToHexStr(resultBuffer, resultBuffer.Length)}"); + Log.Information($"{m_deviceID}接收读取指令{_stringChange.bytesToHexStr(resultBuffer, resultBuffer.Length)}"); tagInfoList = Device_DealTagInfoList(resultBuffer); @@ -232,7 +233,7 @@ namespace SlnMesnac.Rfid.Factory } catch (Exception e) { - _logger.Error($"{m_deviceID}数据接收异常"+ e); + Log.Information($"{m_deviceID}数据接收异常"+ e); return tagInfoList; //throw new InvalidOperationException($"{m_strIP}按时间段盘点异常:{e.Message}"); } @@ -366,11 +367,11 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - _logger.Info($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x42) { return true; @@ -421,7 +422,7 @@ namespace SlnMesnac.Rfid.Factory using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x72) { byte[] bDB = new byte[2]; @@ -463,7 +464,7 @@ namespace SlnMesnac.Rfid.Factory } catch (Exception ex) { - _logger.Error(ex.Message); + Log.Information(ex.Message); } } @@ -499,11 +500,11 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - _logger.Info($"{m_deviceID}发送停止连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送停止连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收停止连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收停止连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x12) { return true; @@ -551,11 +552,11 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - _logger.Info($"{m_deviceID}发送连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送连续盘点指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收连续盘点指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x11) { return true; @@ -646,11 +647,11 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - _logger.Info($"{m_deviceID}发送写入指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送写入指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收写入指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收写入指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x03 && reciveBuffer[4] == 0x00) { return true; @@ -967,7 +968,7 @@ namespace SlnMesnac.Rfid.Factory iFirstPC = iFirstAnt + 1; iFirstLeftBarcketPos = iFirstLeftBarcketPos + iBarcodeLength + 5; _RefreshLogMessageAction?.Invoke($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",\r\n读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); - _logger.Info($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); + Log.Information($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); iCommonSecondFlag++; if (iCommonSecondFlag == iBarcodeGroupCount) { @@ -987,7 +988,7 @@ namespace SlnMesnac.Rfid.Factory catch (Exception ex) { //mutauto.ReleaseMutex(); - _logger.Error($"{m_deviceID},IP:{m_strIP}Device_AutoDealContent异常" + ex.Message); + Log.Information($"{m_deviceID},IP:{m_strIP}Device_AutoDealContent异常" + ex.Message); return tagInfoList; //throw new InvalidOperationException($"Device_AutoDealContent 自动处理函数异常:{ex.Message}"); } @@ -1072,7 +1073,7 @@ namespace SlnMesnac.Rfid.Factory iFirstPC = iFirstAnt + 1; iFirstLeftBarcketPos = iFirstLeftBarcketPos + iBarcodeLength + 5; _RefreshLogMessageAction?.Invoke($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",\r\n读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); - _logger.Info($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); + Log.Information($"----{m_deviceID}第[" + (iCommonSecondFlag + 1) + "]次数据解析为:" + tag.EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]"); iCommonSecondFlag++; if (iCommonSecondFlag == iBarcodeGroupCount) { @@ -1092,7 +1093,7 @@ namespace SlnMesnac.Rfid.Factory catch (Exception ex) { //mutauto.ReleaseMutex(); - _logger.Error($"{m_deviceID},IP:{m_strIP}Device_AutoDealContent异常" + ex.Message); + Log.Information($"{m_deviceID},IP:{m_strIP}Device_AutoDealContent异常" + ex.Message); return tagInfoList; //throw new InvalidOperationException($"Device_AutoDealContent 自动处理函数异常:{ex.Message}"); } @@ -1132,17 +1133,17 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - //_logger.Info($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + //Log.Information($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); Filter = Filter + "\r\n"; //byte[] reciveBuffer = await waitClient.SendThenReturnAsync(Filter + "\r\n", 2000); using (var responsedData = await waitClient.SendThenResponseAsync(Filter, 2000)) { var memory = responsedData.Memory; string result = Encoding.GetEncoding("GBK").GetString(memory.ToArray(), 0, memory.Length); - _logger.Info($"{m_deviceID}接收设置过滤指令{result}"); + Log.Information($"{m_deviceID}接收设置过滤指令{result}"); return true; } - //_logger.Info($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + //Log.Information($"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); } return iflag; } @@ -1174,7 +1175,7 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - //_logger.Info($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + //Log.Information($"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); //byte[] reciveBuffer = await waitClient.SendThenReturnAsync("filter?\r\n", 2000); @@ -1182,7 +1183,7 @@ namespace SlnMesnac.Rfid.Factory { var memory = responsedData.Memory; string result = Encoding.GetEncoding("GBK").GetString(memory.ToArray(), 0, memory.Length); - _logger.Info($"{m_deviceID}接收设置过滤指令{result}"); + Log.Information($"{m_deviceID}接收设置过滤指令{result}"); return result; } return iflag; @@ -1233,11 +1234,11 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - _logger.Info($"{m_deviceID}发送设置频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送设置频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收设置频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收设置频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x44) { iflag = true; @@ -1290,11 +1291,11 @@ namespace SlnMesnac.Rfid.Factory return false; } }); - _logger.Info($"{m_deviceID}发送获取频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); + Log.Information($"{m_deviceID}发送获取频段指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}"); using (var responsedData = await waitClient.SendThenResponseAsync(pMessagePack.m_pData, 2000)) { var reciveBuffer = responsedData.Memory.ToArray(); - _logger.Info($"{m_deviceID}接收获取频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID}接收获取频段指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); if (reciveBuffer[3] == 0x74) { byte bDB = reciveBuffer[5]; @@ -1320,7 +1321,7 @@ namespace SlnMesnac.Rfid.Factory //连续盘点返回 if (reciveBuffer[3] == 0x01) { - _logger.Info($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length); List tagInfoList = Device_LXDealTagInfoList(resultBuffer); string info = ""; @@ -1339,7 +1340,7 @@ namespace SlnMesnac.Rfid.Factory //按时间段盘点返回 if (reciveBuffer[3] == 0x02) { - _logger.Info($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); + Log.Information($"{m_deviceID},IP:{m_strIP}接收自报{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}"); byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length); List tagInfoList = Device_DealTagInfoList(resultBuffer); string info = ""; @@ -1358,7 +1359,7 @@ namespace SlnMesnac.Rfid.Factory } catch (Exception ex) { - _logger.Error($"》》》设备:{m_deviceID},IP:{m_strIP},处理数据异常:{ex}"); + Log.Information($"》》》设备:{m_deviceID},IP:{m_strIP},处理数据异常:{ex}"); } } public override async Task SendHeartPack() diff --git a/SlnMesnac.Rfid/MeshttpClient.cs b/SlnMesnac.Rfid/MeshttpClient.cs index 60df340..65f013c 100644 --- a/SlnMesnac.Rfid/MeshttpClient.cs +++ b/SlnMesnac.Rfid/MeshttpClient.cs @@ -124,7 +124,7 @@ namespace SlnMesnac.TouchSocket if (responseValue != null) { Console.WriteLine("MES接口返回:" + JsonSerializer.Serialize(responseValue)); - // _logger.Info("MES接口返回:" + JsonSerializer.Serialize(responseValue)); + // Log.Information("MES接口返回:" + JsonSerializer.Serialize(responseValue)); rFIDDeviceApiResponse = JsonSerializer.Deserialize>(responseValue); Console.WriteLine("MES接口返回成功"); return responseValue; @@ -139,7 +139,7 @@ namespace SlnMesnac.TouchSocket { rFIDDeviceApiResponse.Code = 202; Console.WriteLine(ex.ToString()); - _logger.Error(ex.Message); + Log.Information(ex.Message); return "202"; } @@ -174,14 +174,14 @@ namespace SlnMesnac.TouchSocket JToken responseValue = await MESHttpClient.InvokeTAsync("POST:/autoread", null, requestValue); _RefreshLogMessageAction?.Invoke("发送MES自动读取指令"+JsonSerializer.Serialize(requestValue)); - _logger.Info("发送MES自动读取指令"+ JsonSerializer.Serialize(requestValue)); + Log.Information("发送MES自动读取指令"+ JsonSerializer.Serialize(requestValue)); RefreshStateEvent?.Invoke(requestValue.EQUID,requestValue.EPCID); return JTokenToEntity>(responseValue); } catch (Exception ex) { - _logger.Info("autoread ERROR: " + ex.Message); + Log.Information("autoread ERROR: " + ex.Message); return new ApiResponse(); } diff --git a/SlnMesnac.Serilog/SerilogExtensions.cs b/SlnMesnac.Serilog/SerilogExtensions.cs index 622d7f7..889d189 100644 --- a/SlnMesnac.Serilog/SerilogExtensions.cs +++ b/SlnMesnac.Serilog/SerilogExtensions.cs @@ -48,25 +48,38 @@ namespace SlnMesnac.Serilog // 使用实例数据路径下的Logs目录 logPath = Path.Combine(appConfig.InstanceDataPath, "Logs"); } - + Console.WriteLine($"[DEBUG] Serilog 日志路径: {logPath}"); + Console.WriteLine($"[DEBUG] InstanceDataPath: {appConfig?.InstanceDataPath}"); // 确保日志目录存在 EnsureLogDirectories(logPath); #endregion - + // Log.Logger = new LoggerConfiguration() + //.MinimumLevel.Information() + //.Enrich.WithProperty("InstanceId", appConfig?.InstanceId ?? "Unknown") + //.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {InstanceId}] {Message:lj}{NewLine}{Exception}") + //// ✅ 先用最简单的配置测试,去掉所有 Filter + //.WriteTo.File( + // Path.Combine(logPath, "app.log"), + // rollingInterval: RollingInterval.Day, + // shared: true) + //.CreateLogger(); // 构建日志配置,包含实例ID作为上下文 Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() + .Enrich.WithProperty("InstanceId", appConfig?.InstanceId ?? "Unknown") .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {InstanceId}] {Message:lj}{NewLine}{Exception}") .WriteTo.Logger(lc => lc - .Filter.ByIncludingOnly(logEvent => - logEvent.Properties.ContainsKey("Module") && - logEvent.Properties["Module"].ToString().Contains("Info")) + //.Filter.ByIncludingOnly(logEvent => + // logEvent.Properties.ContainsKey("Module") && + // logEvent.Properties["Module"].ToString().Contains("Info")) + .WriteTo.File( Path.Combine($"{logPath}/Info/", "Info.log"), rollingInterval: RollingInterval.Day, outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Level:u3} {InstanceId}] {Message:lj}{NewLine}{Exception}", + shared: true, retainedFileCountLimit: 7)) // 保留最近7天的日志文件 .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(logEvent => @@ -76,6 +89,7 @@ namespace SlnMesnac.Serilog Path.Combine($"{logPath}/Data/", "Data.log"), rollingInterval: RollingInterval.Day, outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Level:u3} {InstanceId}] {Message:lj}{NewLine}{Exception}", + shared: true, retainedFileCountLimit: 7)) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(logEvent => @@ -85,6 +99,7 @@ namespace SlnMesnac.Serilog Path.Combine($"{logPath}/Error/", "Error.log"), rollingInterval: RollingInterval.Day, outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Level:u3} {InstanceId}] {Message:lj}{NewLine}{Exception}", + shared: true, retainedFileCountLimit: 30)) // 错误日志保留更长时间 .CreateLogger(); } diff --git a/SlnMesnac.TouchSocket/TcpServer.cs b/SlnMesnac.TouchSocket/TcpServer.cs index f52af82..25a17f6 100644 --- a/SlnMesnac.TouchSocket/TcpServer.cs +++ b/SlnMesnac.TouchSocket/TcpServer.cs @@ -1,4 +1,5 @@ -using SlnMesnac.Common; +using Serilog; +using SlnMesnac.Common; using SlnMesnac.Model.dto; using SlnMesnac.Model.Enum; using SlnMesnac.Serilog; @@ -73,7 +74,7 @@ namespace SlnMesnac.TouchSocket { _service.Connecting = (client, e) => { - _logger.Info($"客户端{client.IP}正在接入服务"); + Log.Information($"客户端{client.IP}正在接入服务"); if (!client.IsClient) { e.Id = $"{client.IP}:{client.Port}"; @@ -82,13 +83,13 @@ namespace SlnMesnac.TouchSocket }; _service.Connected = (client, e) => { - _logger.Info($"客户端{client.IP}接入服务成功"); + Log.Information($"客户端{client.IP}接入服务成功"); RefreshClientInfoEvent?.Invoke(_service); return EasyTask.CompletedTask; }; _service.Closed = (client, e) => { - _logger.Info($"客户端{client.IP}断开连接"); + Log.Information($"客户端{client.IP}断开连接"); RefreshClientInfoEvent?.Invoke(_service); return EasyTask.CompletedTask; }; @@ -124,7 +125,7 @@ namespace SlnMesnac.TouchSocket //自定义插件 })); _service.StartAsync(); - _logger.Info($"TcpServer启动成功,监听端口:{serverPort}"); + Log.Information($"TcpServer启动成功,监听端口:{serverPort}"); Task.Run(async delegate { //await Task.Delay(1000 * 20); @@ -134,7 +135,7 @@ namespace SlnMesnac.TouchSocket catch (Exception ex) { //throw new InvalidOperationException($"TcpServer启动异常:{ex.Message}"); - _logger.Error($"TcpServer启动异常:{ex.Message}"); + Log.Information($"TcpServer启动异常:{ex.Message}"); } } public byte DealMessage(byte[] bytes) @@ -216,7 +217,7 @@ namespace SlnMesnac.TouchSocket } catch (Exception ex) { - _logger.Error($"SendHeartBeat异常:{ex.Message}"); + Log.Information($"SendHeartBeat异常:{ex.Message}"); } Thread.Sleep(1000 * 5 * 1); @@ -273,12 +274,12 @@ namespace SlnMesnac.TouchSocket await tcpSessionClient.SendAsync(readOnlyMemory); } #endregion - _logger.Info($"{pMessagePack.iStationId}发送原始报文{_stringChange.bytesToHexStr(SendBuffer, SendBuffer.Length)}"); + Log.Information($"{pMessagePack.iStationId}发送原始报文{_stringChange.bytesToHexStr(SendBuffer, SendBuffer.Length)}"); } } catch (Exception ex) { - _logger.Error($"SendMessage异常:{ex.Message}"); + Log.Information($"SendMessage异常:{ex.Message}"); } } diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index edbebc5..efbee28 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -90,25 +90,27 @@ namespace SlnMesnac.WPF // 创建ServiceProvider ServiceProvider = services.BuildServiceProvider(); ServiceProvider.UseTouchSocketExtensions(); - // 配置Serilog和其他扩展 - ServiceProvider.UseSerilogExtensions(); + // 获取AppConfig并更新实例特定的配置 - var appConfig = ServiceProvider.GetService(); - if (appConfig != null) - { - // 更新日志路径为实例专用路径 - appConfig.logPath = Path.Combine(_instanceDataPath, "Logs"); - Directory.CreateDirectory(appConfig.logPath); + //var appConfig = ServiceProvider.GetService(); + //if (appConfig != null) + //{ + // // 更新日志路径为实例专用路径 + // appConfig.logPath = Path.Combine(_instanceDataPath, "Logs"); + // Directory.CreateDirectory(appConfig.logPath); - // 更新其他可能需要实例化的路径 - appConfig.InstanceId = _instanceId; - appConfig.InstanceDataPath = _instanceDataPath; + // // 更新其他可能需要实例化的路径 + // appConfig.InstanceId = _instanceId; + // appConfig.InstanceDataPath = _instanceDataPath; - // 根据实例ID调整数据库连接(如果需要) - // AdjustDatabaseConnectionForInstance(appConfig); - } + // // 根据实例ID调整数据库连接(如果需要) + // // AdjustDatabaseConnectionForInstance(appConfig); + //} + + // 配置Serilog和其他扩展 + ServiceProvider.UseSerilogExtensions(); // 显示主窗口 var loginWindow = ServiceProvider.GetRequiredService(); loginWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen; @@ -120,7 +122,7 @@ namespace SlnMesnac.WPF serilog = ServiceProvider.GetRequiredService(); serilog.Info($"应用程序启动 - 实例ID: {_instanceId}, 进程ID: {Process.GetCurrentProcess().Id}"); - serilog.Info($"日志目录: {appConfig?.logPath}"); + //serilog.Info($"日志目录: {appConfig?.logPath}"); //serilog.Data($"数据目录: {_instanceDataPath}"); //serilog.Error($"日志目录: {appConfig?.logPath}"); } @@ -400,12 +402,11 @@ namespace SlnMesnac.WPF .AsSelf() .WithSingletonLifetime()); - services.Scan(scan => scan - .FromAssemblyOf() - .AddClasses(classes => classes.WithAttribute()) - .AsSelf() - .WithTransientLifetime()); - //services.AddSingleton>(); + //services.Scan(scan => scan + // .FromAssemblyOf() + // .AddClasses(classes => classes.WithAttribute()) + // .AsSelf() + // .WithTransientLifetime()); // 注册ORM services.AddSqlSugarSetup(); diff --git a/SlnMesnac.WPF/MainWindow.xaml.cs b/SlnMesnac.WPF/MainWindow.xaml.cs index d0b0999..38c5a40 100644 --- a/SlnMesnac.WPF/MainWindow.xaml.cs +++ b/SlnMesnac.WPF/MainWindow.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Serilog; using SlnMesnac.Model.AirportApiEntity; using SlnMesnac.Serilog; using SlnMesnac.TouchSocket; @@ -68,12 +69,12 @@ namespace SlnMesnac.WPF private void Window_Loaded(object sender, RoutedEventArgs e) { - _logger.Info("系统启动!"); + Log.Information("系统启动!"); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { - _logger.Info("系统关闭!"); + Log.Information("系统关闭!"); } diff --git a/SlnMesnac.WPF/Page/IndexPage/ProductionLineUserControl.xaml b/SlnMesnac.WPF/Page/IndexPage/ProductionLineUserControl.xaml index 530886b..4521e51 100644 --- a/SlnMesnac.WPF/Page/IndexPage/ProductionLineUserControl.xaml +++ b/SlnMesnac.WPF/Page/IndexPage/ProductionLineUserControl.xaml @@ -125,7 +125,7 @@