首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FileLoadException时使用NLog

使用FileLoadException时使用NLog
EN

Stack Overflow用户
提问于 2017-05-22 20:36:33
回答 1查看 1.2K关注 0票数 1

我正在开发API,其中我使用NLog记录传入的请求及其响应。偶尔,我会在服务器上看到以下错误

代码语言:javascript
复制
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看起来像这样

代码语言:javascript
复制
<?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在多个并发线程中运行。

谢谢你的建议

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-27 22:49:30

同一应用程序中的多个线程不应出现任何问题,因为它们只会写入异步队列(而不是文件)。但是,如果有多个应用程序写入同一个文件,那么您可能会遇到问题。

如果您有外部应用程序,可以监视日志文件并尝试将内容复制到其他地方(Ex ),您也会遇到这个问题。(扣篮)

如果没有多个应用程序写入同一个文件,那么只需将这些选项添加到文件目标:

代码语言:javascript
复制
<target xsi:type="File" 
        fileName="${logPath}/${shortdate}.trace.log" 
        layout="${rowFormatInfo}" 
        keepFileOpen="true" 
        concurrentWrites="false" />

如果您确实有多个应用程序/应用程序域写入同一个文件,那么升级到最新的NLog (当前为4.4.10),并在文件目标上使用这些选项:

代码语言:javascript
复制
<target xsi:type="File" 
        fileName="${logPath}/${shortdate}.trace.log" 
        layout="${rowFormatInfo}" 
        keepFileOpen="true" 
        concurrentWrites="true" />
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44121736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档