public static void writeException(int code, Exception innerException, string stackTrace, params object[] parameters) { citylifedb8_blContext db = new citylifedb8_blContext(); ErrorCode theErrorCode = db.ErrorCode.SingleOrDefault(aRecord => aRecord.Code == code); if (theErrorCode != null) { //This error code already exists in the DB. theErrorCode.LastOccurenceDate = DateTime.Now; theErrorCode.OccurenceCount++; } else { //The error code does not exist - create it theErrorCode = new ErrorCode() { Code = code, Message = ErrorCodeCollection.message(code), LastOccurenceDate = DateTime.Now, OccurenceCount = 1 }; db.ErrorCode.Add(theErrorCode); } db.SaveChanges(); //check if the same error message exists in the error message table string formattedMessage = AppException.formatMessage(code, parameters); ErrorMessage theErrorMessage = (from anErrorMessage in theErrorCode.ErrorMessages where anErrorMessage.FormattedMessage == formattedMessage && anErrorMessage.StackTrace == stackTrace select anErrorMessage).FirstOrDefault(); //actually we expect only a single record to be found if (theErrorMessage != null) { //Such an exact error message already exists - increment the counter theErrorMessage.LastOccurenceDate = DateTime.Now; theErrorMessage.OccurenceCount++; } else { //such exact error message does not exist - add it theErrorMessage = new ErrorMessage() { ErrorCodeCode = theErrorCode.Code, FormattedMessage = formattedMessage, LastOccurenceDate = DateTime.Now, OccurenceCount = 1, StackTrace = stackTrace, }; db.ErrorMessage.Add(theErrorMessage); } db.SaveChanges(); }
public AppException(int code, Exception innerException, params object[] parameters) : base(AppException.formatMessage(code, parameters), innerException) { writeException(code, innerException, this.StackTrace, parameters); }