반응형

윈도우의 이벤트로그에 기록하는 방법입니다.

static 함수로 만들었는데 필요에 따라 변경하면 됩니다. Event type 은 Error / Warning / Information 으로만 구분했는데 이 구분도 필요에 따라 추가/삭제 하시면 됩니다.

로그는 "응용 프로그램" 구분으로 쌓이게 됩니다. 원복(source) 는 빌드되는 프로그램 이름으로 하였습니다. 변경 시 아래 sourceName 변수를 수정하면 됩니다.

public static void WriteEventLog(System.Diagnostics.EventLogEntryType eventLogEntryType, string message)
{
    string sourceName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; 

    System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();

    if(!System.Diagnostics.EventLog.SourceExists(sourceName)) {
        System.Diagnostics.EventLog.CreateEventSource(sourceName,"Application");
    }

    eventLog.Source = sourceName;
    int eventID = 1;

    if(eventLogEntryType == System.Diagnostics.EventLogEntryType.Error)
    {
        eventID = 300; //에러인  경우 에러 ID 300
    }
    else if (eventLogEntryType == System.Diagnostics.EventLogEntryType.Warning)
    {
        eventID = 200; //경고인  경우 에러 ID 200
    }
    else if (eventLogEntryType == System.Diagnostics.EventLogEntryType.Information)
    {
        eventID = 100; //일반 정보인  경우 에러 ID 100
    }

    eventLog.WriteEntry(message, eventLogEntryType, eventID);
    eventLog.Close();
}
반응형

'IT > C#' 카테고리의 다른 글

enum 값 확인  (0) 2024.01.02
EXE (Assembly) 버전 자동으로 업데이트 하는 방법  (1) 2023.12.29
C# .NET AES 256 암복호화  (0) 2023.12.22
PRG(POST/Redirect/GET)패턴  (0) 2023.03.30
MVC5 Html Helper (Core 아님)  (0) 2023.03.30

+ Recent posts