0

I have ASP MVC web application and trying to setup error logging where i am trying to display UI friendly message for web and log more detail message in database in need. My application might not need to log error message in DB always but i wanted to setup Custom Exception in a way so that it handles both scenario.

  1. Show short UI friendly message and log detail error message in DB
  2. Show short UI friendly message without logging in DB.

Here is what i have: CustomException: If detailMessage is passed, get new guid, log message in DB and concatenate guid value in shortMessage.

public class CustomException : Exception
{
    public CustomException(string message) : base(message)
    {
    }

    public CustomException(string shortMessage, string detailMessage = "") : base(shortMessage)
    {
        if (!string.IsNullOrWhiteSpace(detailMessage))
        {          
            string loggingId = Guid.NewGuid().ToString();
            //code to log in DB with detail message
            shortMessage = $"Reference# : {loggingId}. {shortMessage}";
        }
    }        
}

ThrowIf Helper Method to check value so that the code is cleaner and i don't need to do if block check as old school in web layer.

public static class ThrowIf
{
    public static void IsZeroOrNegative(int argument, string shortMessage, string detailMessage = "")
    {
        if (argument <= 0)
        {
            throw new CustomException(shortMessage, detailMessage);         
        }
    }

    public static void IsNull(object argument, string shortMessage, string detailMessage = "")
    {
        if (argument == null)
        {
            throw CustomException.LogMessage(shortMessage, detailMessage);
        }
    }
}

Web layer code: (Please don't ask why i'm not throwing ArgumentException and using my own CustomException in this scenario)

public ActionResult GetEmployee(int employeeId)
{
    var responseViewModel = new EmployeeModel();
    try
    {
        ThrowIf.IsZeroOrNegative(employeeId, shortMessage: "This is short message for UI.", detailMessage: "This is detail message for DB");
    }
    catch(CustomException ex)
    {
        ViewBag.Error = ex.Message();
    }
    return View(responseViewModel);
}

Issue: As you can see in my CustomException code, if i pass value for 'detailMessage', it updates shortMessage with 'Reference..' text. Now in Web code, whenever the error happens, the text for ex.Message() doesn't contain 'Reference...' text and only shows 'This is short message for UI' instead of 'Reference# : [guid value]. This is short message for UI'.

Does anyone know why ex.Message is not displaying updated text when the error gets caught in Web layer?

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
sanjeev
  • 765
  • 1
  • 7
  • 15
  • you are assiging a value to shortMessage variable in the constructor. and expecting Message property of Exception class to have the value of shortmessage – Matt.G Feb 28 '19 at 21:05
  • 1
    You could assign the value of shortmessage to a private variable in the class and override the Message Property to return the value of the private variable. – Matt.G Feb 28 '19 at 21:12
  • @Matt.G lol..such a silly mistake. thanks i got it working now using private variable. :) – sanjeev Feb 28 '19 at 21:22
  • @Matt.G i think using private variable has one down side. since it needs to be passed in base it needs to be static. And when the first time error happens, it fails to return actual error message, the other calls after that works just fine. private static string _errorMessage = string.Empty; public CustomException(string shortMessage, string detailMessage = "") : base(_errorMessage) {_errorMessage = ....} i think instead of assigning value to private variable, assigning value to new string property and accessing that property worked better. – sanjeev Mar 01 '19 at 13:24
  • since you are overriding the Message property, you don't have to pass the value to base class constructor. – Matt.G Mar 01 '19 at 13:41

0 Answers0