Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Created June 4, 2012 13:03
Show Gist options
  • Save Dynyx/2868238 to your computer and use it in GitHub Desktop.
Save Dynyx/2868238 to your computer and use it in GitHub Desktop.
Logging exceptions
// 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