aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Logging/SessionFileLogger.cs
blob: 4e378bfbfb8bd8344400a62468c6d4e87e9c4777 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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;
        private int _writeCount;
        private const string FILE_SESSION_EXTENSION = "_session";
        private const int FILE_SIZE_CHECK_COUNT = 100;

        #region Static Properties

        /// <summary>
        /// Gets the default logs folder.
        /// </summary>
        public static String DefaultLogsFolder { get; private set; }

        #endregion

        #region Properties

        public bool Enabled { get; set; }

        /// <summary>
        /// Folder is used for save file session logs
        /// </summary>
        public String Folder { get; private set; }

        /// <summary>
        /// Full path of log file
        /// </summary>
        public String LogFile { get; private set; }

        /// <summary>
        /// Gets the tag name which will be appended to the file.
        /// </summary>
        public String Tag { get; private set; }

        /// <summary>
        /// Gets or sets the maximum file size limit.
        /// </summary>
        public long MaxFileSizeLimit { get; set; }

        #endregion

        #region Constructors
        /// <summary>
        /// Initializes the static members of the class.
        /// </summary>
        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;
            _writeCount = 0;
            MaxFileSizeLimit = 1000000 * 10; //10 MB
        }

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

        #region Public Methods

        /// <summary>
        /// Creates the session. Only one file should be for session log, so removed old file and create a new file.
        /// </summary>
        public void CreateSession()
        {
            _writeCount = 0;
            RemoveOldLogFile();
            LogFile = CreateLogFileName();
            _inInSession = true;
        }

        /// <summary>
        /// Ends the session. Set flag _inInSession to false.
        /// </summary>
        public void EndSession()
        {
            _inInSession = false;
        }

        /// <summary>
        /// Called when write to session log file.
        /// </summary>
        /// <param name="output">The output.</param>
        public void OnLog(LogItemBase output)
        {
            if (_inInSession)
            {
                try
                {
                    if (!File.Exists(LogFile) || (++_writeCount > FILE_SIZE_CHECK_COUNT && new FileInfo(LogFile).Length > MaxFileSizeLimit))
                    {
                        CreateSession();
                    }

                    File.AppendAllText(LogFile, output.ToString() + Environment.NewLine);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error Writing To Session Log File!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n{ex.ToString()}");
                }
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Creates the name of the log file.
        /// </summary>
        /// <returns></returns>
        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));
        }

        /// <summary>
        /// Delete the old log file if is existed.
        /// </summary>
        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);
            }
        }

        #endregion
    }
}