由于未处理的异常 - WCF服务,该进程终止

时间:2018-05-17 09:46:36

标签: c# wcf windows-services nlog

所以我创建了一个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 =&#34; true&#34;,当我在超过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 =&#34; true&#34;属性为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:

1 个答案:

答案 0 :(得分:2)

OutOfMemoryException表示机器上没有足够的内存。也许它已经被另一个应用程序用完了。也许它已经被你的应用程序用完了。也许你的32位应用程序耗尽了2 GB的内存地址空间。

要诊断此错误,请使用Process Explorer捕获使用过多内存的应用程序的Fulldump。然后尝试使用Visual Studio或WinDbg来调查内存转储文件。

要从雷达中删除NLog,您可以升级到NLog 4.4.6(或更新版本)。它将减少70 pct的内存分配。从旧版本(使用FileTarget时)

如果运行NLog 4.4.6(或更新版本),那么您可以通过不使用<targets async="true">进一步优化NLog,而是使用此default-wrapper

<targets>
   <default-wrapper xsi:type="AsyncWrapper" timeToSleepBetweenBatches="0" />

   <target .... />

   <target .... />

</targets>

另见https://github.com/NLog/NLog/wiki/Performance

相关问题