增加udp日志、background异常处理
This commit is contained in:
parent
1053c3aebd
commit
1ef5d2465b
@ -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>
|
||||
@ -33,6 +37,8 @@ namespace FastGithub.DomainResolve
|
||||
/// <param name="stoppingToken"></param>
|
||||
/// <returns></returns>
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await this.dnscryptProxy.StartAsync(stoppingToken);
|
||||
while (stoppingToken.IsCancellationRequested == false)
|
||||
@ -41,6 +47,14 @@ namespace FastGithub.DomainResolve
|
||||
await Task.Delay(this.testPeriodTimeSpan, stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogError(ex, "域名解析异常");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止服务
|
||||
|
||||
@ -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 (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
catch (Win32Exception ex) when (ex.NativeErrorCode == 995)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
stoppingToken.ThrowIfCancellationRequested();
|
||||
this.logger.LogError(ex, "dns拦截器异常");
|
||||
await this.host.StopAsync(stoppingToken);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -9,5 +9,10 @@
|
||||
/// 父进程id
|
||||
/// </summary>
|
||||
public int ParentProcessId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// udp日志服务器端口
|
||||
/// </summary>
|
||||
public int UdpLoggerPort { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -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" />
|
||||
@ -44,4 +45,8 @@
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Properties\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -65,7 +65,6 @@ namespace FastGithub
|
||||
appBuilder.UseRequestLogging();
|
||||
appBuilder.UseHttpReverseProxy();
|
||||
|
||||
app.UseStaticFiles();
|
||||
appBuilder.UseRouting();
|
||||
appBuilder.UseEndpoints(endpoint =>
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user