using DxBlazorApplication2.Components.Pages;
using System.Text;

namespace DxBlazorApplication2.Services
{
    public class Logger
    {
        private Encoding _encoding = Encoding.GetEncoding(1252);   //ANSI
        string _logFile = @"D:\Work\Log\DxBlazorApplication2.log";

        public Logger() 
        {
            if (!Directory.Exists(Path.GetDirectoryName(_logFile)))
                Directory.CreateDirectory(Path.GetDirectoryName(_logFile));
        }

        public void Log(string text)
        {
            DateTime now = DateTime.Now;
            while (true)
            {
                try
                {
                    File.AppendAllText(_logFile, $"{now:dd.MM.yyyy HH:mm:ss} {text}\r\n", _encoding); // ANSI
                }
                catch (IOException)
                {
                    // Datei ist gesperrt, passiert schonmal, nochmal versuchen
                    Thread.Sleep(100);
                    continue;
                }
                break;
            }
        }
    }
}
