[英]c# Reading event log of remote server
我正在關注這篇文章,並嘗試運行代碼(復制在下面),而我遇到了一個稍微不同的問題。 我可以遠程連接到服務器並通過Event Viewer程序查看事件日志,但無法遍歷代碼中的事件。 我收到一個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:
這很可能是策略許可問題。 請參閱授予非管理員權限,以閱讀 TechNet上的Windows 2003和Windows 2008博客事件日志 。 完成此操作后,我就能使您的代碼正常工作。 對於Windows 2008 Server和更高版本,只需將用戶添加到“事件日志讀取器”本地安全組即可。 在我這樣做之前,我一直遇到相同的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.