添加全局异常捕获

This commit is contained in:
SpringHgui 2020-10-21 15:39:07 +08:00
parent 67ebf56e5f
commit 49020ede15
3 changed files with 44 additions and 8 deletions

View File

@ -3,6 +3,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<UserSecretsId>17e7d83b-9640-4fba-8e72-2b6b022a01b0</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Program.cs.BASE.cs" />

View File

@ -4,11 +4,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
<SelfContained>false</SelfContained>
<DeleteExistingFiles>False</DeleteExistingFiles>
<ExcludeApp_Data>False</ExcludeApp_Data>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<PublishProvider>FileSystem</PublishProvider>
<PublishUrl>bin\Release\netcoreapp3.1\publish\</PublishUrl>
<WebPublishMethod>FileSystem</WebPublishMethod>
</PropertyGroup>
</Project>

View File

@ -5,9 +5,11 @@ using FastTunnel.Server.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
@ -18,15 +20,44 @@ namespace FastTunnel.Server.Service
ILogger<ServiceFastTunnelServer> _logger;
FastTunnelServer _fastTunnelServer;
TestAuthenticationFilter _testAuthenticationFilter;
IConfiguration _configuration;
IConfiguration _configuration;
public ServiceFastTunnelServer(
ILogger<ServiceFastTunnelServer> logger,
IConfiguration configuration,
IConfiguration configuration,
TestAuthenticationFilter testAuthenticationFilter)
{
_configuration = configuration;
_testAuthenticationFilter = testAuthenticationFilter;
_logger = logger;
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
_logger.LogError("【UnhandledException】" + e.ExceptionObject);
_logger.LogError("【UnhandledException】" + JsonConvert.SerializeObject(e.ExceptionObject));
var type = e.ExceptionObject.GetType();
_logger.LogError("ExceptionObject GetType " + type);
}
catch
{
}
}
private void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
if (e.Exception is System.IO.DirectoryNotFoundException)
{
// nlog第一次找不到文件的错误跳过
}
else
{
_logger.LogError(e.Exception, "【FirstChanceException】");
}
}
public Task StartAsync(CancellationToken cancellationToken)
@ -39,6 +70,8 @@ namespace FastTunnel.Server.Service
try
{
_fastTunnelServer.Run();
_logger.LogDebug("Server Run Success");
}
catch (Exception ex)
{