5.1.6.日誌

1.目的

  • 日誌記錄陳述可以追蹤與理解程式的執行

  • 日誌陳述的輸出比較方便過濾與查詢

2.日誌記錄函式庫

  • 1.Windows API: ReportEvent

    • 1.在程式中呼叫API以在Windows Event log中留下紀錄

            #include "stdafx.h"
            #include <windows.h>
      
            int _tmain(int argc, _TCHAR* argv[])
            {
                LPTSTR lpszStrings[] = {"Called main() in file", __FILE__};
                HANDLE hEventSource = RegisterEventSource(NULL, "myService");
                if (hEventSource == NULL){
                       return 1;
                }
                ReportEvent(hEventSource,
                   EVENTLOG_INFORMATION_TYPE,
                   0,
                   0,
                   NULL,
                   2,
                   0,
                   (LPCSTR*)lpszStrings,
                   NULL);
      
                DeregisterEventSource(hEventSource);
                return 0;
            }
    • 2.檢視Windows Event log

      • 在開始功能表下輸入以下指令即可打開Windows Event log

              eventvwr.msc

Last updated