63 lines
1.5 KiB
C#
63 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace BackupService
|
|
{
|
|
public class ErrorLogger
|
|
{
|
|
private const string Message1 = @"錯誤類別:{0}
|
|
錯誤來源:{1}
|
|
錯誤訊息:{2}
|
|
堆疊內容:
|
|
{3}
|
|
|
|
";
|
|
|
|
private const string Message2 = @"以上的錯誤發生的原因是:
|
|
錯誤類別:{0}
|
|
錯誤來源:{1}
|
|
錯誤訊息:{2}
|
|
堆疊內容:
|
|
{3}
|
|
|
|
";
|
|
|
|
private static string logPath = null;
|
|
|
|
public static string LogPath
|
|
{
|
|
get { return ErrorLogger.logPath; }
|
|
set { ErrorLogger.logPath = value; }
|
|
}
|
|
|
|
|
|
public static void WriteException(Exception ex)
|
|
{
|
|
// 發生未處理錯誤時執行的程式碼
|
|
string Message = "";
|
|
|
|
Message += String.Format(ErrorLogger.Message1,
|
|
ex.GetType().FullName,
|
|
ex.Source,
|
|
ex.Message,
|
|
ex.StackTrace);
|
|
|
|
ex = ex.InnerException;
|
|
while (ex != null)
|
|
{
|
|
Message += String.Format(ErrorLogger.Message2,
|
|
ex.GetType().FullName,
|
|
ex.Source,
|
|
ex.Message,
|
|
ex.StackTrace);
|
|
ex = ex.InnerException;
|
|
}
|
|
|
|
//寫入文字檔
|
|
string fileName = DateTime.UtcNow.AddHours(8).ToString("yyyyMMdd_HHmmss");
|
|
System.IO.File.AppendAllText(string.Format(@"{0}/{1}.txt", ErrorLogger.logPath, fileName), Message);
|
|
}
|
|
}
|
|
} |