4 Replies - 635 Views - Last Post: 14 October 2017 - 03:20 PM Rate Topic: -----

#1 ScottinTexas  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 261
  • Joined: 13-March 12

Logging Exceptions

Posted 29 August 2017 - 07:10 AM

I am trying to find something to help me properly log and report (via email) any exception thrown, handled or unhandled. C#, WPF, MVVM, .Net 4.5, Win 10.

What I want to do is send a message with the exception info to an email address. My program checks the emails, looks for the correct title and reads and records the message. So if a user calls me and says "The program broke" I can see what she is talking about. Unhandled exceptions can be caught by DispatcherUnhandledException which can get an instance of my custom class. But for handled exceptions, I still want the message. So do I take the exception, modify the info as required and throw an exception to be caught? But then the program will quit for a simple "File not found" or some such error.

Anyway, if any one knows of a good article about the subject I would appreciate the link.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Logging Exceptions

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13491
  • View blog
  • Posts: 53,888
  • Joined: 12-June 08

Re: Logging Exceptions

Posted 29 August 2017 - 08:01 AM

I typically got simple. A 'Exception Tracker' class with a public static methods that takes in an exception object, a string for comments, and a enum type. The methods can be various things like 'write to file' or 'send to email'.

Typically just drop this into new projects and use it in the 'catch' as needed.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5895
  • View blog
  • Posts: 20,126
  • Joined: 05-May 12

Re: Logging Exceptions

Posted 29 August 2017 - 08:24 AM

Log4Net supports several different kinds of appenders, including one that sends emails. As with all other logging frameworks, it is up to you to insert the logging code to call the framework. You can't be lazy and just have a single generic catch-all-unhandled-exceptions and call the framework at that point. You will have to insert the logging calls wherever to catch exceptions and handle them. For the exceptions that you handle you can likely log them as warnings or informational, vs. true errors which you would log as errors or fatal errors.
Was This Post Helpful? 0
  • +
  • -

#4 ScottinTexas  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 261
  • Joined: 13-March 12

Re: Logging Exceptions

Posted 30 August 2017 - 04:11 AM

Thanks to both for your responses. I set it up with a class to do the work and I call on it with each catch. Then when the big one hits I call on it again. I just didn't know if that was the way it is done or whether I was missing some .Net class, or whatever. Every time I think I did something that's right, some blog appears that shows me a MUCH simpler way to do the same thing in one line of code vs my many lines.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#5 BenScharbach2017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-October 17

Re: Logging Exceptions

Posted 14 October 2017 - 03:20 PM

In try-Catch blocks, you could use calls like this;

Debug.WriteLine() to log to VS console.
Console.WriteLine() to log to the console.
EventLog class in C# to log to Windows Event Logger.

Example;

if (!EventLog.SourceExists(LogSourceName))
    EventLog.CreateEventSource(LogSourceName, LogName);

var evt = new EventLog { Source = "ICLWebService" };
evt.WriteEntry(errorMessage, EventLogEntryType.Error);



Ben Scharbach
Youtube:Ben Scharbach
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1