我正在开发API,其中我使用NLog记录传入的请求及其响应。偶尔,我会在服务器上看到以下错误
Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileLoadException
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32, IntPtr)
at NLog.Internal.FileAppenders.BaseFileAppender.WindowsCreateFile(System.String, Boolean)
at NLog.Internal.FileAppenders.BaseFileAppender.TryCreateFileStream(Boolean)
at NLog.Internal.FileAppenders.BaseFileAppender.CreateFileStream(Boolean)
at NLog.Internal.FileAppenders.RetryingMultiProcessFileAppender.Write(Byte[])
at NLog.Targets.FileTarget.WriteToFile(System.String, NLog.LogEventInfo, Byte[], Boolean)
at NLog.Targets.FileTarget.ProcessLogEvent(NLog.LogEventInfo, System.String, Byte[])
at NLog.Targets.FileTarget.FlushCurrentFileWrites(System.String, NLog.LogEventInfo, System.IO.MemoryStream, System.Collections.Generic.List`1<NLog.Common.AsyncContinuation>)
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.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.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()我的nlog.config看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="true">
<variable name="logPath" value="${basedir}/App_Data/Logs"/>
<variable name="rowFormatInfo" value="${longdate} | ${level:uppercase=true} | ${message} | Thread: ${threadid}" />
<targets async="true">
<target name="traceFile" xsi:type="AsyncWrapper" overflowAction="Grow">
<target xsi:type="File" fileName="${logPath}/${shortdate}.trace.log" layout="${rowFormatInfo}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" maxlevel="Debug" writeTo="traceFile" />
</rules>
</nlog>在我看来,问题在于我使用的是异步包装器,它可能导致多个线程混合,试图访问日志文件。有什么解决办法来防止这种错误发生在我的API必须每秒处理数十个请求的高峰时间吗?我不认为切换到同步日志记录会有帮助,因为API在多个并发线程中运行。
谢谢你的建议
发布于 2017-05-27 22:49:30
同一应用程序中的多个线程不应出现任何问题,因为它们只会写入异步队列(而不是文件)。但是,如果有多个应用程序写入同一个文件,那么您可能会遇到问题。
如果您有外部应用程序,可以监视日志文件并尝试将内容复制到其他地方(Ex ),您也会遇到这个问题。(扣篮)
如果没有多个应用程序写入同一个文件,那么只需将这些选项添加到文件目标:
<target xsi:type="File"
fileName="${logPath}/${shortdate}.trace.log"
layout="${rowFormatInfo}"
keepFileOpen="true"
concurrentWrites="false" />如果您确实有多个应用程序/应用程序域写入同一个文件,那么升级到最新的NLog (当前为4.4.10),并在文件目标上使用这些选项:
<target xsi:type="File"
fileName="${logPath}/${shortdate}.trace.log"
layout="${rowFormatInfo}"
keepFileOpen="true"
concurrentWrites="true" />https://stackoverflow.com/questions/44121736
复制相似问题