Files
caliverse_server/ServerCore/Dump/DotNetDumpHelper.cs
2025-05-01 07:20:41 +09:00

62 lines
1.8 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Diagnostics.NETCore.Client;
namespace ServerCore;
public static class DotNetDumpHelper
{
private static DumpConfig DumpConfig;
private static void unhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
// 덤프 파일 생성 경로와 이름
string dumpFile = $"{DumpConfig}_{DateTime.Now.Ticks}.dmp";
string txtOutputFile = $"{DumpConfig}_{DateTime.Now.Ticks}.txt";
// 현재 프로세스 id 가져오기
int processId = Process.GetCurrentProcess().Id;
var client = new DiagnosticsClient(processId);
// 덤프 파일 생성
try
{
client.WriteDump(DumpType.Normal, dumpFile, logDumpGeneration: true);
}
catch (Exception dumpEx)
{
Log.getLogger().error($"Exception !!!, WriteDump() !!! - exception:{dumpEx}");
}
try
{
DumpConfig.TxtFile = txtOutputFile;
// 예외 정보 및 프로세스 정보를 텍스트 파일로 저장
DumpHandler.writeTxt(DumpConfig, ex);
}
catch (Exception txtEx)
{
Log.getLogger().error($"Exception !!!, DumpHandler.write() !!! - exception:{txtEx}");
}
}
}
public static void init(string dumpPath, string name)
{
// 경로 및 이름 설정
DumpConfig.DumpFile = $"{dumpPath}/{name}";
// 예외 처리 핸들러 등록
AppDomain.CurrentDomain.UnhandledException += unhandledExceptionHandler;
}
}