从控制器操作返回文件(MemoryStream)时内部 nlog 异常
Exception in internal-nlog when returning a File(MemoryStream) from a controller action
将我的应用程序更新到 .net core 3.1 后,我开始注意到 internal-nlog 文件中的新日志,例如:
2020-07-23 23:49:07.0858 Warn Failed to get property value for object: System.IO.MemoryStream Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: Timeouts are not supported on this stream.
at System.IO.Stream.get_ReadTimeout()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at NLog.Internal.ObjectReflectionCache.ObjectPropertyList.Enumerator.get_Current()
应用程序本身可以正常运行,只是弹出此日志。
我设法追踪到从 4.5.6 到 4.7.2 的 nlog 更新。经过一番尝试后,我得出的结论是日志条目没有出现在 nlog 4.5.11 上,它开始出现在 nlog 4.6.0
上
日志条目在退出特定控制器操作后写入(两次)。可以概括为:
var stream = generateMemoryStream();
return this.File(stream,mediatype,somefilename);
可能相关的 nlog 配置块如下
<target xsi:type="File" name="jsonFile" fileName="Logs\nlog-json-${shortdate}.log" >
<layout type="JsonLayout">
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level}"/>
<attribute name="message" layout="${message}"/>
<attribute name="templatedMessage" layout="${message:raw=true}" />
<attribute name="exception" encode="false">
<layout type='JsonLayout'>
<attribute name="message" layout="${exception:format=message}" />
<attribute name="type" layout="${exception:format=shortType}" />
<attribute name="stackTrace" layout="${exception:format=stackTrace}" />
<attribute name="data" layout="${exception:format=Data}" />
</layout>
</attribute>
<attribute name="eventProperties" encode="false">
<layout type='JsonLayout' includeAllProperties="true" maxRecursionLimit="2"/>
</attribute>
</layout>
</target>
有没有其他人注意到这种行为?或者可以给我指明方向吗?
在 NLog 4.7.3 发布之前,您可以使用以下代码防止流对象的反射:
NLog.LogManager.Setup().SetupSerialization(s =>
s.RegisterObjectTransformation<System.IO.Stream>(o => o.ToString()));
这将消除来自 NLog 尝试执行 Stream
-对象的 属性- 反射的噪音。
将我的应用程序更新到 .net core 3.1 后,我开始注意到 internal-nlog 文件中的新日志,例如:
2020-07-23 23:49:07.0858 Warn Failed to get property value for object: System.IO.MemoryStream Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: Timeouts are not supported on this stream.
at System.IO.Stream.get_ReadTimeout()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at NLog.Internal.ObjectReflectionCache.ObjectPropertyList.Enumerator.get_Current()
应用程序本身可以正常运行,只是弹出此日志。
我设法追踪到从 4.5.6 到 4.7.2 的 nlog 更新。经过一番尝试后,我得出的结论是日志条目没有出现在 nlog 4.5.11 上,它开始出现在 nlog 4.6.0
上日志条目在退出特定控制器操作后写入(两次)。可以概括为:
var stream = generateMemoryStream();
return this.File(stream,mediatype,somefilename);
可能相关的 nlog 配置块如下
<target xsi:type="File" name="jsonFile" fileName="Logs\nlog-json-${shortdate}.log" >
<layout type="JsonLayout">
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level}"/>
<attribute name="message" layout="${message}"/>
<attribute name="templatedMessage" layout="${message:raw=true}" />
<attribute name="exception" encode="false">
<layout type='JsonLayout'>
<attribute name="message" layout="${exception:format=message}" />
<attribute name="type" layout="${exception:format=shortType}" />
<attribute name="stackTrace" layout="${exception:format=stackTrace}" />
<attribute name="data" layout="${exception:format=Data}" />
</layout>
</attribute>
<attribute name="eventProperties" encode="false">
<layout type='JsonLayout' includeAllProperties="true" maxRecursionLimit="2"/>
</attribute>
</layout>
</target>
有没有其他人注意到这种行为?或者可以给我指明方向吗?
在 NLog 4.7.3 发布之前,您可以使用以下代码防止流对象的反射:
NLog.LogManager.Setup().SetupSerialization(s =>
s.RegisterObjectTransformation<System.IO.Stream>(o => o.ToString()));
这将消除来自 NLog 尝试执行 Stream
-对象的 属性- 反射的噪音。