using System; using System.Data.SqlClient; using System.Configuration; using System.Diagnostics; namespace framework_library { public class exception { /// /// Method to log an exception that occured in the application /// /// /// /// /// public static void HandleException(string className, string functionName, Exception exception, dynamic user, bool sendMail = true) { int userId = 0; SqlCommand command = new SqlCommand(); string Query = ""; try { //CVH 2019-01-23 Do this in a try catch, don't want an exception because user not the type we expect try { if (user == null) userId = 0; else if (user.GetType() != typeof(int)) //if (user != null && user.GetType() != typeof(int)) userId = int.Parse(user.recId.ToString()); else userId = user; } catch (Exception) { } string stackTrace = exception.StackTrace == null ? "" : exception.StackTrace.Replace("'", "''"); //instanitiate the SQL transaction command = dataTier.CommandMethods.BeginSQLCommand(); command.Parameters.AddWithValue("@className", className); command.Parameters.AddWithValue("@functionName", "'" + functionName + "'"); command.Parameters.AddWithValue("@exception", "'Message: " + exception.Message.Replace("'", "''") + "StackTrace:" + stackTrace + "'"); command.Parameters.AddWithValue("@userId", userId); //command.Parameters.AddWithValue("@dateLogged", "getdate()"); Query += "INSERT INTO pal_Exceptions ([ClassName], [FunctionName], [Exception], [UserID], [DateLogged]) "; Query += "VALUES (@className,@functionName,@exception,@userId, getdate()) "; //set commandText command.CommandText = Query; //Log the Bug to the tblExceptions Table dataTier.SavingMethods.AddRecord(ref command); //Send Exception to Email if (sendMail) EmailException(className, functionName, exception.StackTrace == null ? "" : exception.StackTrace, exception.Message, userId); } catch (Exception ex) { } finally { //commit transaction dataTier.CommandMethods.CompleteSQLCommand(ref command); } } /// /// Method to email the Exception to support /// /// /// /// /// public static void EmailException(string className, string functionName, string exception, string exceptionMessage, int userID) { string body = String.Empty; string email = ConfigurationManager.AppSettings["support"]; string from = ConfigurationManager.AppSettings["from"]; string subject = ConfigurationManager.AppSettings["subject"]; body += ""; body += ""; body += ""; body += ""; body += "An error occured on the " + ConfigurationManager.AppSettings["website"] + " website.

"; body += "The Exception occured in the following class: " + className + "

"; body += "At the following function: " + functionName + "

"; body += "The user that experienced the error: " + userID + "

"; body += "The exception message: " + exceptionMessage + "

"; body += "The exception strack trace: " + exception + "

"; body += "Thank You,

"; body += ConfigurationManager.AppSettings["website"] + "
"; body += ""; if (!Debugger.IsAttached) { oEmail mail = new oEmail(); mail.toAddress = email; mail.Subject = subject.Replace("{content}", "Exception"); mail.fromAddress = from; mail.Body = body; //send email communication.SendAnEmail(mail); } } } }