Question

We are implementing our own custom errors for our custom actions to help with logging in our product. I have properly created a handful of errors that are put into the error table when the project is built, and I have successfully been able to log errors properly. My question is, I want to be able to attach the exception message (if any) to the custom error to help us get a better idea of what went wrong.

I know that I could log the events separately, that is, I could have a try/catch block and if an exception is caught, I could handle our custom error and right after, I could handle the exception message, like so;

try
{
 //My Code
}
catch (Exception ex)
{
   MsiLogging.HandleError(session, InstallerErrors.CError_IsUserAdmin);
   MsiLogging.HandleError(session, ex.Message);
   return false;
}

Then, my logging class would handle it like this

public class MsiLogging
{
    //Placeholder for errors without a custom error message - default error message will appear
    public static void HandleError(Session session)
    {
        HandleError(session, 25000);
    }

    public static void HandleError(Session session, int error)
    {
        Record record = new Record(2);
        record[1] = error;
        session.Message(InstallMessage.Error, record);
    }
    public static void HandleError(Session session, string message)
    {
        Record record = new Record(2);
        record.FormatString = message;
        session.Message(InstallMessage.Error, record);
    }
}

That would generate two log entries, one with our custom error (which we pretty much use to identify the method in which the error occured and helps our customers IT admins who communicate with us give us better information when error occurs), and one with the exception message if one exists.

Is there a way to append our custom error message with the exception message directly and create only one log entry? This isn't super important, but it's just one of those things I'd really like to make happen. I have tried using the GetString() and SetString() methods for the Record object, but other than crashing my installer I've only been able to over write the custom error message instead of append to it. Any ideas?

Was it helpful?

Solution 2

I was able to get it to work by implementing a new MSI property, EXCEPTIONTEXT. I appended my custom errors to include this property, which by default would just read "No Exception". Then, I simply added another HandleError method:

public static void HandleError(Session session, int error, string message)
{
    Record record = new Record(2);
    session["EXCEPTIONTEXT"] = message;
    session.Message(InstallMessage.Error, record);
}

By setting the exception text property before logging the error, I was able to output the log. Kudos to PhilDW for pointing me in the right direction.

OTHER TIPS

I don't know exactly what you have in your Error table, but I suspect that a template is what you need. You'd have something like Error [1] Additional Info [2] and then you should be able to use the equivalent of MsiFormatRecord to get all the info into one record. This is what you'd do with C++, and I assume that the (DTF?) library exposes the equivalent to create one message out of multiple pieces of data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top