mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-05 09:10:53 +08:00
42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
|
|
using System.Runtime.ExceptionServices;
|
|||
|
|
using Microsoft.Extensions.Logging;
|
|||
|
|
using Yi.Framework.Infrastructure.Enums;
|
|||
|
|
|
|||
|
|
namespace Yi.Framework.Infrastructure.Exceptions;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <see cref="Exception"/><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static class ExceptionExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// ʹ<><CAB9> <see cref="ExceptionDispatchInfo.Capture"/> <20>ٴ<EFBFBD><D9B4>׳<EFBFBD><D7B3>쳣
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="exception">Exception to be re-thrown</param>
|
|||
|
|
public static void ReThrow(this Exception exception)
|
|||
|
|
{
|
|||
|
|
ExceptionDispatchInfo.Capture(exception).Throw();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>쳣<EFBFBD>е<EFBFBD><D0B5><EFBFBD>־<EFBFBD>ȼ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="exception"></param>
|
|||
|
|
/// <param name="defaultLevel"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static LogLevel GetLogLevel(this Exception exception, LogLevel defaultLevel = LogLevel.Error)
|
|||
|
|
{
|
|||
|
|
return (exception as IHasLogLevel)?.LogLevel ?? defaultLevel;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>쳣<EFBFBD>е<EFBFBD><D0B5><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="exception"></param>
|
|||
|
|
/// <param name="defaultLevel"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static int GetLogErrorCode(this Exception exception, ResultCodeEnum defaultCode = ResultCodeEnum.NotSuccess)
|
|||
|
|
{
|
|||
|
|
return (exception as IHasErrorCode)?.Code ?? (int)defaultCode;
|
|||
|
|
}
|
|||
|
|
}
|