view ServerMonitor/Objects/Logger.cs @ 14:2db36ab759de

Add comments.
author Brad Greco <brad@bgreco.net>
date Mon, 22 Apr 2019 21:10:42 -0400
parents 7127d5b5ac75
children 68d7834dc28e
line wrap: on
line source

using ServerMonitorApp.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ServerMonitorApp
{
    public class Logger
    {
        private const int logVersion = 1;
        private readonly string logFile;
        
        public Logger(string file)
        {
            logFile = file;
        }

        public async void Log(string logString)
        {
            if (!File.Exists(logFile))
                logString = "Server Monitor log version " + logVersion + Environment.NewLine + logString;
            using (FileStream stream = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, true))
            using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
            {
                await writer.WriteLineAsync(logString);
            }
        }

        public void Log(CheckResult result)
        {
            Log(result.ToLogString());
        }

        public IList<CheckResult> Read(Server server)
        {
            Dictionary<int, Check> checks = server.Checks.ToDictionary(c => c.Id);
            List<CheckResult> results = new List<CheckResult>();
            using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                while (true)
                {
                    try
                    {
                        string line = reader.ReadLine();
                        if (line == null)
                            break;
                        if (int.TryParse(line.Substring(0, 5), out int id) && checks.TryGetValue(id, out Check check))
                        {
                            results.Add(CheckResult.FromLogString(check, line));
                        }
                    }
                    catch (Exception) { }
                }
            }
            results.Reverse();
            return results;
        }

        public async void TrimLog()
        {
            DateTime maxAge = DateTime.Now.AddDays(-1 * Settings.Default.KeepLogDays);
            string tempFile = logFile + ".tmp";
            try
            {
                using (FileStream stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                using (FileStream outStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None))
                using (StreamWriter writer = new StreamWriter(outStream, Encoding.UTF8))
                {
                    while (true)
                    {
                        try
                        {
                            string line = reader.ReadLine();
                            if (line == null)
                                break;
                            if (DateTime.TryParse(line.Substring(6, 10), out DateTime date) && date > maxAge)
                            {
                                await writer.WriteLineAsync(line);
                            }
                        }
                        catch (Exception) { }
                    }
                }
                File.Delete(logFile);
                File.Move(tempFile, logFile);
            }
            catch (FileNotFoundException)
            {
                // Do nothing if the log file does not exist yet.
            }
        }
    }
}