Example #1
0
        public Logger(string filepath, string appname, Level min_level = Level.Info)
        {
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }

            string filename = Path.Combine(filepath, appname + ".log");

            // Backup existing log
            if (File.Exists(filename))
            {
                string backupfile = Path.Combine(filepath, appname + "-" + DateTime.Now.ToString("yyyyMMdd-hhmmss") + ".log");
                try { File.Move(filename, backupfile); }
                catch { /*Just eat this*/ }
            }

            _file = File.CreateText(filename);

            _min_level = min_level;

            Info("Starting up log facility ...");

            // Replace original listeners, replacing with writing into our own log
            Trace.Listeners.RemoveAt(0);
            _trace = new _Trace(this);
            Trace.Listeners.Add(_trace);

            Info("... log facility up and running");
            _file.Write("\n");

            SetLog(this);
        }
        public Logger(string filepath, string appname, Level min_level = Level.Info, bool as_singleton = true)
        {
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }

            _filename = Path.Combine(filepath, appname + ".log");

            // Backup existing log
            if (File.Exists(_filename))
            {
                // Move to zip, ...
                string backupfile = Path.Combine(filepath, appname + "-" + DateTime.Now.ToString("yyyyMMdd-hhmmss") + ".zip");
                Compressor.CompressToFile(backupfile, new Dictionary <string, byte[]> {
                    { Path.GetFileName(_filename), Helpers.GetFileContents(_filename) }
                });
                File.Delete(_filename);
                // ..., and reduce to at most 5 latest logs
                var logs = Directory.EnumerateFiles(filepath, appname + "*.zip")
                           .OrderByDescending(f => File.GetLastAccessTime(f))
                           .ToList()
                ;
                if (logs.Count > 5)
                {
                    logs.RemoveRange(0, 5);
                    logs.ForEach(f => File.Delete(f));
                }
            }

            _file = new StreamWriter(File.Open(_filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite));

            _min_level = min_level;

            Info("Starting up log facility ...");

            // Replace original listeners, replacing with writing into our own log
            if (as_singleton)
            {
                // Only avail with singleton, e.g. the main application log
                Trace.Listeners.RemoveAt(0);
                _trace = new _Trace(this);
                Trace.Listeners.Add(_trace);
            }

            Info("... log facility up and running");
            _file.Write("\r\n");

            if (as_singleton)
            {
                SetLog(this);
            }
        }