This repository was archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMSBuildTarget.cs
More file actions
58 lines (54 loc) · 2.11 KB
/
MSBuildTarget.cs
File metadata and controls
58 lines (54 loc) · 2.11 KB
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
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NLog;
using NLog.Targets;
namespace Phase.MsBuild
{
public class MSBuildTarget : TargetWithLayout
{
private readonly TaskLoggingHelper _log;
public MSBuildTarget(TaskLoggingHelper log)
{
_log = log;
}
private static readonly Regex ErrorPattern = new Regex(@"(?<File>[^(]+)\((?<Location>[^)]+)\): (?<Identifier>[^:]+): (?<Message>.*)");
protected override void Write(LogEventInfo logEvent)
{
var formattedMessage = logEvent.FormattedMessage;
var match = ErrorPattern.Match(formattedMessage);
if(match.Success)
{
var location = match.Groups["Location"].Value.Split(',').Select(int.Parse).ToArray();
if (match.Groups["Identifier"].Value.Contains("warning PH000"))
{
_log.LogWarning(null, null, null, match.Groups["File"].Value,
location[0], location[1],
location.Length > 2 ? location[2] : location[0],
location.Length > 3 ? location[3] : location[0],
match.Groups["Message"].Value
);
}
else if (match.Groups["Identifier"].Value.Contains("error PH000"))
{
_log.LogError(null, null, null, match.Groups["File"].Value,
location[0], location[1],
location.Length > 2 ? location[2] : location[0],
location.Length > 3 ? location[3] : location[0],
match.Groups["Message"].Value
);
}
else
{
_log.LogMessage(MessageImportance.High, formattedMessage);
}
}
else
{
var message = RenderLogEvent(Layout, logEvent);
_log.LogMessage(MessageImportance.High, message);
}
}
}
}