We recently found that our application is throwing System.AccessViolationException
when utilizing some third party library doing some unmanaged memory access.
I will certainly try to work with the third party to get a fix from them, this post isn't for that though :).
I am hoping for some insights on how I can actually get this exception information into my logs. We're running Asp.net Core 5.
We only found this error by enabling stdoutLogEnabled
:https://learn.microsoft.com/en-us/aspnet/core/test/troubleshoot-azure-iis?view=aspnetcore-5.0#aspnet-core-module-stdout-log-iis. However, this isn't recommended for production environments.
So my current question is, how can I actually capture the same log message stdoutLogEnabled=true
was able to give us, by using some other logging provider, i.e. Log4Net
.
I have the following in my appsettings:
"Logging": {
"LogLevel": {
"Default": "Trace",
}
},
And yet, when I run some unsafe code to to simulate the System.AccessViolationException
I do not see if being logged in any of my log4net
appenders.
I don't specifically have to get this working for log4net, that is just what we are using today. In general I would like to be able to capture this log using some advanced log provider so that it could potentially log somewhere other than a local file on the server.
Sample log output form the stdoutLogEnabled
on the server
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()
Edit: Adding more information. I did read this similar post: Gracefully handling corrupted state exceptions in .NET Core Which does not bode well for my problem. My question does differ though, because I don't care to catch the exceptions, I'm ok with the application crashing. I just want to ensure we can log this information to our log provider of choice when these errors do occur.