增加udp日志、background异常处理

This commit is contained in:
陈国伟 2021-11-02 09:47:51 +08:00
parent 1053c3aebd
commit 1ef5d2465b
8 changed files with 58 additions and 22 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
@ -12,7 +13,8 @@ namespace FastGithub.DomainResolve
{
private readonly DnscryptProxy dnscryptProxy;
private readonly IDomainResolver domainResolver;
private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds (1d);
private readonly ILogger<DomainResolveHostedService> logger;
private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds(1d);
/// <summary>
/// 域名解析后台服务
@ -21,10 +23,12 @@ namespace FastGithub.DomainResolve
/// <param name="domainResolver"></param>
public DomainResolveHostedService(
DnscryptProxy dnscryptProxy,
IDomainResolver domainResolver)
IDomainResolver domainResolver,
ILogger<DomainResolveHostedService> logger)
{
this.dnscryptProxy = dnscryptProxy;
this.domainResolver = domainResolver;
this.logger = logger;
}
/// <summary>
@ -34,11 +38,21 @@ namespace FastGithub.DomainResolve
/// <returns></returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await this.dnscryptProxy.StartAsync(stoppingToken);
while (stoppingToken.IsCancellationRequested == false)
try
{
await this.domainResolver.TestAllEndPointsAsync(stoppingToken);
await Task.Delay(this.testPeriodTimeSpan, stoppingToken);
await this.dnscryptProxy.StartAsync(stoppingToken);
while (stoppingToken.IsCancellationRequested == false)
{
await this.domainResolver.TestAllEndPointsAsync(stoppingToken);
await Task.Delay(this.testPeriodTimeSpan, stoppingToken);
}
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
this.logger.LogError(ex, "域名解析异常");
}
}

View File

@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
@ -77,9 +78,14 @@ namespace FastGithub.PacketIntercept
{
await this.dnsInterceptor.InterceptAsync(stoppingToken);
}
catch (Exception ex)
catch (OperationCanceledException)
{
stoppingToken.ThrowIfCancellationRequested();
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 995)
{
}
catch (Exception ex)
{
this.logger.LogError(ex, "dns拦截器异常");
await this.host.StopAsync(stoppingToken);
}

View File

@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading;
@ -47,9 +48,14 @@ namespace FastGithub.PacketIntercept
var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
await Task.WhenAll(tasks);
}
catch (OperationCanceledException)
{
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 995)
{
}
catch (Exception ex)
{
stoppingToken.ThrowIfCancellationRequested();
this.logger.LogError(ex, "tcp拦截器异常");
await this.host.StopAsync(stoppingToken);
}

View File

@ -9,5 +9,10 @@
/// 父进程id
/// </summary>
public int ParentProcessId { get; set; }
/// <summary>
/// udp日志服务器端口
/// </summary>
public int UdpLoggerPort { get; set; }
}
}

View File

@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.Network" Version="2.0.2.68" />
<ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />
<ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" />
<ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" />
@ -19,7 +20,7 @@
<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
</ItemGroup>
@ -44,4 +45,8 @@
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>

View File

@ -2,8 +2,10 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Sinks.Network;
using System;
using System.IO;
using System.Net;
namespace FastGithub
{
@ -71,6 +73,12 @@ namespace FastGithub
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: template)
.WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day, outputTemplate: template);
var udpLoggerPort = hosting.Configuration.GetValue(nameof(AppOptions.UdpLoggerPort), 0);
if (udpLoggerPort > 0)
{
logger.WriteTo.UDPSink(IPAddress.Loopback, udpLoggerPort);
}
});
});
}

View File

@ -1,14 +1,7 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
{
"profiles": {
"FastGithub": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development",
"Logging__LogLevel__Default": "Trace"
}
"commandName": "Project"
}
}
}
}

View File

@ -64,8 +64,7 @@ namespace FastGithub
{
appBuilder.UseRequestLogging();
appBuilder.UseHttpReverseProxy();
app.UseStaticFiles();
appBuilder.UseRouting();
appBuilder.UseEndpoints(endpoint =>
{