﻿// Copyright 2019 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using JetBrains.Annotations;
using Nuke.Common.OutputSinks;
using Nuke.Common.Utilities;

namespace Nuke.Common.BuildServers
{
    [UsedImplicitly]
    [ExcludeFromCodeCoverage]
    internal class TeamCityOutputSink : AnsiColorOutputSink
    {
        private readonly TeamCity _teamCity;

        public TeamCityOutputSink(TeamCity teamCity)
            : base(traceCode: "37", informationCode: "36", warningCode: "33", errorCode: "31", successCode: "32")
        {
            _teamCity = teamCity;
        }

        public override IDisposable WriteBlock(string text)
        {
            return DelegateDisposable.CreateBracket(
                () => _teamCity.OpenBlock(text),
                () => _teamCity.CloseBlock(text));
        }
    }
}

﻿// Copyright 2019 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using JetBrains.Annotations;
using Nuke.Common.OutputSinks;
using Nuke.Common.Utilities;
using Nuke.Common.Utilities.Collections;

namespace Nuke.Common.BuildServers
{
    [UsedImplicitly]
    [ExcludeFromCodeCoverage]
    internal class TeamCityOutputSink : AnsiColorOutputSink
    {
        private readonly TeamCity _teamCity;

        public TeamCityOutputSink(TeamCity teamCity)
            : base(traceCode: "37", informationCode: "36", warningCode: "33", errorCode: "31", successCode: "32")
        {
            _teamCity = teamCity;
        }

        public override IDisposable WriteBlock(string text)
        {
            var stopWatch = new Stopwatch();

            return DelegateDisposable.CreateBracket(
                () =>
                {
                    _teamCity.OpenBlock(text);
                    stopWatch.Start();
                },
                () =>
                {
                    _teamCity.CloseBlock(text);
                    _teamCity.AddStatisticValue(
                        $"NUKE_DURATION_{text.SplitCamelHumpsWithSeparator("_").ToUpper()}",
                        stopWatch.ElapsedMilliseconds.ToString());
                    stopWatch.Stop();
                });
        }
    }
}

Add duration as statistical value for blocks in TeamCity
Add duration as statistical value for blocks in TeamCity
