【问题标题】:Is there a definitive log entry to check for system shutdown and startup?是否有明确的日志条目来检查系统关闭和启动?
【发布时间】:2020-10-08 18:38:27
【问题描述】:

我需要计算计算机每月的运行时间。

似乎有各种消息可以检查关闭 PC 的不同事件:

关机

  • 事件 ID 41 系统已重新启动,但未先完全关闭。这 如果系统停止响应、崩溃或 意外断电。
  • 在应用程序(例如:Windows 更新)时记录的事件 ID 1074 导致系统重新启动,或者当用户启动重新启动或 关掉。
  • 事件 ID 6006 记录为干净关闭。它给出了消息“ 事件日志服务已停止”。6008 记录为脏关机。它 给出消息“上一次系统关闭日期时间是 出乎意料”。
  • 事件 ID 1074(备用):“进程 X 已启动 代表用户 Y 重新启动/关闭计算机 以下原因:Z。”表示应用程序或用户 启动了重启或关机。
  • 事件 ID 1076(备用):“ 用户 X 提供的最后一次意外关闭的原因 计算机是:Y。”记录第一个具有关机权限的用户 在意外重启或关机后登录计算机,并且 提供发生的原因。

启动

  • 事件 ID 12:操作系统在系统时间启动
  • 事件 ID 6005(备用):“事件日志服务已启动。”这是系统启动的同义词。
  • 事件 ID 6009(备用):指示在启动时检测到的 Windows 产品名称、版本、内部版本号、服务包编号和操作系统类型。
  • 事件 ID 6013:显示上次重启后计算机的正常运行时间

是否有一条日志消息可以让我检查 Windows 事件日志以捕获 PC 关闭/重新启动的所有时间?

事件 ID 12 是否总是被发送而不管发生关闭的原因是什么?

【问题讨论】:

  • 6005, 6006 似乎是常用的解释 herehere。我多年来一直使用它来跟踪我的工作时间。有时,我会遇到没有适当关闭间隔的日子。
  • @AxelKemper,如果 Windows 由于事件 1074 而关闭,它仍然会随着事件 6005 重新启动,还是只有在事件 6006 关闭时才会这样做?
  • 6005 不依赖关机原因,但我没试过这个星座。
  • 所以不管你为什么关闭,你都会得到 6005?
  • 我应该补充一点,使用事件6005, 6006 未检测到节能和休眠。将我的 PC 发送到 powersave 导致 Kernel-Powerevent 42。恢复正常导致Power-Troubleshooter事件1

标签: c# windows


【解决方案1】:

这是我的 C 代码的核心部分,该代码自 2001 年以来一直在使用:

// Open the Systemevent log. 

h = OpenEventLog(name,  // NULL = use local computer 
         "System");     // source name 
if (h == NULL) 
    fatal("Could not open the System event log"); 

pevlr = (EVENTLOGRECORD *) &bBuffer; 

time(&startTime);

startTime   -= 30*24*3600L;         //  30 days (~ 4 weeks) before now
start        = 0;
prevDay      = 0;
earliestTime = 0;
duration     = 0;

// Opening the event log positions the file pointer for this 
// handle at the beginning of the log. Read the records 
// sequentially until there are no more. 

while (ReadEventLog(h,                // event log handle 
            EVENTLOG_FORWARDS_READ |  // reads forward 
            EVENTLOG_SEQUENTIAL_READ, // sequential read 
            0,            // ignored for sequential reads 
            pevlr,        // pointer to buffer 
            BUFFER_SIZE,  // size of buffer 
            &dwRead,      // number of bytes read 
            &dwNeeded))   // bytes in next record 
{
    while (dwRead > 0) 
    { 
        // The source name is just past the end of the 
        // formal structure. 

        sourceName = (LPSTR) ((LPBYTE) pevlr + sizeof(EVENTLOGRECORD));
        id         = pevlr->EventID & 0x01FFF;
        now        = (time_t)(pevlr->TimeGenerated);


        if (((id == EL_START) || (id == EL_END)) && 
            (now >= startTime) &&
            !strcmp(sourceName, "EventLog"))
          {
            if (!earliestTime)
              earliestTime = now;

            dwThisRecord++;

            tm  = localtime(&now);
            day = tm->tm_mday;

            if (day != prevDay)
              {
                printf("\n%s ", DateStamp(&now));
                if (id == EL_END)
                  printf("      ... ");
              }
            else if (id == EL_START)
              printf("\n            ");

            if (id == EL_START)
              {
                if (start)
                  {
                    printf("%s ...        (no end time!)", TimeStamp(&start));
                    printf("\n       %s", TimeStamp(&now));
                  }
                else
                  {
                    printf("%s ... ", TimeStamp(&now));
                  }
                start = now;
              }
            else
              {
                if (start)
                  {
                    printf("%s  ", TimeStamp(&now));                        
                    printf("%s", Duration(now - start));
                    duration += (now - start);
                  }
                else
                  {
                    printf(" ... %s  (no start time!)", TimeStamp(&now));
                  }
                start = 0;
              }

            prevDay = day;
          }

        dwRead -= pevlr->Length; 
        pevlr   = (EVENTLOGRECORD *) ((LPBYTE) pevlr + pevlr->Length); 
    } 

    pevlr = (EVENTLOGRECORD *) &bBuffer; 
} 

CloseEventLog(h); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    相关资源
    最近更新 更多