Can I send error from console application that is

2019-07-21 02:44发布

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


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.

3条回答
来,给爷笑一个
2楼-- · 2019-07-21 03:21

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;
        }
    }
}
查看更多
疯言疯语
3楼-- · 2019-07-21 03:39

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

查看更多
看我几分像从前
4楼-- · 2019-07-21 03:40

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

查看更多
登录 后发表回答