views:

315

answers:

5

Because I set post-build event in ASP.net Web Application project to run the following command line.

start $(SolutionDir)[PathToMyApplicationInSameSolution] [some parameter]

So I need to send some error from console application to Visual Studio for showing Build Error(Like Visual Studio Build Error)

Thanks

A: 

If you don't use "start", the application's output will appear in Visual Studio's output window.

JesperE
A: 

So I change post-build event command to the following command.

$(SolutionDir)[PathToMyApplicationInSameSolution] [some parameter]

And then, I edit my main function to display error. I can't see anything in Error List in Visual Studio. I see only in Output only. Do you know how to display error like build error that is generated by Visual Studio?

[STAThread]
static void Main(string[] args)
{
    Console.Error.WriteLine("Test Error");
    Console.WriteLine("test error");
}

Thanks

Ps. Because I new to use command application, So I forgot that start like create new thread for console application.

Soul_Master
A: 

You need to return an error result from your application. Change the return type of Main to int. You can't use "start", because this'll throw away the result.

If you want an error message displayed in the Error List window, simply output a string in the correct format...

using System;

namespace FailBuild
{
    static class Program
    {
        static int Main(string[] args)
        {
            string fileName =
                @"D:\Source\Roger\IsServiceStarted\IsServiceStarted.cpp";
            int lineNumber = 4;
            string errorCode = "X1234";
            string errorMessage = "I don't like the colour";

            Console.WriteLine("{0}({1}): error {2}: {3}",
                fileName, lineNumber, errorCode, errorMessage);
            return 1;
        }
    }
}
Roger Lipscombe
A: 

Thanks. But I can't display error message in error list(All console.writeline will be displayed in output.

If it isn't possible to display error in error list, I will display error message by custom windows application. Thanks again!

Soul_Master
A: 

I found the solution to solve this problem. Visual studio will detect some pattern of output to be reported as error. So, I don't need to change default main method interface to return int(But you can use return value to identify error publicly).

More info : MSBuild / Visual Studio aware error messages and message formats

Soul_Master
On StackOverflow, please add comments to answers, rather than creating another answer.Also: You should return a non-zero result if you want the build to stop.
Roger Lipscombe