我遇到了一个非常令人沮丧的问题。我正在写一个Windows服务,它的作用类似于TCPServer(我不确定技术术语,但该服务通过一组客户端之间的tcp处理通信)。
我的问题是服务随机崩溃(至少我不知道导致异常的原因)并且Windows应用程序日志包含一个VsJITDebugger(EventID 4096)错误消息说:
An unhandled win32 exception occurred in SDUNoteTCPServerService.exe [1028]. Just-In- Time debugging this exception failed with the following error: Debugger could not be started because no user is logged on.
我不是100%确定VsJITDebugger是什么,但据我通过Google搜索来发现VsJITDebugger是一种使用Visual Studio来澄清未处理异常的工具。有时我也会在VsJITDebugger错误消息之前看到以下.Net运行时(EventID:1026)错误消息:
Application: SDUNoteTCPServerService.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.ComponentModel.Win32Exception Stack: at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32, UInt16, System.Diagnostics.EventLogEntryType, System.String[], Byte[], System.String) at System.Diagnostics.EventLogInternal.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32, Int16, Byte[]) at System.Diagnostics.EventLog.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32) at TCPServer.TCPServerListener+AsynchronousSocketListener.WriteCustomSocketObjectMessagesToLog (System.String, System.Diagnostics.EventLogEntryType, Int32) at TCPServer.TCPServerListener+CustomSocketObject.SendMessageToClientThread() at System.Threading.ThreadHelper.ThreadStart_Context(System.Object) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at System.Threading.ThreadHelper.ThreadStart()
如.Net运行时错误消息中所述,当我尝试在EventLog中写入有关捕获的异常的详细信息时,将引发未处理的错误。例外是总是在套接字的BeginReceive方法的回调函数中抛出。
我不知道为什么这个异常首先崩溃服务,因为我在Socket.BeginReceive方法的回调函数中使用try {} catch(Exception)捕获泛型异常,但是我无法提取有关被捕获异常的任何信息,因为这个未处理的异常会被抛出。
任何想法可能是什么问题?所有回复都表示赞赏。
注意:该服务正在Windows Server 2003(SP2)上运行。安装了Visual Studio 2008。
编辑:WriteCustomSocketObjectMessagesToLog如下所示(eventlog对象是AsynchronousSocketListener类的属性):
private void WriteExceptionToLog(string customstartmessage, Exception ex) { try { eventlog.WriteEntry(customstartmessage, EventLogEntryType.Error); eventlog.WriteEntry(ex.Message, EventLogEntryType.Error, 5); eventlog.WriteEntry(ex.Source, EventLogEntryType.Error, 5); eventlog.WriteEntry(ex.StackTrace, EventLogEntryType.Error, 5); } catch (Exception) { eventlog.WriteEntry("Failed reporting exception", EventLogEntryType.Error); } }
编辑2:
我弄清楚问题是什么。报告的事件日志已满。难怪我当时无法写信。我通过将类库转换为控制台应用程序找到了问题。当我开始编写TCPServer类时,我也这样做了,但当时我使用WriteLine方法向Console写错误。这次控制台应用程序写入创建的事件日志,未处理的异常被写入控制台(如果这是控制台应用程序的默认行为,我不这样做,因为我没有做任何编码)。我的Windows服务中未处理的错误很可能是这样的:
Unhandled Exception: System.ComponentModel.Win32Exception: The event log file is full at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName) at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at System.Diagnostics.EventLog.WriteEntry(String message) at TCPServer.Program.Main(String[] args)
现在我只需要处理这个问题。处理此错误的更好解决方案是什么?每次抛出错误时以编程方式保存和清空事件日志?不加限制地增加事件日志大小(这可能并且是一个好主意吗?)?还有其他建议吗?
答案 0 :(得分:1)
正如在第二次编辑中所写,我发现问题是在尝试写入时事件日志已满。我记录A LOT并且EventLog的默认行为是MaximumKilobytes为512左右,保留条目超过7天。我更改了配置,以便MaximumKilobytes等于25兆字节并根据需要覆盖条目。
答案 1 :(得分:0)
为AppDomain UnhandledException事件定义处理程序:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
void MyHandler(object sender, UnhandledExceptionEventArgs e)
{
// See the exception context here using e.ExceptionObject
}
答案 2 :(得分:0)
如果要捕获异常,则需要在AsynchronousSocketListener.WriteCustomSocketObjectMessagesToLog
中添加异常处理程序。 AsynchronousSocketListener
类嵌套在TCPServerListener
命名空间中的TCPServer
内。我认为这个课程是你或你的组织实施的。
不幸的是,在尝试写入事件日志的某些代码中抛出了异常,因此如果异常的根本原因是内存耗尽,捕获异常并尝试记录它可能会有些困难。
查看InternalWriteEvent
类中System.Diagnostics.EventLogInternal
的.NET 4.0源代码,似乎只有一个地方可以抛出Win32Exception
,即调用{ {3}}功能失败。
Win32Exception
将包含Windows错误代码。根据{{1}}文档,可能有四个错误代码:
发现ReportEvent
的错误代码应该有助于您了解问题的根源。