我们最近发现我们的应用程序System.AccessViolationException
在使用一些第三方库进行一些非托管内存访问时会抛出异常。我当然会尝试与第三方合作以从他们那里获得修复,但这篇文章不是为了那个:)。
我希望对如何将异常信息实际获取到我的日志中获得一些见解。我们正在运行 Asp.net Core 5。我们仅通过启用才发现此错误stdoutLogEnabled
:https ://docs.microsoft.com/en-us/aspnet/core/test/troubleshoot-azure-iis?view=aspnetcore-5.0# aspnet-core-module-stdout-log-iis。但是,不建议将其用于生产环境。所以我目前的问题是,我如何才能 stdoutLogEnabled=true
通过使用其他一些日志记录提供程序(即Log4Net
.
我的 appsettings 中有以下内容:
"Logging": {
"LogLevel": {
"Default": "Trace",
}
},
然而,当我运行一些不安全的代码来模拟时,System.AccessViolationException
我看不到是否登录了我的任何log4net
附加程序。
我不需要专门为 log4net 工作,这正是我们今天使用的。一般来说,我希望能够使用一些高级日志提供程序来捕获此日志,以便它可能在服务器上的本地文件以外的地方记录。
stdoutLogEnabled
服务器上的示例日志输出形式
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at +.(IntPtr, System.String, Byte[], Int32, IntPtr ByRef, Int32 ByRef, IntPtr ByRef, System.String ByRef)
--------------------------------
at d.( , System.String, System.IO.Stream)
at ThirdPartyClass.Save(System.String, System.IO.Stream)
at FirstPartyClass.CreateImage()
at FirstPartyClass.Execute()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
编辑:添加更多信息。我确实读过这篇类似的文章:Gracefully handling corrupted state exceptions in .NET Core 这对我的问题来说不是个好兆头。我的问题确实不同,因为我不在乎捕捉异常,我对应用程序崩溃没意见。我只是想确保当这些错误确实发生时,我们可以将此信息记录到我们选择的日志提供程序中。