Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to log error inside exception constructor?

I'm developing a web application in C#, and I have a special exception type, that I need to have a full log from its happenings, and of course I'm going to handle that to show a special message to the user or something.

The question is, is it a good practice to write logging codes inside exception's constructor? I'm asking this because I have not seen something similar to this before.

Thanks in advance

like image 885
Ahmad Avatar asked Mar 12 '23 12:03

Ahmad


1 Answers

A short and good answer should be: no, you shouldn't go this way becuase you want to develop your code with a good separation of concerns: exception handling isn't logging, thus, logging can't be part of constructing an exception.

If that exception is not handled by your code, you can use a last-chance exception handler like AppDomain.UnhandledException event and log it there.

like image 79
Matías Fidemraizer Avatar answered Apr 09 '23 02:04

Matías Fidemraizer