Tuesday 12 September 2017

How to write Exceptions to Event Viewer in C#

1 - First Create Static Method (LogError)
public static class Log
{
  public static void LogError(Exception ex)
  {
     EventLog.WriteEntry("Application", ex.Message + " Trace" + ex.StackTrace, EventLogEntryType.Error, 121, short.MaxValue);
  }
}
2 - Then Call this Static Method inside Catch Block.
try
{
               
}
catch (Exception ex)
{
  Log.LogError(ex);
}