我正在做一些简单的事情,我希望24小时或更短时间内出现系统和应用程序错误。
这是我的代码...
string machine = ReadEventViewerTaskSettings.Settings.Machine;
var aLog = new EventLog(log, machine);
var twentyFourHoursAgo = DateTime.Now.AddHours(-24); // not sure if this should be UTC or local, change to UtcNow if applicable
List<EventLogEntry> entries = (from entry in aLog.Entries.Cast<EventLogEntry>()
where entry.TimeWritten >= twentyFourHoursAgo &&
entry.EntryType == EventLogEntryType.Error
orderby entry.TimeWritten descending
select entry).ToList();
const char weirdChar = (char)3;
foreach (EventLogEntry entry in entries)
{
if (entry != null)
{
try
{
Name = Resources.EntryName;
Value = string.Format(
"[EntryType] " + entry.EntryType.ToString() + " <br />" +
Resources.IndexLabel + entry.Index.ToString() + "<br />" +
Resources.EventIDLabel + entry.InstanceId.ToString() + "<br />" +
Resources.TimeWrittenLabel + entry.TimeWritten.ToString() + "<br />" +
Resources.MachineNameLabel + entry.MachineName.ToString() + "<br />" +
Resources.SourceLabel + entry.Source.ToString() + "<br />" +
Resources.UserNameLabel + entry.UserName.ToString() + "<br />" +
Resources.MessageLabel + entry.Message.Replace(weirdChar.ToString(), ""));
}
catch (Exception ex)
{
}
}
}
似乎(有时)事件查看器存在解析问题。我在网上找到了这个“ wierdChar”。还有其他吗?
还是有更好的方法?我只想显示事件查看器信息