Add ExceptionExtensions...
This commit is contained in:
parent
56f1eb75f9
commit
d6356451ac
|
|
@ -10,10 +10,6 @@
|
|||
<ProjectReference Include="..\AyCode.Utils\AyCode.Utils.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Extensions\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
using AyCode.Utils.Extensions;
|
||||
|
||||
namespace AyCode.Core.Extensions;
|
||||
|
||||
public static class ExceptionExtensions
|
||||
{
|
||||
public static void GetCategoryAndMemberNameFromStackTraceString(this Exception? exception, out string? memberName, out string? categoryName)
|
||||
{
|
||||
categoryName = null;
|
||||
memberName = null;
|
||||
|
||||
if (exception == null) return;
|
||||
|
||||
var stackTraceString = exception.StackTrace ?? exception.InnerException?.StackTrace;
|
||||
if (stackTraceString.IsNullOrWhiteSpace()) return;
|
||||
|
||||
var stackSplit = stackTraceString.Split(" in ");
|
||||
if (stackSplit.Length <= 0) return;
|
||||
|
||||
stackSplit = stackSplit[0].Split('.');
|
||||
if (stackSplit.Length <= 1) return;
|
||||
|
||||
memberName = stackSplit[^1]; //new StackTrace(exception).GetFrame(0)?.GetMethod()?.Name;
|
||||
categoryName = stackSplit[^2]; //exception.Source
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue