@@ -70,9 +70,10 @@ namespace JT808.DotNetty.Tcp.Handlers | |||
JT808HeaderPackage jT808HeaderPackage = JT808Serializer.Deserialize<JT808HeaderPackage>(msg); | |||
jT808AtomicCounterService.MsgSuccessIncrement(); | |||
jT808TrafficService.ReceiveSize(msg.Length); | |||
if (logger.IsEnabled(LogLevel.Debug)) | |||
if (logger.IsEnabled(LogLevel.Trace)) | |||
{ | |||
logger.LogDebug("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); | |||
logger.LogTrace("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); | |||
logger.LogTrace("accept msg <<< " + ByteBufferUtil.HexDump(msg)); | |||
} | |||
jT808SessionManager.TryAdd(jT808HeaderPackage.Header.TerminalPhoneNo,ctx.Channel); | |||
if (handler.HandlerDict.TryGetValue(jT808HeaderPackage.Header.MsgId, out var handlerFunc)) | |||
@@ -0,0 +1,36 @@ | |||
<?xml version="1.0" encoding="utf-8" ?> | |||
<!-- | |||
参考:http://www.cnblogs.com/fuchongjundream/p/3936431.html | |||
autoReload:自动再配置 | |||
internalLogFile:可以让NLog把内部的调试和异常信息都写入指定文件里程序没问题了,日志却出了问题。这个该怎么办,到底是哪里不正确了?假如日志本身除了bug该如何解决?这就需要日志排错。把日志的错误信息写入日志。 | |||
<nlog throwExceptions="true" /> | |||
<nlog internalLogFile="file.txt" />- 设置internalLogFile属性可以让NLog把内部的调试和异常信息都写入指定文件里。 | |||
<nlog internalLogLevel="Trace|Debug|Info|Warn|Error|Fatal" /> - 决定内部日志的级别,级别越高,输出的日志信息越简洁。 | |||
<nlog internalLogToConsole="false|true" /> - 是否把内部日志输出到标准控制台。 | |||
<nlog internalLogToConsoleError="false|true" /> - 是否把内部日志输出到标准错误控制台 (stderr)。 | |||
设置throwExceptions属性为“true”可以让NLog不再阻挡这类异常,而是把它们抛给调用者。在部署是这样做可以帮我们快速定位问题。一旦应用程序已经正确配置了,我们建议把throwExceptions的值设为“false”,这样由于日志引发的问题不至于导致应用程序的崩溃。 | |||
--> | |||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd" | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
autoReload="true" | |||
internalLogFile="/data/logs/JT808.DotNetty.Hosting/internalLog.txt" | |||
internalLogLevel="Debug" > | |||
<variable name="Directory" value="/data/logs/JT808.DotNetty.Hosting"/> | |||
<targets> | |||
<target name="all" xsi:type="File" | |||
fileName="${Directory}/all/${shortdate}.log" | |||
layout="${date:format=yyyyMMddHHmmss} ${callsite} ${level}:${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"/> | |||
<target name="console" xsi:type="ColoredConsole" | |||
useDefaultRowHighlightingRules="false" | |||
layout="${date:format=yyyyMMddHHmmss} ${callsite} ${level} ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"> | |||
<highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" /> | |||
<highlight-row condition="level == LogLevel.Info" foregroundColor="Gray" /> | |||
<highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" /> | |||
<highlight-row condition="level == LogLevel.Error" foregroundColor="Red" /> | |||
<highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" /> | |||
</target> | |||
</targets> | |||
<rules> | |||
<logger name="*" minlevel="Debug" maxlevel="Fatal" writeTo="all"/> | |||
</rules> | |||
</nlog> |
@@ -0,0 +1,35 @@ | |||
<?xml version="1.0" encoding="utf-8" ?> | |||
<!-- | |||
参考:http://www.cnblogs.com/fuchongjundream/p/3936431.html | |||
autoReload:自动再配置 | |||
internalLogFile:可以让NLog把内部的调试和异常信息都写入指定文件里程序没问题了,日志却出了问题。这个该怎么办,到底是哪里不正确了?假如日志本身除了bug该如何解决?这就需要日志排错。把日志的错误信息写入日志。 | |||
<nlog throwExceptions="true" /> | |||
<nlog internalLogFile="file.txt" />- 设置internalLogFile属性可以让NLog把内部的调试和异常信息都写入指定文件里。 | |||
<nlog internalLogLevel="Trace|Debug|Info|Warn|Error|Fatal" /> - 决定内部日志的级别,级别越高,输出的日志信息越简洁。 | |||
<nlog internalLogToConsole="false|true" /> - 是否把内部日志输出到标准控制台。 | |||
<nlog internalLogToConsoleError="false|true" /> - 是否把内部日志输出到标准错误控制台 (stderr)。 | |||
设置throwExceptions属性为“true”可以让NLog不再阻挡这类异常,而是把它们抛给调用者。在部署是这样做可以帮我们快速定位问题。一旦应用程序已经正确配置了,我们建议把throwExceptions的值设为“false”,这样由于日志引发的问题不至于导致应用程序的崩溃。 | |||
--> | |||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd" | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
autoReload="true" | |||
internalLogFile="${basedir}/wwwroot/logs/internalLog.txt" | |||
internalLogLevel="Debug" > | |||
<targets> | |||
<target name="all" xsi:type="File" | |||
fileName="${basedir}/wwwroot/logs/all/${shortdate}.log" | |||
layout="${date:format=yyyyMMddHHmmss} ${callsite} ${level}:${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"/> | |||
<target name="console" xsi:type="ColoredConsole" | |||
useDefaultRowHighlightingRules="false" | |||
layout="${date:format=yyyyMMddHHmmss} ${callsite} ${level} ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}"> | |||
<highlight-row condition="level == LogLevel.Debug" foregroundColor="DarkGray" /> | |||
<highlight-row condition="level == LogLevel.Info" foregroundColor="Gray" /> | |||
<highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" /> | |||
<highlight-row condition="level == LogLevel.Error" foregroundColor="Red" /> | |||
<highlight-row condition="level == LogLevel.Fatal" foregroundColor="Red" backgroundColor="White" /> | |||
</target> | |||
</targets> | |||
<rules> | |||
<logger name="*" minlevel="Debug" maxlevel="Fatal" writeTo="all,console"/> | |||
</rules> | |||
</nlog> |
@@ -25,5 +25,35 @@ namespace JT808.DotNetty.Hosting.Handlers | |||
logger.LogDebug("Tcp_Msg0x0200"); | |||
return base.Msg0x0200(request); | |||
} | |||
public override IJT808Reply Msg0x0001(JT808Request request) | |||
{ | |||
logger.LogDebug("Tcp_Msg0x0001"); | |||
return base.Msg0x0001(request); | |||
} | |||
public override IJT808Reply Msg0x0002(JT808Request request) | |||
{ | |||
logger.LogDebug("Tcp_Msg0x0002"); | |||
return base.Msg0x0002(request); | |||
} | |||
public override IJT808Reply Msg0x0003(JT808Request request) | |||
{ | |||
logger.LogDebug("Tcp_Msg0x0003"); | |||
return base.Msg0x0003(request); | |||
} | |||
public override IJT808Reply Msg0x0100(JT808Request request) | |||
{ | |||
logger.LogDebug("Tcp_Msg0x0100"); | |||
return base.Msg0x0100(request); | |||
} | |||
public override IJT808Reply Msg0x0102(JT808Request request) | |||
{ | |||
logger.LogDebug("Tcp_Msg0x0102"); | |||
return base.Msg0x0102(request); | |||
} | |||
} | |||
} |
@@ -25,5 +25,35 @@ namespace JT808.DotNetty.Hosting.Handlers | |||
logger.LogDebug("Udp_Msg0x0200"); | |||
return base.Msg0x0200(request); | |||
} | |||
public override IJT808Reply Msg0x0001(JT808Request request) | |||
{ | |||
logger.LogDebug("Udp_Msg0x0001"); | |||
return base.Msg0x0001(request); | |||
} | |||
public override IJT808Reply Msg0x0002(JT808Request request) | |||
{ | |||
logger.LogDebug("Udp_Msg0x0002"); | |||
return base.Msg0x0002(request); | |||
} | |||
public override IJT808Reply Msg0x0003(JT808Request request) | |||
{ | |||
logger.LogDebug("Udp_Msg0x0003"); | |||
return base.Msg0x0003(request); | |||
} | |||
public override IJT808Reply Msg0x0100(JT808Request request) | |||
{ | |||
logger.LogDebug("Udp_Msg0x0100"); | |||
return base.Msg0x0100(request); | |||
} | |||
public override IJT808Reply Msg0x0102(JT808Request request) | |||
{ | |||
logger.LogDebug("Udp_Msg0x0102"); | |||
return base.Msg0x0102(request); | |||
} | |||
} | |||
} |
@@ -0,0 +1,25 @@ | |||
using JT808.DotNetty.Abstractions; | |||
using JT808.DotNetty.Abstractions.Enums; | |||
using Microsoft.Extensions.Logging; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using JT808.Protocol.Extensions; | |||
namespace JT808.DotNetty.Hosting.Impls | |||
{ | |||
public class JT808DownlinkPacketLogging : IJT808DownlinkPacket | |||
{ | |||
private readonly ILogger logger; | |||
public JT808DownlinkPacketLogging(ILoggerFactory loggerFactory ) | |||
{ | |||
logger = loggerFactory.CreateLogger("JT808DownlinkPacketLogging"); | |||
} | |||
public Task ProcessorAsync(byte[] data, JT808TransportProtocolType transportProtocolType) | |||
{ | |||
logger.LogInformation("send >>>"+data.ToHexString()); | |||
return Task.CompletedTask; | |||
} | |||
} | |||
} |
@@ -12,6 +12,8 @@ | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" /> | |||
<PackageReference Include="NLog" Version="4.6.4" /> | |||
<PackageReference Include="NLog.Extensions.Logging" Version="1.5.0" /> | |||
<PackageReference Include="WebApiClient.Extensions.DependencyInjection" Version="1.2.3" /> | |||
</ItemGroup> | |||
@@ -26,6 +28,15 @@ | |||
<None Update="appsettings.json"> | |||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||
</None> | |||
<None Update="Configs\nlog.unix.config"> | |||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||
</None> | |||
<None Update="Configs\nlog.win.config"> | |||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||
</None> | |||
<None Update="Configs\NLog.xsd"> | |||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||
</None> | |||
</ItemGroup> | |||
</Project> |
@@ -2,6 +2,7 @@ | |||
using JT808.DotNetty.Core; | |||
using JT808.DotNetty.Core.Handlers; | |||
using JT808.DotNetty.Hosting.Handlers; | |||
using JT808.DotNetty.Hosting.Impls; | |||
using JT808.DotNetty.Tcp; | |||
using JT808.DotNetty.Udp; | |||
using JT808.DotNetty.WebApi; | |||
@@ -11,6 +12,7 @@ using Microsoft.Extensions.DependencyInjection; | |||
using Microsoft.Extensions.DependencyInjection.Extensions; | |||
using Microsoft.Extensions.Hosting; | |||
using Microsoft.Extensions.Logging; | |||
using NLog.Extensions.Logging; | |||
using System; | |||
using System.Collections.Concurrent; | |||
using System.Collections.Generic; | |||
@@ -33,7 +35,15 @@ namespace JT808.DotNetty.Hosting | |||
}) | |||
.ConfigureLogging((context, logging) => | |||
{ | |||
logging.AddConsole(); | |||
if (Environment.OSVersion.Platform == PlatformID.Unix) | |||
{ | |||
NLog.LogManager.LoadConfiguration("Configs/nlog.unix.config"); | |||
} | |||
else | |||
{ | |||
NLog.LogManager.LoadConfiguration("Configs/nlog.win.config"); | |||
} | |||
logging.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true }); | |||
logging.SetMinimumLevel(LogLevel.Trace); | |||
}) | |||
.ConfigureServices((hostContext, services) => | |||
@@ -48,17 +58,18 @@ namespace JT808.DotNetty.Hosting | |||
services.Replace(new ServiceDescriptor(typeof(JT808MsgIdTcpHandlerBase), typeof(JT808MsgIdTcpCustomHandler), ServiceLifetime.Singleton)); | |||
// 自定义Udp消息处理业务 | |||
services.Replace(new ServiceDescriptor(typeof(JT808MsgIdUdpHandlerBase), typeof(JT808MsgIdUdpCustomHandler), ServiceLifetime.Singleton)); | |||
services.Replace(new ServiceDescriptor(typeof(IJT808DownlinkPacket), typeof(JT808DownlinkPacketLogging), ServiceLifetime.Singleton)); | |||
// 自定义会话通知(在线/离线)使用异步方式 | |||
//services.Replace(new ServiceDescriptor(typeof(IJT808SessionPublishing), typeof(CustomJT808SessionPublishing), ServiceLifetime.Singleton)); | |||
// 自定义原包转发 使用异步方式 | |||
//services.Replace(new ServiceDescriptor(typeof(IJT808SourcePackageDispatcher), typeof(CustomJT808SourcePackageDispatcher), ServiceLifetime.Singleton)); | |||
// webapi客户端调用 | |||
services.AddHttpApi<IJT808DotNettyWebApi>().ConfigureHttpApiConfig((c, p) => | |||
{ | |||
c.HttpHost = new Uri("http://localhost:12828/jt808api/"); | |||
c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; | |||
c.LoggerFactory = p.GetRequiredService<ILoggerFactory>(); | |||
}); | |||
//services.AddHttpApi<IJT808DotNettyWebApi>().ConfigureHttpApiConfig((c, p) => | |||
//{ | |||
// c.HttpHost = new Uri("http://localhost:12828/jt808api/"); | |||
// c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; | |||
// c.LoggerFactory = p.GetRequiredService<ILoggerFactory>(); | |||
//}); | |||
//var client = services.BuildServiceProvider().GetRequiredService<IJT808DotNettyWebApi>(); | |||
//var result = client.GetTcpAtomicCounter().InvokeAsync().Result; | |||
}); | |||
@@ -13,8 +13,8 @@ | |||
} | |||
}, | |||
"JT808Configuration": { | |||
"TcpPort": 12808, | |||
"UdpPort": 12818, | |||
"WebApiPort": 12828 | |||
"TcpPort": 808, | |||
"UdpPort": 808, | |||
"WebApiPort": 828 | |||
} | |||
} |
@@ -71,9 +71,10 @@ namespace JT808.DotNetty.Udp.Handlers | |||
jT808AtomicCounterService.MsgSuccessIncrement(); | |||
jT808TrafficService.ReceiveSize(msg.Buffer.Length); | |||
jT808UdpSessionManager.TryAdd(ctx.Channel, msg.Sender, jT808HeaderPackage.Header.TerminalPhoneNo); | |||
if (logger.IsEnabled(LogLevel.Debug)) | |||
if (logger.IsEnabled(LogLevel.Trace)) | |||
{ | |||
logger.LogDebug("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); | |||
logger.LogTrace("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); | |||
logger.LogTrace("accept msg <<< " + ByteBufferUtil.HexDump(msg.Buffer)); | |||
} | |||
if (handler.HandlerDict.TryGetValue(jT808HeaderPackage.Header.MsgId, out var handlerFunc)) | |||
{ | |||