Advertisement
Willcode4cash

Exception Logging

Sep 22nd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.62 KB | None | 0 0
  1. // The Logger Class
  2. public static class Logger
  3. {
  4.     public static void Log(string message)
  5.     {
  6.         File.AppendAllText("C:\\MyLogFile.txt", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":\t" + message + Environment.NewLine);
  7.     }
  8. }
  9.  
  10. // Usage
  11. static void Main(string[] args)
  12. {
  13.     Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //it must be before Application.Run
  14.     ...
  15.     Application.Run();
  16. }
  17.  
  18. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  19. {
  20.     Logger.Log(e.Exception.ToString());
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement