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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Sep 20, 2022, 12:10 AM EDT
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/
Amit KumarRank #459 · 5.4K reputationPosted Sep 20, 2022, 12:25 AM EDT
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
Vishal JoshiRank #377 · 6.7K reputationPosted Sep 20, 2022, 2:05 AM EDT
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.
Vishal YelveRank #178 · 13.6K reputationPosted Sep 20, 2022, 9:48 AM EDT
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/
Sachin SinghRank #66 · 26.2K reputationPosted Sep 20, 2022, 1:19 PM EDT
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.