What is different between handle exceptions and logging errors in asp.net core
Loading
What is different between handle exceptions and logging errors in asp.net core
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sachin SinghPosted Sep 20, 2022, 5:19 PM
Handling Exceptions means handling the situation in cases where code breaks or page is not found etc. Handling here means, you know that there is some error but you won't display the errors to end users instead .you will display them a page with a generic message or popup. So, Exception handling is primarily for end users.
Where as logging errors is for developers, so that developers can rectify the errors by enalyzing the stack trace, page path and parameters which are logged to a file or db or anywhere else.
However we often use them in interchangibly as well, for example if you put a debugger in catch block and quick watch the errors then you are not actually handling the error you are just logging the error for that moment of time inside quick watch window to rectify the errors.
Vishal YelvePosted Sep 20, 2022, 1:48 PM
Hi Naresh,
Please refer below link which include both exception handling and error logging
http://codingsonata.com/exception-handling-and-logging-in-asp-net-core-web-api/
Vishal JoshiPosted Sep 20, 2022, 6:05 AM
Exception Handling
Exception handling means you can always surround your code with try-catch blocks. Also, exception handling is to know what is going wrong with your endpoints and backend. So you can detect and handle the exceptions in the correct way: either to return a graceful and meaningful response or otherwise find that bug and solve it once and for all.
Exception Handling in ASP.NET Core
Surround your controller function code with try-catch blocks, that does work, and nothing technically wrong with that, however surrounding each and every block of controller functions with try-catch blocks would eventually render your code spaghetti and hard to read.
Instead, you should always opt for the more organized, loosely coupled, generalized solution which is having a middleware to handle your exceptions and gracefully return the proper response message structure for your APIs’ client.
Error Logging
Error Logging mense whatever information in the exception at a specific time you can write it down to a file which can be reviewed later.
Imagine in a production environment we can't debug and check the exception but instead, we can write it to a file and at a specific period of time we can review the log file and review and fix the exceptions locally.
For more advanced and professional cases, you can use different file structures, such as elasticsearch, to write your files in a format that can be understood by comprehensive GUI tools like Kibana for reading and visualizing these logs, or even you can post these logs over cloud-based services such as Amazon Cloudwatch.
Amit KumarPosted Sep 20, 2022, 4:25 AM
Hi,
Logging and exception handling are like two peas in a pod. When a problem happens in your C# code, that typically means you have an exception that needs to be handled, and of course, any time an error or unanticipated event occurs, that occurrence should be logged appropriately.
For more details gothrough the below link.
http://codingsonata.com/exception-handling-and-logging-in-asp-net-core-web-api/
Regards,
Amit
Uday DodiyaPosted Sep 20, 2022, 4:10 AM
Refer This
https://www.c-sharpcorner.com/UploadFile/de41d6/exceptionerror-handling-in-Asp-Net-simplified/
https://martin.ankerl.com/2006/06/16/exception-handling-versus-logging/