aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Insights/InsightsFile.cs
blob: 2bcb2f76160fc859a87077b64b718646e5198e04 (plain)
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
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Bson;

namespace Tango.Insights
{
    public class InsightsFile
    {
        private static JsonSerializer _serializer;

        static InsightsFile()
        {
            _serializer = new BsonUtcSerializer();
        }

        public List<InsightsFrame> Frames { get; set; }

        public List<InsightsEvent> Events { get; set; }

        public List<InsightsStatus> Statuses { get; set; }

        public List<InsightsApplicationException> ApplicationExceptions { get; set; }

        public InsightsFile()
        {
            Frames = new List<InsightsFrame>();
            Events = new List<InsightsEvent>();
            Statuses = new List<InsightsStatus>();
            ApplicationExceptions = new List<InsightsApplicationException>();
        }

        public Stream ToStream()
        {
            return new MemoryStream(ToBytes());
        }

        public byte[] ToBytes()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BsonWriter writer = new BsonWriter(ms))
                {
                    _serializer.Serialize(writer, this);
                    ms.Position = 0;
                    return ms.ToArray();
                }
            }
        }

        public void ToFile(String filePath)
        {
            File.WriteAllBytes(filePath, ToBytes());
        }

        public static InsightsFile FromStream(Stream stream)
        {
            using (BsonReader reader = new BsonReader(stream))
            {
                return _serializer.Deserialize<InsightsFile>(reader);
            }
        }

        public static InsightsFile FromBytes(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                ms.Position = 0;
                return FromStream(ms);
            }
        }

        public static InsightsFile FromFile(String filePath)
        {
            using (FileStream fs = File.OpenRead(filePath))
            {
                return FromStream(fs);
            }
        }
    }
}