Work Добавлено больше информации в сообщение об ошибке.

This commit is contained in:
ngfrolov 2023-10-12 12:45:19 +05:00
parent fd5deaa5a2
commit d99d1a27a1
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7

View File

@ -1,5 +1,6 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using System; using System;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -74,7 +75,8 @@ public abstract class Work : BackgroundWorkDto
} }
catch (Exception exception) catch (Exception exception)
{ {
SetLastError(exception.Message); var message = FormatExceptionMessage(exception);
SetLastError(message);
if (OnErrorAsync is not null) if (OnErrorAsync is not null)
{ {
var task = Task.Run( var task = Task.Run(
@ -86,6 +88,31 @@ public abstract class Work : BackgroundWorkDto
return false; return false;
} }
private static string FormatExceptionMessage(Exception exception)
{
var firstException = FirstException(exception);
var message = new StringBuilder();
if (firstException != exception)
{
message.Append("top exception:");
message.AppendLine(exception.Message);
message.Append("inner exception:");
message.AppendLine(firstException.Message);
}
else
message.AppendLine(firstException.Message);
message.AppendLine(exception.StackTrace);
return message.ToString();
}
private static Exception FirstException(Exception exception)
{
if (exception.InnerException is not null)
return FirstException(exception.InnerException);
return exception;
}
/// <summary> /// <summary>
/// делегат фоновой работы /// делегат фоновой работы
/// </summary> /// </summary>