如何在 C# 中将代码中的自定义消息打印到 Teamcity 构建日志

发布于 2024-10-03 23:14:58 字数 158 浏览 0 评论 0原文

最近我需要在执行时将 C# 代码中的一些特殊消息打印到 Teamcity 构建日志中,有人知道该怎么做吗?

比如:

//dosomething

//打印消息

希望在Teamcity构建日志中可以看到这些消息。

提前致谢

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 advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遇到 2024-10-10 23:14:58

如果您希望在测试中的 Team City 构建日志中显示一些消息,那么只需使用

Console.WriteLine("")

以下示例即可将单元测试日志记录到 TeamCity 日志中

 [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));
    ....
            }

  }

,您可以在此处看到结果:

在此处输入图像描述

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:

enter image description here

Saygoodbye 2024-10-10 23:14:58

使用服务消息,例如编写将此字符串发送到 stdout ##teamcity[message text='Hello from TeamCity'] 将导致 Hello from TeamCity 出现在构建日志中。

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.

孤寂小茶 2024-10-10 23:14:58

您是使用 msbuild 编译 C# 应用程序,还是通过构建脚本执行 C# 应用程序?

或者,您可以使用从 msbuild 脚本中继消息。

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.

儭儭莪哋寶赑 2024-10-10 23:14:58

从 Task 继承并使用 Microsoft.Build.Utilities:

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

Log.LogMessage("Test!");

Inherit from Task and use Microsoft.Build.Utilities:

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

or

Log.LogMessage("Test!");
归属感 2024-10-10 23:14:58

我无法让 Console.WriteLine 从 NUnit 测试写入构建日志。

我假设 NUnit 或 TeamCity 重定向控制台流,因此来自被测系统的任何调用都不会被记录并弄乱构建日志。

我从测试方法暂时重定向回标准输出,它出现在日志中。

// 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文