-
Notifications
You must be signed in to change notification settings - Fork 61
/
MessageLogger.cs
96 lines (82 loc) · 2.68 KB
/
MessageLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// ReSharper disable ClassNeverInstantiated.Global
namespace TeamCity.Docker
{
using System;
using System.Text;
using global::Docker.DotNet.Models;
using IoC;
using Newtonsoft.Json;
internal class MessageLogger : IMessageLogger
{
private readonly ILogger _logger;
public MessageLogger([NotNull] ILogger logger) => _logger = logger ?? throw new ArgumentNullException(nameof(logger));
public Result Log(JSONMessage message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
if (message.Error != null && !string.IsNullOrWhiteSpace(message.Error.Message))
{
_logger.Log(message.Error.Message.Trim(), Result.Error);
return Result.Error;
}
var sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(message.ID))
{
sb.Append(message.ID);
sb.Append(": ");
}
if (!string.IsNullOrWhiteSpace(message.Status))
{
sb.Append(message.Status.Trim());
}
if (!string.IsNullOrWhiteSpace(message.Stream))
{
if (sb.Length > 0)
{
sb.Append(' ');
}
sb.Append(message.Stream.Trim());
}
if (!string.IsNullOrWhiteSpace(message.ProgressMessage))
{
if (sb.Length > 0)
{
sb.Append(' ');
}
sb.Append(message.ProgressMessage);
}
else
{
if (message.Progress != null && message.Progress.Total > 0)
{
if (sb.Length > 0)
{
sb.Append(' ');
}
var percent = message.Progress.Current * 100.0 / message.Progress.Total;
if (percent <= 100)
{
sb.Append($"{percent: 000}%");
}
else
{
sb.Append(message.Progress.Current);
}
}
}
_logger.Log(sb.ToString());
return Result.Success;
}
public Result Log(string jsonMessage)
{
if (jsonMessage == null)
{
throw new ArgumentNullException(nameof(jsonMessage));
}
var message = JsonConvert.DeserializeObject<JSONMessage>(jsonMessage);
return Log(message);
}
}
}