我正在关注这篇文章并尝试运行代码(复制如下),但我遇到了一个稍微不同的问题。我可以远程连接到服务器并通过事件查看器程序查看事件日志,但我无法遍历代码中的事件。我收到一个 InvalidOperationException 说“无法在机器上打开日志 EventLogName。Windows 没有提供错误代码。” 还有一个 System.ComponentModel.Win32Exception 类型的内部异常,显示“访问被拒绝”。
private static bool GetEventLogData(DateTime start)
{
var log = new EventLog("EventLogName", "SERVER.domain.net");
bool errorFound = false;
foreach (EventLogEntry entry in log.Entries)
{
if ((entry.EntryType == EventLogEntryType.Error) &&
(entry.TimeGenerated >= start))
{
Console.WriteLine("Error in Event Log:\n" + entry.Message + "\n");
errorFound = true;
}
}
return errorFound;
}
有任何想法吗?
编辑:
异常数据如下。我无法发布服务器名称,因为它是公司信息。尝试读取事件日志时收到错误消息。我绝对确定我可以阅读日志,因为我可以远程连接我的帐户并使用事件查看器阅读日志。
System.InvalidOperationException was unhandled
Message=Cannot open log EventLogName on machine SERVER.domain.net. Windows has not provided an error code.
Source=System
StackTrace:
at System.Diagnostics.EventLogInternal.OpenForRead(String currentMachineName)
at System.Diagnostics.EventLogInternal.GetEntryAtNoThrow(Int32 index)
at System.Diagnostics.EventLogEntryCollection.EntriesEnumerator.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
at MyApp.Program.GetEventLogData(String machineName, DateTime start) in c:\users\me\documents\visual studio 2010\Projects\MyApp\MyApp\Program.cs:line 45
at MyApp.Program.Main(String[] args) in c:\users\me\documents\visual studio 2010\Projects\MyApp\MyApp\Program.cs:line 28
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.ComponentModel.Win32Exception
Message=Access is denied
ErrorCode=-2147467259
NativeErrorCode=5
InnerException: