aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Logging/SessionFileLogger.cs
blob: f3c23ca250eddca1552a8707bc98a4fce66414f1 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Logging
{
    public class SessionFileLogger : ILogger
    {
        private bool _inInSession;

        public const string FILE_SESSION_EXTENSION = "_session";

        public static String DefaultLogsFolder { get; private set; }

        public bool Enabled { get; set; }

        public String Folder { get; private set; }

        public String LogFile { get; private set; }

        public String Tag { get; private set; }

        static SessionFileLogger()
        {
            DefaultLogsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Logs", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName), "session");
        }

        public SessionFileLogger(String folder, String tag)
        {
            Folder = folder;
            Tag = tag;
            Directory.CreateDirectory(Folder);
            Enabled = true;
        }

        public SessionFileLogger() : this(DefaultLogsFolder, Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName))
        {

        }

        public void CreateSession()
        {
            RemoveOldLogFile();
            LogFile = CreateLogFileName();
            _inInSession = true;
        }

        public void EndSession()
        {
            _inInSession = false;
        }

        private String CreateLogFileName()
        {
            return Path.Combine(Folder, string.Format("{1}-{0:dd-MM-yyyy_HH-mm-ss}{2}.log", DateTime.Now, Tag, FILE_SESSION_EXTENSION));
        }

        private void RemoveOldLogFile()
        {
            try
            {
                if (Directory.Exists(Folder))
                {
                    string[] fileEntries = Directory.GetFiles(Folder, "*.log");
                    foreach (string fileName in fileEntries)
                    {
                        try
                        {
                            File.Delete(fileName);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

        public void OnLog(LogItemBase output)
        {
            if (_inInSession)
            {
                try
                {
                    File.AppendAllText(LogFile, output.ToString() + Environment.NewLine);
                }
                catch
                {
                    Debug.WriteLine("Error Writing To Session Log File!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                }
            }
        }
    }
}