Willcode4cash icon

Exception Logging

Willcode4cash | PRO | 09/23/16 04:12:32 AM UTC | 0 ⭐ | 293 👁️ | Never ⏰ | []
C# |

620 B

|

None

|

0 👍

/

0 👎

// 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());
}

Comments