private static string formatMessage(int code, params object[] parameters) { if (ErrorCodeCollection.exists(code)) { string message = ErrorCodeCollection.message(code); string formattedMessage = string.Format(message, parameters); return(formattedMessage); } else { return(ErrorCodeCollection.message(code) + " " + parameters); } }
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(); }