How to identify the criticality of an exception to set EventLogEntryType value

Pat Hanks 141 Reputation points
2022-12-19T14:30:30.817+00:00

Greeting all. I am writing an Winapp in C# and attempting to identify the criticality of an exception to set the EventLogEntryType value when writing the error/info to the log file or event log. I have read many of the documents on the MS site but I am unable to understand which property or properties I use to can arrive at this. I walked through the exception object in the debugger and found many of the important information to include in the log just not the criticality state.

I would appreciate any help to direct me to the data on this.

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,326 Reputation points
    2022-12-19T15:38:16.85+00:00

    An exception is an "exception to the normal flow of your application". By definition all exceptions are errors so I would flag any exception as an error. There is no other property that indicates how severe an exception is because an exception, by definition, is an unexpected error.

    What you really have to decide is which exceptions are recoverable and which aren't. That is really what determines how to respond and unfortunately it completely depends upon your app's needs. For example an error opening a file is recoverable if you asked the user for a file and they gave you a bad one. There is no reason to log that one, just tell the user they messed up. Similarly if you're trying to open a file containing the user's custom settings but you cannot find the file then that is also recoverable (just use the app defaults). But if your application relies on a data file that ships as part of your app and you cannot find it then that may be a fatal application error that you need to log. The user is unlikely to be able to recover from that unless they do a repair.

    The way you should "handle" exceptions is using a try-catch. Wrap the code you can handle errors from (such as opening a user-specified file) and specify the exception types you can handle (e.g. FileNotFoundException). Then respond to each exception type appropriately. If you cannot handle an exception then do nothing and let the system handle it. The system will log the exception to the event log and crash the app with WER. This is preferable. If you need custom exception logging then you'll need to get notifications when unhandled exceptions occur (see SetUnhandledException) but note that your app is terminating at that point.

       void OpenUserProvidedFile ( string filename )  
       {  
          try  
          {  
              OpenFile(filename);  
          } catch (FileNotFoundException)  
          {  
              MessageBox.Show("The file could not be found.");  
          } catch (IOException)  
          {  
              MessageBox.Show("An error occurred trying to open the file");  
          };  //Let other exceptions filter up through the app  
       }  
    

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2022-12-19T17:35:57.29+00:00

    this is up to your application to decide. that is, how impactful is the error to your running code. it your code catches and handles a error, it may just be a warning, if you program will exit, its probably an error.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.