Created
June 4, 2012 13:03
-
-
Save Dynyx/2868238 to your computer and use it in GitHub Desktop.
Logging exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Logger Class | |
public static class Logger | |
{ | |
public static void Log(string message) | |
{ | |
File.AppendAllText("C:\\MyLogFile.txt", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":\t" + message + Environment.NewLine); | |
} | |
} | |
// Usage | |
static void Main(string[] args) | |
{ | |
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //it must be before Application.Run | |
... | |
Application.Run(); | |
} | |
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) | |
{ | |
Logger.Log(e.Exception.ToString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment