초기커밋
This commit is contained in:
61
ServerCore/Dump/DotNetDumpHelper.cs
Normal file
61
ServerCore/Dump/DotNetDumpHelper.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
49
ServerCore/Dump/DumpHandler.cs
Normal file
49
ServerCore/Dump/DumpHandler.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
|
||||
|
||||
namespace ServerCore;
|
||||
|
||||
public struct DumpConfig
|
||||
{
|
||||
public string DumpFile;
|
||||
public string TxtFile;
|
||||
}
|
||||
|
||||
public static class DumpHandler
|
||||
{
|
||||
public static void unhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
|
||||
{
|
||||
var exception = e.ExceptionObject as Exception;
|
||||
File.WriteAllText("unhandled_exception.log", exception?.ToString());
|
||||
|
||||
writeTxt(DumpHelper.DumpConfig, exception);
|
||||
}
|
||||
|
||||
public static void writeTxt(DumpConfig dumpCopnfig, Exception? e)
|
||||
{
|
||||
using (var fs = new FileStream(dumpCopnfig.TxtFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(fs))
|
||||
{
|
||||
writer.WriteLine("================================================================================================");
|
||||
writer.WriteLine("Date : " + DateTime.Now.ToString());
|
||||
writer.WriteLine();
|
||||
|
||||
Exception? exception = e;
|
||||
while (exception != null)
|
||||
{
|
||||
writer.WriteLine($"{exception}");
|
||||
writer.WriteLine("================================================================================================");
|
||||
|
||||
exception = exception.InnerException;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
41
ServerCore/Dump/DumpHelper.cs
Normal file
41
ServerCore/Dump/DumpHelper.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
|
||||
|
||||
namespace ServerCore;
|
||||
|
||||
public static class DumpHelper
|
||||
{
|
||||
public static DumpConfig DumpConfig;
|
||||
|
||||
private static void unhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
|
||||
{
|
||||
// 윈도우 덤프 경로: C:\Users\xxxxxx\AppData\Local\CrashDumps
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
Exception ex = (Exception)e.ExceptionObject;
|
||||
MiniDump.write(DumpConfig, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void init(string dumpPath, string name)
|
||||
{
|
||||
DumpConfig.DumpFile = $"{dumpPath}\\{name}_{DateTime.Now.Ticks}.dmp";
|
||||
DumpConfig.TxtFile = $"{dumpPath}\\{name}_{DateTime.Now.Ticks}.txt";
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(DumpHelper.unhandledExceptionHandler);
|
||||
}
|
||||
else
|
||||
{
|
||||
AppDomain.CurrentDomain.UnhandledException += DumpHandler.unhandledExceptionHandler;
|
||||
}
|
||||
}
|
||||
}
|
||||
123
ServerCore/Dump/MiniDump.cs
Normal file
123
ServerCore/Dump/MiniDump.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
|
||||
|
||||
namespace ServerCore;
|
||||
|
||||
public static class MiniDump
|
||||
{
|
||||
[Flags]
|
||||
public enum MiniDumpType : uint
|
||||
{
|
||||
// From dbghelp.h:
|
||||
Normal = 0x00000000,
|
||||
WithDataSegs = 0x00000001,
|
||||
WithFullMemory = 0x00000002,
|
||||
WithHandleData = 0x00000004,
|
||||
FilterMemory = 0x00000008,
|
||||
ScanMemory = 0x00000010,
|
||||
WithUnloadedModules = 0x00000020,
|
||||
WithIndirectlyReferencedMemory = 0x00000040,
|
||||
FilterModulePaths = 0x00000080,
|
||||
WithProcessThreadData = 0x00000100,
|
||||
WithPrivateReadWriteMemory = 0x00000200,
|
||||
WithoutOptionalData = 0x00000400,
|
||||
WithFullMemoryInfo = 0x00000800,
|
||||
WithThreadInfo = 0x00001000,
|
||||
WithCodeSegs = 0x00002000,
|
||||
WithoutAuxiliaryState = 0x00004000,
|
||||
WithFullAuxiliaryState = 0x00008000,
|
||||
WithPrivateWriteCopyMemory = 0x00010000,
|
||||
IgnoreInaccessibleMemory = 0x00020000,
|
||||
ValidTypeFlags = 0x0003ffff,
|
||||
};
|
||||
|
||||
//typedef struct _MINIDUMP_EXCEPTION_INFORMATION {
|
||||
// DWORD ThreadId;
|
||||
// PEXCEPTION_POINTERS ExceptionPointers;
|
||||
// BOOL ClientPointers;
|
||||
//} MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION;
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 4)] // Pack=4 is important! So it works also for x64!
|
||||
public struct MiniDumpExceptionInformation
|
||||
{
|
||||
public uint ThreadId;
|
||||
public IntPtr ExceptionPointers;
|
||||
[MarshalAs(UnmanagedType.Bool)]
|
||||
public bool ClientPointers;
|
||||
}
|
||||
|
||||
//BOOL
|
||||
//WINAPI
|
||||
//MiniDumpWriteDump(
|
||||
// __in HANDLE hProcess,
|
||||
// __in DWORD ProcessId,
|
||||
// __in HANDLE hFile,
|
||||
// __in MINIDUMP_TYPE DumpType,
|
||||
// __in_opt PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
|
||||
// __in_opt PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
|
||||
// __in_opt PMINIDUMP_CALLBACK_INFORMATION CallbackParam
|
||||
// );
|
||||
|
||||
// Overload requiring MiniDumpExceptionInformation
|
||||
[DllImport("dbghelp.dll", EntryPoint = "MiniDumpWriteDump", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
|
||||
static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, ref MiniDumpExceptionInformation expParam, IntPtr userStreamParam, IntPtr callbackParam);
|
||||
|
||||
// Overload supporting MiniDumpExceptionInformation == NULL
|
||||
[DllImport("dbghelp.dll", EntryPoint = "MiniDumpWriteDump", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
|
||||
|
||||
static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam);
|
||||
|
||||
[DllImport("kernel32.dll", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
|
||||
static extern uint GetCurrentThreadId();
|
||||
|
||||
public static bool write(SafeHandle fileHandle, MiniDumpType dumpType)
|
||||
{
|
||||
Process currentProcess = Process.GetCurrentProcess();
|
||||
IntPtr currentProcessHandle = currentProcess.Handle;
|
||||
uint currentProcessId = (uint)currentProcess.Id;
|
||||
MiniDumpExceptionInformation exp;
|
||||
exp.ThreadId = GetCurrentThreadId();
|
||||
exp.ClientPointers = false;
|
||||
exp.ExceptionPointers = IntPtr.Zero;
|
||||
exp.ExceptionPointers = Marshal.GetExceptionPointers();
|
||||
|
||||
bool bRet = false;
|
||||
if (exp.ExceptionPointers == IntPtr.Zero)
|
||||
{
|
||||
bRet = MiniDumpWriteDump(currentProcessHandle, currentProcessId, fileHandle, (uint)dumpType, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
else
|
||||
{
|
||||
bRet = MiniDumpWriteDump(currentProcessHandle, currentProcessId, fileHandle, (uint)dumpType, ref exp, IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
|
||||
return bRet;
|
||||
}
|
||||
|
||||
public static bool write(string fileName, MiniDumpType dumpType)
|
||||
{
|
||||
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
|
||||
{
|
||||
return write(fs.SafeFileHandle, dumpType);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool write(DumpConfig dumpCopnfig, Exception e)
|
||||
{
|
||||
using (var fs = new FileStream(dumpCopnfig.DumpFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
|
||||
{
|
||||
write(fs.SafeFileHandle, MiniDumpType.WithFullMemory);
|
||||
}
|
||||
|
||||
DumpHandler.writeTxt(dumpCopnfig, e);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user