Example #1
0
        public void TestOpenFileAppend()
        {
            fs.AppendAllText("/toto.txt", "content");
            Assert.True(fs.FileExists("/toto.txt"));
            Assert.Equal("content", fs.ReadAllText("/toto.txt"));

            fs.AppendAllText("/toto.txt", "content");
            Assert.True(fs.FileExists("/toto.txt"));
            Assert.Equal("contentcontent", fs.ReadAllText("/toto.txt"));
        }
Example #2
0
        private void Error(Exception e, int indentation)
        {
            if (e == null)
            {
                return;
            }

            var delimiter = indentation == 0 ? DateTime.Now.ToString() + " - " : string.Empty;

            var log = string.Format("{0}{1}{2} - {3}: {5}{4}{5}", new string('\t', indentation), delimiter, e.GetType().FullName, e.Message, PrependTabsToLinebreaks(e.StackTrace, indentation), Environment.NewLine);

            fileSystem.AppendAllText(path, log);

            Error(e.InnerException, indentation + 1);

            if (indentation == 0)
            {
                fileSystem.AppendAllText(path, Environment.NewLine);
            }
        }
        protected override void Flush(T[] logEvents)
        {
            if (logEvents.Length == 0)
            {
                return;
            }

            var path = _pathFactory();

            var directoryPath = Path.GetDirectoryName(path);

            if (directoryPath == null)
            {
                return;
            }

            _fileSystem.CreateDirectory(directoryPath);

            var builder = new StringBuilder();

            foreach (var logEvent in logEvents)
            {
                builder.AppendLine(_formatter.Format(logEvent));
            }

            while (true)
            {
                try
                {
                    _fileSystem.AppendAllText(path, builder.ToString());
                    return;
                }
                catch (Exception ex)
                {
                    Logger.Exception(ex);
                }

                Thread.Sleep(1000);
            }
        }
Example #4
0
 private void AppendToFile(string buffer)
 {
     _fileSystem.AppendAllText(TargetFilePath, buffer);
 }