开发者

How to print customized message in code to Teamcity build log in C#

开发者 https://www.devze.com 2023-01-27 09:00 出处:网络
RecentlyI need to print some special message in C# code to Teamcity build log when executing, Does anyone know how to do that?

Recently I need to print some special message in C# code to Teamcity build log when executing, Does anyone know how to do that?

like:

//dosomething

//print messages

hope in Teamcity build log can see these messages.

thanks in advan开发者_Python百科ce


If you want to some message to appear in Team City build log from lets say a test then just simply use

Console.WriteLine("")

Here's an example to unit test logging to TeamCity Log

 [SetUp]
 public void Init()
 {
        try
            {
                Console.WriteLine(string.Format("with_fresh_db Init Before RefreshController.StartRefresh: Time: {0:yyyy-MM-dd HH:mm:ssss}", DateTime.Now));
    ....
            }

  }

And here you can see the result:

How to print customized message in code to Teamcity build log in C#


Use Service Messages, for example writing this string to the stdout ##teamcity[message text='Hello from TeamCity'] will cause Hello from TeamCity to appear in the build log.


Are you using msbuild to compile a C# application, or executing a C# application via your build script?.

Alternatively you can relay messages from your msbuild scripts using this.


Inherit from Task and use Microsoft.Build.Utilities:

Log.LogMessage(MessageImportance.High, "Bla-bla-bla");

or

Log.LogMessage("Test!");


I had trouble getting Console.WriteLine to write to the build log from an NUnit test.

I assume NUnit or TeamCity redirects the Console stream so any calls from the system under test don't get logged and mess up the build log.

I redirected temporarily back to std out from my test method and it appeared in the log.

// redirect to stdio
var tw = Console.Out;
var sw = new StreamWriter(Console.OpenStandardOutput());
sw.AutoFlush = true;
Console.SetOut(sw);

// write 
Console.WriteLine($"Console writeline from test");

// put it back
Console.SetOut(tw);
0

精彩评论

暂无评论...
验证码 换一张
取 消