【发布时间】:2012-07-19 17:24:46
【问题描述】:
我正在关注this 的帖子并尝试运行代码(复制如下),但我遇到了一个稍微不同的问题。我可以远程连接到服务器并通过事件查看器程序查看事件日志,但我无法遍历代码中的事件。我收到一个 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:
【问题讨论】:
-
这可能与您在远程计算机上运行并且没有管理员帐户或访问查看/打开事件日志的权限有关吗?您是否成功连接到远程服务器..?您是否需要在服务器名称前加上@"\\" ..?这也需要用户名、密码、域..?
-
如果你想使用win32APIpinvoke.net/default.aspx/advapi32.LogonUser查看这个网站
-
检查 ErrorCode 属性。您可以通过在网络上查找该数字的十六进制表示来获得一些额外的信息。
-
@JamieSee,正如我上面提到的,错误表明没有错误代码。
-
@DJKRAZE,我可以很好地连接到机器(通过远程连接和代码)。问题是通过代码读取日志。
标签: c# permissions event-log