Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write error log or exception into file in C#.NET

Tags:

c#

I need help in writing error into separate text file and Email that file to me. Actually, I had created windows application but I didn't add any exception or writing error into separate file. Now, I want whenever my application is interrupted because of any error then error should be noted into separate text file and that file should be email to me.

I did research on error log file and all I found that create separate Class for writing Error Log into text file and call that file into try-catch block of all the methods. But my program/application has grown vast and I need to add intermediate of it. Also, I found about log4net but still it works in same manner.

Can anyone guide me how to write error log at application level or when application get any error message?

Thanks for help.

like image 262
Rushabh Shah Avatar asked Dec 02 '25 02:12

Rushabh Shah


1 Answers

As far as I can see, all you need is to handle AppDomain.UnhandledException Event. It will give you most common information about crash. You can use it to start the research of crash reasons.

And of course in the UnhandledException handler you can use whatever you want, log4net or custom logger.

like image 128
ie. Avatar answered Dec 03 '25 19:12

ie.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!