进程因未处理的异常而终止 - WCF 服务
The process was terminated due to an unhandled exception - WCF service
所以我创建了一个 WCF 服务,它从客户那里获取消息并将它们解析为所需的输出,然后通过 TCP/HTTP/FTP 等将它们发送给其他客户。
此 windows 服务为每个使用 TPL 创建的客户提供了很长的 运行 线程。
所以对于日志记录,我使用了 NLOG,使用以下配置记录到文件和事件查看器
<target xsi:type="File" name="flatfile" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring,StackTrace}"
archiveAboveSize="2000000" archiveFileName="${basedir}/logs/archive/${shortdate}-{#####}.engine.log" archiveNumbering="Sequence" archiveEvery="None"
maxArchiveFiles="100" fileName="${basedir}/logs/engine.current.log" keepFileOpen="true" concurrentWrites="true" />
<target xsi:type="EventLog" name="eventlog" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring} ${StackTrace}" log="Application" source="Nuance Interface Engine Service" eventId="${event-properties:EventID}" />
即使我添加了 concurrentWrites="true",当我在 20 多个小时后启动 WCF 服务时,我在事件查看器中出现以下错误
Application: MyWcfService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.OutOfMemoryException
at System.Text.StringBuilder..ctor(System.String, Int32, Int32, Int32)
at NLog.Layouts.SimpleLayout.GetFormattedMessage(NLog.LogEventInfo)
at NLog.Targets.FileTarget.GetFormattedMessage(NLog.LogEventInfo)
at NLog.Targets.FileTarget.GetBytesToWrite(NLog.LogEventInfo)
at NLog.Targets.FileTarget.Write(NLog.Common.AsyncLogEventInfo[])
at NLog.Targets.Target.WriteAsyncLogEvents(NLog.Common.AsyncLogEventInfo[])
at NLog.Targets.Wrappers.AsyncTargetWrapper.ProcessPendingEvents(System.Object)
at System.Threading.TimerQueueTimer.CallCallbackInContext(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireQueuedTimerCompletion(System.Object)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
你能告诉我这个问题发生在哪里吗,我认为多个线程正在访问这个日志文件,因此我将 concurrentWrites="true" 属性添加到 nlog 的文件目标。
在出现上述错误 1 秒后,我在事件查看器中看到另一个错误。
Faulting application name: Hl7ic.Engine.View.exe, version: 18.0.1.160, time stamp: 0x5af5cd1f
Faulting module name: KERNELBASE.dll, version: 6.3.9600.18938, time stamp: 0x5a7dd8a7
Exception code: 0xe0434352
Fault offset: 0x00015ef8
Faulting process id: 0x1074
Faulting application start time: 0x01d3ea7338d9851c
Faulting application path: C:\Program Files (x86)\MyServices\MyWcfService.exe
Faulting module path: C:\windows\SYSTEM32\KERNELBASE.dll
Report Id: 59b36929-5688-11e8-80ca-005056a80aaa
Faulting package full name:
Faulting package-relative application ID:
OutOfMemoryException
表示机器内存不足。也许它已经被另一个应用程序用完了。也许它已经被你的应用程序用完了。也许您的 32 位应用程序已经用完了 2 GB 的内存地址 space。
要诊断此错误,请使用 Process Explorer 捕获使用过多内存的应用程序的 Fulldump。然后尝试使用 Visual Studio 或 WinDbg 来调查内存转储文件。
要从雷达中删除 NLog,然后您可以升级到 NLog 4.4.6(或更新版本)。它将减少内存分配与 70 pct。来自旧版本(使用 FileTarget 时)
如果 运行 NLog 4.4.6(或更新版本)那么您可以通过不使用 <targets async="true">
而是使用 default-wrapper
:
来进一步优化 NLog
<targets>
<default-wrapper xsi:type="AsyncWrapper" timeToSleepBetweenBatches="0" />
<target .... />
<target .... />
</targets>
所以我创建了一个 WCF 服务,它从客户那里获取消息并将它们解析为所需的输出,然后通过 TCP/HTTP/FTP 等将它们发送给其他客户。 此 windows 服务为每个使用 TPL 创建的客户提供了很长的 运行 线程。
所以对于日志记录,我使用了 NLOG,使用以下配置记录到文件和事件查看器
<target xsi:type="File" name="flatfile" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring,StackTrace}"
archiveAboveSize="2000000" archiveFileName="${basedir}/logs/archive/${shortdate}-{#####}.engine.log" archiveNumbering="Sequence" archiveEvery="None"
maxArchiveFiles="100" fileName="${basedir}/logs/engine.current.log" keepFileOpen="true" concurrentWrites="true" />
<target xsi:type="EventLog" name="eventlog" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring} ${StackTrace}" log="Application" source="Nuance Interface Engine Service" eventId="${event-properties:EventID}" />
即使我添加了 concurrentWrites="true",当我在 20 多个小时后启动 WCF 服务时,我在事件查看器中出现以下错误
Application: MyWcfService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.OutOfMemoryException
at System.Text.StringBuilder..ctor(System.String, Int32, Int32, Int32)
at NLog.Layouts.SimpleLayout.GetFormattedMessage(NLog.LogEventInfo)
at NLog.Targets.FileTarget.GetFormattedMessage(NLog.LogEventInfo)
at NLog.Targets.FileTarget.GetBytesToWrite(NLog.LogEventInfo)
at NLog.Targets.FileTarget.Write(NLog.Common.AsyncLogEventInfo[])
at NLog.Targets.Target.WriteAsyncLogEvents(NLog.Common.AsyncLogEventInfo[])
at NLog.Targets.Wrappers.AsyncTargetWrapper.ProcessPendingEvents(System.Object)
at System.Threading.TimerQueueTimer.CallCallbackInContext(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireQueuedTimerCompletion(System.Object)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
你能告诉我这个问题发生在哪里吗,我认为多个线程正在访问这个日志文件,因此我将 concurrentWrites="true" 属性添加到 nlog 的文件目标。
在出现上述错误 1 秒后,我在事件查看器中看到另一个错误。
Faulting application name: Hl7ic.Engine.View.exe, version: 18.0.1.160, time stamp: 0x5af5cd1f
Faulting module name: KERNELBASE.dll, version: 6.3.9600.18938, time stamp: 0x5a7dd8a7
Exception code: 0xe0434352
Fault offset: 0x00015ef8
Faulting process id: 0x1074
Faulting application start time: 0x01d3ea7338d9851c
Faulting application path: C:\Program Files (x86)\MyServices\MyWcfService.exe
Faulting module path: C:\windows\SYSTEM32\KERNELBASE.dll
Report Id: 59b36929-5688-11e8-80ca-005056a80aaa
Faulting package full name:
Faulting package-relative application ID:
OutOfMemoryException
表示机器内存不足。也许它已经被另一个应用程序用完了。也许它已经被你的应用程序用完了。也许您的 32 位应用程序已经用完了 2 GB 的内存地址 space。
要诊断此错误,请使用 Process Explorer 捕获使用过多内存的应用程序的 Fulldump。然后尝试使用 Visual Studio 或 WinDbg 来调查内存转储文件。
要从雷达中删除 NLog,然后您可以升级到 NLog 4.4.6(或更新版本)。它将减少内存分配与 70 pct。来自旧版本(使用 FileTarget 时)
如果 运行 NLog 4.4.6(或更新版本)那么您可以通过不使用 <targets async="true">
而是使用 default-wrapper
:
<targets>
<default-wrapper xsi:type="AsyncWrapper" timeToSleepBetweenBatches="0" />
<target .... />
<target .... />
</targets>