-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only log application errors #50
Comments
Part of the solution is to create an Exception Filter. The filter should behave as:
// Partial implemenation of the exception filter
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NCI.OCPL.Api.Common;
public class NCIResponseExceptionFilter : IActionFilter, IOrderedFilter
{
public int Order => int.MaxValue - 10;
public void OnActionExecuting(ActionExecutingContext context) { }
public void OnActionExecuted(ActionExecutedContext context)
{
if (context.Exception is APIErrorException httpResponseException)
{
context.Result = new ObjectResult(new { Message = httpResponseException.Message })
{
StatusCode = httpResponseException.HttpStatusCode
};
context.ExceptionHandled = true;
}
}
}
// Register the filter in NciStartupBase
services.AddControllers(options => {
options.Filters.Add<NCIResponseExceptionFilter>();
}); |
Set up logging to the Windows event viewer with code along the lines of this in public static IHostBuilder CreateHostBuilder (string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging (logging =>
{
logging.AddEventLog (new EventLogSettings
{
SourceName = "YourSourceName",
LogName = "Application"
}):
})
.ConfigureWebHostDefaults (webBuilder =>
{
webBuilder. UseStartup<Startup> ( ) :
}): |
This was referenced May 12, 2023
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Issue description
Both application and user errors are being indiscriminately written to the Windows Application log.
This is because our controllers throw
APIErrorException
for errors and use a global exception handler to return a suitable HTTP status code. Despite the exception handler in the startup, these exceptions are considered unhandled exceptions and logged in the Windows event log, regardless of whether the user was due to application logic or something the user did.What's the expected change?
What's the current functionality?
What's the updated acceptance criteria?
Additional details / screenshot
Related Tickets
The text was updated successfully, but these errors were encountered: