Prevent catched & handled exception from windows event log entry

Viewed 34

while catch and handle a duplicate entry-exception to continue with the app-workflow, i would like to prevent the windows event log to insert the exception.

to be clear: i dont want to turn off exception logging globaly in my asp.net core application. i only want to prevent logging on a specific codespace and specific exceptiontype.

ideas?

 bool success = false;
        try
        {
            await dbcontext.AddAsync(order);
            await dbcontext.SaveChangesAsync();
            success = true;
        }
        catch (DbUpdateException ex)
        {
            if (ex.InnerException != null)
            {
                if (ex.InnerException.Message.Contains("Duplicate"))
                {
                    success = false;
                }
            }
        }
        return success;
1 Answers

In a standard web app there are already two different "code space" logging level settings. If you create your own logging broker for your code space you can do the same.

  "Logging": {
    "LogLevel": {
      "Default": "Warning",
      "Microsoft.AspNetCore": "Warning"
    }
  }

As the exception is already caught and logged via a library you need to set its logging level to none and catch at a higher level or use a filter.

Related