aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Logging/FileLogger.cs
blob: 75c6e529e913be56375125e84a9509a34f5e75ed (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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Logging
{
    /// <summary>
    /// Represents an <see cref="ILogger"/> file logger.
    /// </summary>
    /// <seealso cref="Tango.Logging.ILogger" />
    public class FileLogger : ILogger
    {

        /// <summary>
        /// Gets or sets the log file.
        /// </summary>
        public String LogFile { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLogger"/> class.
        /// </summary>
        public FileLogger()
        {
            _isEnabled = true;
            String logsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "logs");
            Directory.CreateDirectory(logsFolder);
            LogFile = Path.Combine(logsFolder, string.Format("{1}-{0:yyyy-MM-dd_hh-mm-ss}.log", DateTime.Now, Path.GetFileNameWithoutExtension(System.AppDomain.CurrentDomain.FriendlyName)));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLogger"/> class.
        /// </summary>
        /// <param name="logFile">The log file.</param>
        public FileLogger(String logFile)
            : this()
        {
            Directory.CreateDirectory(Path.GetDirectoryName(logFile));
            LogFile = logFile;
        }

        /// <summary>
        /// Called when a new library trace is available.
        /// </summary>
        /// <param name="output">The output.</param>
        public void OnLog(LogItemBase output)
        {
            File.AppendAllText(LogFile, output.ToString() + Environment.NewLine);
        }

        private bool _isEnabled;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ILogger" /> is enabled.
        /// </summary>
        public bool Enabled
        {
            get
            {
                return _isEnabled;
            }
            set
            {
                _isEnabled = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ILogger" /> will be notified about logs without waiting for the logs queue.
        /// </summary>
        public bool Immediate { get; set; }
    }
}