1

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.

cjablonski76
  • 173
  • 1
  • 13

2 Answers2

0

İf you didn't try, you can use ILogger interface or Serilog package. You can find helper documentation from the below links.

Logging in .NET Core and ASP.NET Core

Serilog

  • Do you happen to know with any certainty whether Serilog will capture these types of exceptions by chance? I will do a POC with Serilog to see for sure, but still curious if you already know the answer before I even put pen to paper :) – cjablonski76 Jul 22 '21 at 20:40
0

To get the unhandled exceptions like this, ensure you are calling UseExceptionHandler

In the below link search for "Catches and logs unhandled exceptions"

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-5.0

In the startup class, look for the function

public void Configure

In that function, ensure you called UseExceptionHandler as shown below, as mentioned in the document.

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34