mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-04 08:40:52 +08:00
43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
|
|
using System;
|
|||
|
|
using System.Runtime.ExceptionServices;
|
|||
|
|
using Microsoft.Extensions.Logging;
|
|||
|
|
using Yi.Framework.Common.Enum;
|
|||
|
|
|
|||
|
|
namespace Yi.Framework.Common.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 ResultCodeEnum GetLogErrorCode(this Exception exception, ResultCodeEnum defaultCode = ResultCodeEnum.NotSuccess)
|
|||
|
|
{
|
|||
|
|
return (exception as IHasErrorCode)?.Code ?? defaultCode;
|
|||
|
|
}
|
|||
|
|
}
|