forked from coverlet-coverage/coverlet
/
CoverageTracker.cs
71 lines (66 loc) · 2.48 KB
/
CoverageTracker.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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using Coverlet.Core.Attributes;
using Coverlet.Core.Extensions;
namespace Coverlet.Core
{
public static class CoverageTracker
{
private static Dictionary<string, List<string>> _markers;
private static Dictionary<string, int> _markerFileCount;
[ExcludeFromCoverage]
static CoverageTracker()
{
_markers = new Dictionary<string, List<string>>();
_markerFileCount = new Dictionary<string, int>();
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
AppDomain.CurrentDomain.DomainUnload += new EventHandler(CurrentDomain_ProcessExit);
}
[ExcludeFromCoverage]
public static void MarkExecuted(string path, string marker)
{
lock (_markers)
{
_markers.TryAdd(path, new List<string>());
_markers[path].Add(marker);
_markerFileCount.TryAdd(path, 0);
if (_markers[path].Count >= 100000)
{
using (var fs = new FileStream($"{path}_compressed_{_markerFileCount[path]}", FileMode.OpenOrCreate))
using (var gz = new GZipStream(fs, CompressionMode.Compress))
using (var sw = new StreamWriter(gz))
{
foreach(var line in _markers[path])
{
sw.WriteLine(line);
}
}
_markers[path].Clear();
_markerFileCount[path] = _markerFileCount[path] + 1;
}
}
}
[ExcludeFromCoverage]
public static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
lock (_markers)
{
foreach (var kvp in _markers)
{
using (var fs = new FileStream($"{kvp.Key}_compressed_{_markerFileCount[kvp.Key]}", FileMode.OpenOrCreate))
using (var gz = new GZipStream(fs, CompressionMode.Compress))
using (var sw = new StreamWriter(gz))
{
foreach(var line in kvp.Value)
{
sw.WriteLine(line);
}
}
}
_markers.Clear();
}
}
}
}