增加udp日志、background异常处理
This commit is contained in:
parent
1053c3aebd
commit
1ef5d2465b
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -12,7 +13,8 @@ namespace FastGithub.DomainResolve
|
|||||||
{
|
{
|
||||||
private readonly DnscryptProxy dnscryptProxy;
|
private readonly DnscryptProxy dnscryptProxy;
|
||||||
private readonly IDomainResolver domainResolver;
|
private readonly IDomainResolver domainResolver;
|
||||||
private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds (1d);
|
private readonly ILogger<DomainResolveHostedService> logger;
|
||||||
|
private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds(1d);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 域名解析后台服务
|
/// 域名解析后台服务
|
||||||
@ -21,10 +23,12 @@ namespace FastGithub.DomainResolve
|
|||||||
/// <param name="domainResolver"></param>
|
/// <param name="domainResolver"></param>
|
||||||
public DomainResolveHostedService(
|
public DomainResolveHostedService(
|
||||||
DnscryptProxy dnscryptProxy,
|
DnscryptProxy dnscryptProxy,
|
||||||
IDomainResolver domainResolver)
|
IDomainResolver domainResolver,
|
||||||
|
ILogger<DomainResolveHostedService> logger)
|
||||||
{
|
{
|
||||||
this.dnscryptProxy = dnscryptProxy;
|
this.dnscryptProxy = dnscryptProxy;
|
||||||
this.domainResolver = domainResolver;
|
this.domainResolver = domainResolver;
|
||||||
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -34,11 +38,21 @@ namespace FastGithub.DomainResolve
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
await this.dnscryptProxy.StartAsync(stoppingToken);
|
try
|
||||||
while (stoppingToken.IsCancellationRequested == false)
|
|
||||||
{
|
{
|
||||||
await this.domainResolver.TestAllEndPointsAsync(stoppingToken);
|
await this.dnscryptProxy.StartAsync(stoppingToken);
|
||||||
await Task.Delay(this.testPeriodTimeSpan, 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, "域名解析异常");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -77,9 +78,14 @@ namespace FastGithub.PacketIntercept
|
|||||||
{
|
{
|
||||||
await this.dnsInterceptor.InterceptAsync(stoppingToken);
|
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拦截器异常");
|
this.logger.LogError(ex, "dns拦截器异常");
|
||||||
await this.host.StopAsync(stoppingToken);
|
await this.host.StopAsync(stoppingToken);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -47,9 +48,14 @@ namespace FastGithub.PacketIntercept
|
|||||||
var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
|
var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
|
||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
catch (Win32Exception ex) when (ex.NativeErrorCode == 995)
|
||||||
|
{
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
stoppingToken.ThrowIfCancellationRequested();
|
|
||||||
this.logger.LogError(ex, "tcp拦截器异常");
|
this.logger.LogError(ex, "tcp拦截器异常");
|
||||||
await this.host.StopAsync(stoppingToken);
|
await this.host.StopAsync(stoppingToken);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,5 +9,10 @@
|
|||||||
/// 父进程id
|
/// 父进程id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int ParentProcessId { get; set; }
|
public int ParentProcessId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// udp日志服务器端口
|
||||||
|
/// </summary>
|
||||||
|
public int UdpLoggerPort { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
|
<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
|
<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.DomainResolve\FastGithub.DomainResolve.csproj" />
|
||||||
<ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" />
|
<ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" />
|
||||||
<ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" />
|
<ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" />
|
||||||
@ -19,7 +20,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
|
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.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" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
@ -44,4 +45,8 @@
|
|||||||
</None>
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Properties\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -2,8 +2,10 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using Serilog.Sinks.Network;
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub
|
||||||
{
|
{
|
||||||
@ -71,6 +73,12 @@ namespace FastGithub
|
|||||||
.Enrich.FromLogContext()
|
.Enrich.FromLogContext()
|
||||||
.WriteTo.Console(outputTemplate: template)
|
.WriteTo.Console(outputTemplate: template)
|
||||||
.WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day, 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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"FastGithub": {
|
"FastGithub": {
|
||||||
"commandName": "Project",
|
"commandName": "Project"
|
||||||
"dotnetRunMessages": "true",
|
|
||||||
"launchBrowser": true,
|
|
||||||
"environmentVariables": {
|
|
||||||
"DOTNET_ENVIRONMENT": "Development",
|
|
||||||
"Logging__LogLevel__Default": "Trace"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,8 +64,7 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
appBuilder.UseRequestLogging();
|
appBuilder.UseRequestLogging();
|
||||||
appBuilder.UseHttpReverseProxy();
|
appBuilder.UseHttpReverseProxy();
|
||||||
|
|
||||||
app.UseStaticFiles();
|
|
||||||
appBuilder.UseRouting();
|
appBuilder.UseRouting();
|
||||||
appBuilder.UseEndpoints(endpoint =>
|
appBuilder.UseEndpoints(endpoint =>
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user