C#应用程序崩溃原因

4

我正在调查我的一个应用程序发生的崩溃:我已经发现这是因为我订阅了EventLog.EntryWritten事件来监控EntryWritten事件的事件日志。我一直在查看Reflector中的EventLog类,并在WinDBG中跟踪调用堆栈,以查看我的应用程序失败的原因。首先,这是WinDBG中!eestack的输出,围绕着我的应用程序失败的地方:

0632e620 74c9e900 mscorwks!StrongNameErrorInfo+0x103e4, calling mscorwks!GetMetaDataInternalInterface+0x7f70
0632e694 74c9e855 mscorwks!StrongNameErrorInfo+0x10339, calling mscorwks+0x31c0
0632e6a0 74215058 (MethodDesc 0x7402b7c4 +0x18 System.Security.CodeAccessPermission.RevertAssert()), calling (MethodDesc 0x740c84fc +0 System.Security.SecurityRuntime.RevertAssert(System.Threading.StackCrawlMark ByRef))
0632e6ac 73df4598 (MethodDesc 0x738bc65c +0xa0 System.Diagnostics.SharedUtils.CreateSafeWin32Exception(Int32)), calling (MethodDesc       0x7402b7c4 +0 System.Security.CodeAccessPermission.RevertAssert())
0632e6e4 73ee6fa0 (MethodDesc 0x738e064c System.Diagnostics.EventLog.get_OldestEntryNumber()), calling mscorwks!StrongNameErrorInfo+0x1031b
0632e6f4 73df24ed (MethodDesc 0x738e06e8 +0x1bd System.Diagnostics.EventLog.CompletionCallback(System.Object)), calling (MethodDesc    0x738e064c +0 System.Diagnostics.EventLog.get_OldestEntryNumber())
0632e728 74bb8cef mscorwks!CoUninitializeEE+0x5687, calling mscorwks!CoUninitializeEE+0x5613
0632e73c 73df0fe4 (MethodDesc 0x738e096c +0x94 System.Diagnostics.EventLog.StaticCompletionCallback(System.Object, Boolean)), calling 739443d4

大致上,根据我在Reflector中看到的内容,这是OldestEntryNumber的get访问器的一部分:

if (!UnsafeNativeMethods.GetOldestEventLogRecord(this.readHandle, number))
{
    throw SharedUtils.CreateSafeWin32Exception();
}

这个Win32Exception是导致我的应用程序崩溃的原因。查看UnsafeNativeMethods.GetOldestEventLogRecord(...)方法:

[DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern bool GetOldestEventLogRecord(SafeHandle hEventLog, int[] number);

我猜想我的问题可能是以下两个方面之一:
1. GetOldestEventLogRecord(...) 方法出现某些错误。 2. 系统无法访问/加载 advapi32.dll。这可能是为什么我在 !eestack 输出中看到 StrongNameErrorInfo+0x1031b(有可能是在调用该方法时)的原因吗?
如果您对此有任何想法或帮助,将不胜感激!
1个回答

0

你的 P/Invoke 似乎有误。

尝试将其更改为:

[DllImport ("advapi32.dll", SetLastError=true)]
public static extern int GetOldestEventLogRecord (IntPtr hEventLog, ref int OldestRecord);

顺便提一下,还有一种获取这些信息的替代方法:使用托管的EventLog类。
System.Diagnostics.EventLogEntryCollection

1
我无法更改它,我正在使用Reflector查看此代码,它是所有的.NET代码,这一部分来自.NET中的UnsafeNativeMethods类。 - DukeOfMarmalade
你的意思是什么?所有这些代码都是由我的应用程序中这行代码调用的:eventLog.EntryWritten += new EntryWrittenEventHandler(EventLogMonitor); 因为我正在订阅EntryWritten事件。所以这都是.NET在幕后处理它的实现方式。我不能改变其中任何内容,我只是想看看为什么出了问题。 - DukeOfMarmalade
1
你能提供更详细的代码吗?或者在一个单独的类示例中重现它吗? - WoLfulus
我真的无法解决这个问题,因为它并不是可重现的。我的应用程序在运行了大约一周后,偶尔会出现这个问题而崩溃。我已经跟踪了调用堆栈,原因就在这附近,但都是.NET程序集,我无法更改。 - DukeOfMarmalade

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接