充电机日志拆分

zw
rszn 6 months ago
parent f9671aa275
commit 65d5c9c3d9

@ -28,10 +28,14 @@ namespace Service.Charger.Client;
[Scope("InstancePerDependency")] [Scope("InstancePerDependency")]
public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder> public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(ChargerClient));
#region 属性 #region 属性
/// <summary>
/// 充电机编号
/// </summary>
public string Sn { get; set; }
public ushort AuthTimes { get; set; } = 0; public ushort AuthTimes { get; set; } = 0;
public bool IsAuthed { get; set; } = false; public bool IsAuthed { get; set; } = false;
@ -187,6 +191,11 @@ public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
_binInfoRepository = binInfoRepository; _binInfoRepository = binInfoRepository;
} }
private ILog Log()
{
return LogManager.GetLogger("charger_" + this.Sn);
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -253,8 +262,7 @@ public class ChargerClient : TcpClient<IBaseHandler, Decoder, Encoder>
chargeOrderNo = ChargerUtils.GenChargeOrderSn(); chargeOrderNo = ChargerUtils.GenChargeOrderSn();
} }
Log().Info(
Log.Info(
$"SendRemoteStartCharging soc={socLimit}, changePower={changePower}, changePowerCmdType={changePowerCmdType}, chargeOrderNo={chargeOrderNo}"); $"SendRemoteStartCharging soc={socLimit}, changePower={changePower}, changePowerCmdType={changePowerCmdType}, chargeOrderNo={chargeOrderNo}");
var remoteStartCharging = new RemoteStartCharging(socLimit, changePowerCmdType, changePower, chargeOrderNo); var remoteStartCharging = new RemoteStartCharging(socLimit, changePowerCmdType, changePower, chargeOrderNo);
CurrentCmd = JsonConvert.SerializeObject(remoteStartCharging, Formatting.Indented) + "\r\n" + CurrentCmd = JsonConvert.SerializeObject(remoteStartCharging, Formatting.Indented) + "\r\n" +

@ -15,7 +15,7 @@ namespace Service.Charger.Client;
/// <summary> /// <summary>
/// 示例程序 /// 示例程序
/// </summary> /// </summary>
[Scope("SingleInstance")] [Scope]
public static class ClientMgr public static class ClientMgr
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(ClientMgr)); private static readonly ILog Log = LogManager.GetLogger(typeof(ClientMgr));
@ -91,6 +91,7 @@ public static class ClientMgr
Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}"); Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}");
ChargerClient client = AppInfo.Container.Resolve<ChargerClient>(); ChargerClient client = AppInfo.Container.Resolve<ChargerClient>();
client.AutoReconnect = true; client.AutoReconnect = true;
client.Sn = netInfo.Code;
client.BinNo = binInfo?.No; client.BinNo = binInfo?.No;
client.BatteryNo = binInfo?.BatteryNo; client.BatteryNo = binInfo?.BatteryNo;
client.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort)); client.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort));

@ -16,12 +16,19 @@ namespace Service.Charger.Codec;
public class Decoder : ByteToMessageDecoder public class Decoder : ByteToMessageDecoder
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(Decoder));
private readonly IByteBuffer[] _delimiters = { Unpooled.CopiedBuffer(ChargerConst.StartChar) }; private readonly IByteBuffer[] _delimiters = { Unpooled.CopiedBuffer(ChargerConst.StartChar) };
private ILog Log(string? chargerSn)
{
if (ObjUtils.IsNotNullOrWhiteSpace(chargerSn))
{
LogManager.GetLogger("charger_" + chargerSn);
}
return LogManager.GetLogger(typeof(Decoder));
}
protected override void Decode(IChannelHandlerContext context, IByteBuffer buffer, List<object> output) protected override void Decode(IChannelHandlerContext context, IByteBuffer buffer, List<object> output)
{ {
string? chargerSn = ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn);
IByteBuffer? delimiter = FindDelimiter(buffer); IByteBuffer? delimiter = FindDelimiter(buffer);
if (delimiter != null) if (delimiter != null)
{ {
@ -62,13 +69,13 @@ public class Decoder : ByteToMessageDecoder
try try
{ {
ASDU asdu = Parse(buffer, totalFrameLength, out data); ASDU asdu = Parse(buffer, totalFrameLength, out data);
Log.Info($"receive {JsonConvert.SerializeObject(asdu)}:{data} from {ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn)}"); Log(chargerSn).Info($"receive {BitUtls.BytesToHexStr(data)}:{JsonConvert.SerializeObject(asdu)} from {chargerSn}");
output.Add(asdu); output.Add(asdu);
buffer.Clear(); buffer.Clear();
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error($"decode fail msg={BitUtls.BytesToHexStr(data)}"); Log(chargerSn).Error($"decode fail msg={BitUtls.BytesToHexStr(data)}");
} }
} }
} }

@ -15,7 +15,15 @@ namespace Service.Charger.Codec;
/// </summary> /// </summary>
public class Encoder : MessageToByteEncoder<APCI> public class Encoder : MessageToByteEncoder<APCI>
{ {
private static readonly ILog Log = LogManager.GetLogger(typeof(Encoder)); private ILog Log(string? chargerSn)
{
if (ObjUtils.IsNotNullOrWhiteSpace(chargerSn))
{
LogManager.GetLogger("charger_" + chargerSn);
}
return LogManager.GetLogger(typeof(Decoder));
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -25,6 +33,7 @@ public class Encoder : MessageToByteEncoder<APCI>
protected override void Encode(IChannelHandlerContext context, APCI obj, IByteBuffer output) protected override void Encode(IChannelHandlerContext context, APCI obj, IByteBuffer output)
{ {
string? s = ChannelUtils.GetAttr(context.Channel, ChargerConst.DestAddr); string? s = ChannelUtils.GetAttr(context.Channel, ChargerConst.DestAddr);
string? sn = ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn);
if (s != null) if (s != null)
{ {
byte[] destAddr = s.Split(",").Select(b => Convert.ToByte(b)).ToArray(); byte[] destAddr = s.Split(",").Select(b => Convert.ToByte(b)).ToArray();
@ -32,13 +41,7 @@ public class Encoder : MessageToByteEncoder<APCI>
} }
byte[] bytes = obj.ToBytes(); byte[] bytes = obj.ToBytes();
Log.Info($"send {JsonConvert.SerializeObject(obj)}:{BitUtls.BytesToHexStr(bytes)} to {ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn)}"); Log(sn).Info($"send {BitUtls.BytesToHexStr(bytes)}:{JsonConvert.SerializeObject(obj)} to {ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn)}");
//TODO::仅测试使用
//string ou = string.Empty;
//for(int i = 0; i < bytes.Length; i++)
//{
// ou += bytes[i].ToString("X2") + " ";
//}
output.WriteBytes(bytes); output.WriteBytes(bytes);
} }

@ -1,32 +1,179 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<log4net> <log4net>
<!-- 控制台日志配置 --> <!-- 控制台日志配置 -->
<appender name="Console" type="log4net.Appender.ConsoleAppender"> <appender name="Console" type="log4net.Appender.ConsoleAppender">
<!-- 日志输出格式 --> <!-- 日志输出格式 -->
<layout type="log4net.Layout.PatternLayout"> <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level [%thread] (%file:%line) - %message%newline"/> <conversionPattern value="%date %5level [%thread] (%file:%line) - %message%newline" />
</layout> </layout>
</appender> </appender>
<!-- 文件存储日志配置 --> <!-- 文件存储日志配置 -->
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<!-- 保存文件的名称 --> <!-- 保存文件的名称 -->
<file value="logs\WebApi.log"/> <file value="logs\WebApi.log" />
<appendToFile value="true"/> <appendToFile value="true" />
<!-- 文件的编码方式 --> <!-- 文件的编码方式 -->
<param name="Encoding" value="UTF-8"/> <param name="Encoding" value="UTF-8" />
<!-- 每个文件的大小 --> <!-- 每个文件的大小 -->
<maximumFileSize value="100MB"/> <maximumFileSize value="100MB" />
<!-- 保存文件数量 --> <!-- 保存文件数量 -->
<maxSizeRollBackups value="2"/> <maxSizeRollBackups value="2" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!-- 日志输出格式 --> <!-- 日志输出格式 -->
<layout type="log4net.Layout.PatternLayout"> <layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline"/> <conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout> </layout>
</appender> </appender>
<logger name="Charger1" additivity="false">
<level value="ALL" />
<appender-ref ref="Charger1" />
</logger>
<appender name="Charger1" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Charger\" />
<param name="AppendToFile" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<Encoding value="UTF-8" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd/HH&quot;Charger1.log&quot;" />
<param name="maximumFileSize" value="200MB" />
<param name="MaxSizeRollBackups" value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="Charger2" additivity="false">
<level value="ALL" />
<appender-ref ref="Charger2" />
</logger>
<appender name="Charger2" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Charger\" />
<param name="AppendToFile" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<Encoding value="UTF-8" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd/HH&quot;Charger2.log&quot;" />
<param name="maximumFileSize" value="200MB" />
<param name="MaxSizeRollBackups" value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="Charger3" additivity="false">
<level value="ALL" />
<appender-ref ref="Charger3" />
</logger>
<appender name="Charger3" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Charger\" />
<param name="AppendToFile" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<Encoding value="UTF-8" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd/HH&quot;Charger3.log&quot;" />
<param name="maximumFileSize" value="200MB" />
<param name="MaxSizeRollBackups" value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="Charger4" additivity="false">
<level value="ALL" />
<appender-ref ref="Charger4" />
</logger>
<appender name="Charger4" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Charger\" />
<param name="AppendToFile" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<Encoding value="UTF-8" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd/HH&quot;Charger4.log&quot;" />
<param name="maximumFileSize" value="200MB" />
<param name="MaxSizeRollBackups" value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="Charger5" additivity="false">
<level value="ALL" />
<appender-ref ref="Charger5" />
</logger>
<appender name="Charger5" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Charger\" />
<param name="AppendToFile" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<Encoding value="UTF-8" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd/HH&quot;Charger5.log&quot;" />
<param name="maximumFileSize" value="200MB" />
<param name="MaxSizeRollBackups" value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="Charger6" additivity="false">
<level value="ALL" />
<appender-ref ref="Charger6" />
</logger>
<appender name="Charger6" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Charger\" />
<param name="AppendToFile" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<Encoding value="UTF-8" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd/HH&quot;Charger6.log&quot;" />
<param name="maximumFileSize" value="200MB" />
<param name="MaxSizeRollBackups" value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="Charger7" additivity="false">
<level value="ALL" />
<appender-ref ref="Charger7" />
</logger>
<appender name="Charger7" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Charger\" />
<param name="AppendToFile" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<Encoding value="UTF-8" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd/HH&quot;Charger7.log&quot;" />
<param name="maximumFileSize" value="200MB" />
<param name="MaxSizeRollBackups" value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<logger name="Charger8" additivity="false">
<level value="ALL" />
<appender-ref ref="Charger8" />
</logger>
<appender name="Charger8" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Charger\" />
<param name="AppendToFile" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<Encoding value="UTF-8" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value="yyyyMMdd/HH&quot;Charger8.log&quot;" />
<param name="maximumFileSize" value="200MB" />
<param name="MaxSizeRollBackups" value="5" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<root> <root>
<level value="ALL"/> <level value="ALL" />
<appender-ref ref="Console"/> <appender-ref ref="Console" />
<appender-ref ref="RollingFile"/> <appender-ref ref="RollingFile" />
</root> </root>
</log4net> </log4net>
Loading…
Cancel
Save