You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

55 lines
2.2 KiB

using NLog;
using System;
using System.Text.Json;
namespace Mirle.Component.Record
{
/// <summary>
/// 記錄錯誤擴充方法類別
/// </summary>
public static class LogErrorExtension
{
/// <summary>
/// 紀錄錯誤資訊
/// </summary>
/// <param name="log">紀錄器</param>
/// <param name="funcName">方法名稱</param>
/// <param name="ex">例外錯誤</param>
/// <param name="param">參數</param>
public static void LogError(this Logger log, string funcName, Exception ex, string param = "None")
{
log.Error($"[{funcName}] | [{param}] | {ex.Message}");
log.Error($"========= Error Stack Start ====== \r\n " +
$"{ex.StackTrace} \r\n" +
$"========= Error Stack End ========");
}
/// <summary>
/// 記錄錯誤資訊
/// </summary>
/// <param name="log">紀錄器</param>
/// <param name="funcName">方法名稱</param>
/// <param name="errorMessage">錯誤訊息</param>
/// <param name="param">參數</param>
public static void LogError(this Logger log, string funcName, string errorMessage, string param = "None")
{
log.Error($"[{funcName}] | [{param}] | {errorMessage}");
}
/// <summary>
/// 記錄錯誤資料
/// </summary>
/// <typeparam name="T">資料類別</typeparam>
/// <param name="log">紀錄器</param>
/// <param name="tableName">方法名稱</param>
/// <param name="data">錯誤資料</param>
/// <param name="ex">例外錯誤</param>
/// <param name="param">參數</param>
public static void LogErrorData<T>(this Logger log, string tableName, T data, Exception ex, string param = "None")
{
log.Error($"Error Data: {JsonSerializer.Serialize(data)}");
log.Error($"[false] | [{tableName}] | {ex.Message} \r\n" +
$"========= Error Stack Start ========== \r\n" +
$"{ex.StackTrace} \r\n" +
$"========= Error Stack End ============");
}
}
}