简体   繁体   中英

Verbose build event error handling in Visual Studio

I created an executable that performs operations on an assembly after it is built.

I want this executable to run every time the assembly is built, so that it is immediately ready to use. In order to do this, I open the Properties of the assembly in Visual Studio (right-click on project, select Properties ), go to Build Events tab, and enter the following where it says Post-build event command line :

C:\MyExePath\MyAssemblyManager.exe C:\MyProjectPath\obj\Release\MyProject.dll

And every thing works as it should. Every time the project is built, the executable runs and the assembly is ready to go.

But here is my issue: Say I have some error handling in my executable:

static void Main(string[] args)
{
    try
    {
        string assemblyPath = args[0];
        if (!System.IO.File.Exists(assemblyPath))
        {
            throw new System.IO.FileNotFoundException(String.Format("Path {0} is invalid.", assemblyPath);
        }
        // ...
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.Message);
        Environment.Exit(9000);
    }
}

If this error occurs in the executable, the following is displayed in the Error List tab in Visual Studio:

The command C:\MyExePath\MyAssemblyManager.exe C:\MyAssemblyPath\MyAssembly.dll exited with error code 9000.

Is there a way to pass an error message via string from the executable that can be displayed in the Visual Studio Error List instead of the generic error code message?

I think that it's hard to modify VS internal error message.
Try to output error messages to an external file can help to debug.
Change post-build event to:

C:\MyExePath\MyAssemblyManager.exe C:\MyProjectPath\obj\Release\MyProject.dll >D:\build.txt

Note that >D:\\build.txt will output error messages to file build.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM