Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the error handling mechanism of springmvc?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the springmvc error handling mechanism?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the springmvc error handling mechanism" article.

1. First of all, in the protected void Application_Start () of the golable file

Register an error handling mechanism.

Comes with a filter in MVC

FilterConfig.RegisterGlobalFilters (GlobalFilters.Filters)

We see this filter in here.

2. It is actually the FilterConfig.cs file under the app_Start folder.

3. Open the FilterConfig.cs file

Just wrote a registration event. We can see that this is an error handling mechanism.

(of course, what you see is the class HandleErrorAttribute)

4. So you may find it strange, let's take a look at the definition of MyExceptionAttribut

Inherited HandleErrorAttribute

Here, I'll paste the code of this class.

Public class MyExceptionAttribute: HandleErrorAttribute {/ / private static object obj = new object (); public static ConcurrentQueue ExceptionQueue = new ConcurrentQueue (); / / define queue / catch exceptions in this method. / public override void OnException (ExceptionContext filterContext) {base.OnException (filterContext); Exception ex = filterContext.Exception;// captures exception information. / / write exception information to the queue. ExceptionQueue.Enqueue (ex); / / Jump to error page. FilterContext.HttpContext.Response.Redirect ("/ Error.html");}}

Mainly to define a static queue ConcurrentQueue

(of course you can also use Queue. But Microsoft says the ConcurrentQueue is more secure than Queue. It seems to be thread-safe, pile upon pile of theories, to put it bluntly, it is safer to use ConcurrentQueue)

So all the mistakes are in this queue. (that is, memory)

That's not gonna work. If the memory is powered off, it will be gone.

So if we want to save the data to the hard drive.

5. Now we are going to be in

Protected void Application_Start () of the golable file

To register a consumer thread in (explained later, continue if you don't understand) is to add this code to protected void Application_Start () and put it first.

The content is that the thread pool starts a thread to fetch items from the ExceptionQueue queue of the MyExceptionAttribute just defined.

Add the error message to the end of the file most. If the queue is empty, the thread stays for 3 seconds.

String filePath = Server.MapPath ("/ Log/"); ThreadPool.QueueUserWorkItem ((a) = > {while (true) / / Note: the thread cannot end. The data written later in the queue cannot be processed. {/ / you can add an if (MyExceptionAttribute.ExceptionQueue.Count () > 0) / / {send an email to the administrator} if (MyExceptionAttribute.ExceptionQueue.Count () > 0) {/ / Exception ex= MyExceptionAttribute.ExceptionQueue.Dequeue (); / / fetch data from the queue. Exception ex = null; bool isResult = MyExceptionAttribute.ExceptionQueue.TryDequeue (out ex); if (ex! = null & & isResult) {string fullPath = filePath + DateTime.Now.ToString ("yyyy-MM-dd") + ".txt"; File.AppendAllText (fullPath, ex.ToString ()) / / ILog logger = LogManager.GetLogger ("errorMsg"); / / logger.Error (ex.ToString ());} else {Thread.Sleep (3000) }} else {Thread.Sleep (3000); / / avoid idling of CPU. }, filePath)

6. Summary.

This is a producer-consumer model.

The producer is the source of the error. The consumer is the way to register and save the log.

There is a warehouse in the middle that is the static error queue.

You can see that errors generated by the system are temporarily stored in memory. Then a new thread reads and writes the static error queue.

Normally, you need to add a warning to the mailbox when the number of error queues is greater than 1000. That feels a little complicated. After all, it's just about error handling.

7. Log4net an open source framework I mentioned earlier records errors very well.

Here is a connection log4net configuration method that you can put together here. Then there will be

Put the code added in protected void Application_Start ()

Change it to. Note that it is changed to:

Log4net.Config.XmlConfigurator.Configure (); / / start a thread and look at the exception queue string filePath = Server.MapPath ("/ Log/"); ThreadPool.QueueUserWorkItem ((a) = > {while (true) / / Note: the thread cannot end. The data written later in the queue cannot be processed. {if (MyExceptionAttribute.ExceptionQueue.Count () > 0) {/ / Exception ex= MyExceptionAttribute.ExceptionQueue.Dequeue (); / / fetch data from the queue. Exception ex = null; bool isResult = MyExceptionAttribute.ExceptionQueue.TryDequeue (out ex); if (ex! = null & & isResult) {string fullPath = filePath + DateTime.Now.ToString ("yyyy-MM-dd") + ".txt" / / File.AppendAllText (fullPath, ex.ToString ()); ILog logger = LogManager.GetLogger ("errorMsg"); logger.Error (ex.ToString ()) } else {Thread.Sleep (3000);}} else {Thread.Sleep (3000) / / avoid causing CPU idling. }, filePath)

In this way, the error log will be logged to

Under the app_data folder. The premise is that you have unhandled mistakes. Hehe)

If you see the picture below, it's a success.

The above is the content of this article on "what is the error handling mechanism of springmvc". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report