【问题标题】:How to print customized message in code to Teamcity build log in C#如何在 C# 中将代码中的自定义消息打印到 Teamcity 构建日志
【发布时间】:2011-05-15 12:07:01
【问题描述】:

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

喜欢:

//做某事

//打印消息

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

提前致谢

【问题讨论】:

    标签: c# build teamcity logging


    【解决方案1】:

    使用Service Messages,例如将此字符串写入标准输出##teamcity[message text='Hello from TeamCity'] 将导致Hello from TeamCity 出现在构建日志中。

    【讨论】:

      【解决方案2】:

      如果你想让一些消息出现在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));
          ....
                  }
      
        }
      

      在这里你可以看到结果:

      【讨论】:

        【解决方案3】:

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

        或者,您可以使用 this 中继来自 msbuild 脚本的消息。

        【讨论】:

        • 是的,我知道使用msbuild打印消息的方式,但是在我的场景中,我需要在多个线程中运行程序,我想打印任何线程的状态,所以我想做它在代码中。
        【解决方案4】:

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

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

        Log.LogMessage("Test!");
        

        【讨论】:

          【解决方案5】:

          我无法让 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);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-09-08
            • 2019-04-15
            • 2015-12-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多