读取logEntry.EntryType值的EventLog错误

时间:2011-06-01 18:20:59

标签: c# asp.net events logging

我有一个代码来从事件日志中获取信息。

  protected void Page_Load(object sender, EventArgs e)
    {
   EventLog eventLog = new EventLog("Application", ".");

   getEvents(eventLog.Entries);
     }


private void getEvents(EventLogEntryCollection  eventLogEntryCollection)
    {

        foreach (EventLogEntry logEntry in eventLogEntryCollection)
        {                
            if (logEntry.Source.Equals("yen"))
            {
                eventType.Add(logEntry.EntryType.ToString());
                eventTime.Add(logEntry.TimeWritten);
                eventSource.Add(logEntry.Source);
                eventCategory.Add(logEntry.Category);
                eventID.Add(logEntry.EventID);
                eventMsg.Add(logEntry.Message.ToString());
                Global.logger.Info("Level = " + logEntry.EntryType.ToString() + ", eventTime = " + logEntry.TimeWritten);
            }
        }
    }

所以logEntry.EntryType.ToString()有时会返回我的信息,错误,警告,有时只返回0.这是什么意思?

请提出任何建议

日志显示:

  

INFO 01-Jun-2011 11:48:18.SSS 8.Global - Level = Information,eventTime = 5/20/2011 3:19:08 PM    INFO 01-Jun-2011 11:48:18.SSS 8.全球 - 等级= 0,eventTime = 5/20/2011 3:19:16 PM

2 个答案:

答案 0 :(得分:1)

只有当您的事件日志以某种方式损坏时才会发生这种情况。当您尝试使用eventvwr.msc查看此事件时会发生什么?我见过很多腐败的事件日志。当通常同时记录许多事件时,通常会发生这种情况。我从来没有找到一个明确的复制品,但即使在Windows Server 2008上也会发生这种情况,尽管整个事件日志子系统已被重写。

答案 1 :(得分:0)

System.Diagnostics.EventLogEntryType枚举定义了五个值:ErrorFailureAuditInformationSuccessAuditWarning。但是如果你看看documentation to native WinAPI ReportEvent function,你会看到实际上有六种类型的事件。代码为0的事件为EVENTLOG_SUCCESS,其描述与EVENTLOG_INFORMATION_TYPE相同。它也与事件查看器的“信息”显示相同。

因此,logEntry.EntryType.ToString()==“0”可能意味着事件的记录类型为EVENTLOG_SUCCESS