In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use ExceptionFilter filter in asp.net", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use ExceptionFilter filter in asp.net" this article.
Brief introduction
An exception filter, as its name implies, is the filter used when an exception occurs in a program. Used for handling when an uncaught exception occurs in the system.
Implement a custom exception filter
Customizing a global exception filter requires implementing the IExceptionFilter interface
Public class HttpGlobalExceptionFilter: IExceptionFilter {public void OnException (ExceptionContext context) {throw new NotImplementedException ();}}
The IExceptionFilter interface requires the implementation of the OnException method, which is triggered when an uncaught exception occurs in the system. The OnException method has an ExceptionContext exception context that contains specific exception information, HttpContext and mvc routing information. Once there is an uncaptured exception in the system, it is more common to use the logging tool to record the details of the exception to facilitate correction and debugging. The following is the implementation of logging.
/ Global exception filter / public class HttpGlobalExceptionFilter: IExceptionFilter {readonly ILoggerFactory _ loggerFactory; readonly IHostingEnvironment _ env; public HttpGlobalExceptionFilter (ILoggerFactory loggerFactory, IHostingEnvironment env) {_ loggerFactory = loggerFactory; _ env = env;} public void OnException (ExceptionContext context) {var logger = _ loggerFactory.CreateLogger (context.Exception.TargetSite.ReflectedType) Logger.LogError (new EventId (context.Exception.HResult), context.Exception, context.Exception.Message); var json = new ErrorResponse ("unknown error, please try again"); if (_ env.IsDevelopment ()) json.DeveloperMessage = context.Exception; context.Result = new ApplicationErrorResult (json); context.HttpContext.Response.StatusCode = (int) HttpStatusCode.InternalServerError; context.ExceptionHandled = true } public class ApplicationErrorResult: ObjectResult {public ApplicationErrorResult (object value): base (value) {StatusCode = (int) HttpStatusCode.InternalServerError;}} public class ErrorResponse {public ErrorResponse (string msg) {Message = msg;} public string Message {get; set;} public object DeveloperMessage {get; set;}
Register a global filter
Now that the filter has been written, you need to register in asp.net core MVC. Find the system root Startup.cs file, and modify the ConfigureServices method as follows
Services.AddMvc (options = > {options.Filters.Add ();})
test
Throw an exception in the request
Abnormal information was correctly captured in the log.
The browser returns a 500 error and a custom error message.
The above is all the content of the article "how to use ExceptionFilter filter in asp.net". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.