Ejemplo n.º 1
0
        public void RelativePath(string testedRelativePath, string relativePathForCheck)
        {
            string concreteDirectory   = "C:/temp";
            string destinationFilePath = Path.Combine(concreteDirectory, relativePathForCheck);

            File.Delete(destinationFilePath);

            string currentDirectory = Environment.CurrentDirectory;

            Environment.CurrentDirectory = concreteDirectory;

            try
            {
                var logger = new JsonLoggerBuilder(testedRelativePath)
                             .Build();
                logger.Error("tubaluga");
            }
            catch (Exception) { }
            finally
            {
                Environment.CurrentDirectory = currentDirectory;
            }

            Assert.True(File.Exists(destinationFilePath));

            string contentOfFile = File.ReadAllText(destinationFilePath).Trim();

            AssertLogEntry("tubaluga", null, LogLevel.Critical, DataType.Text, contentOfFile);

            File.Delete(destinationFilePath);
        }
Ejemplo n.º 2
0
        public void LogObject()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                logger.Log("test", new { p1 = 2, p2 = "text" });

                string contentOfFile = repo.ReadAllText().Trim();
                AssertLogEntry("test", "{\"p1\":2,\"p2\":\"text\"}", LogLevel.Info, DataType.Object, contentOfFile);
            }
        }
Ejemplo n.º 3
0
        public void LogArray()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                logger.Log(new string[] { "aaaa", "bbbbb", "ccccc" });

                string contentOfFile = repo.ReadAllText().Trim();
                AssertLogEntry("String[]", @"\[""aaaa"",""bbbbb"",""ccccc""\]", LogLevel.Info, DataType.Object, contentOfFile);
            }
        }
Ejemplo n.º 4
0
        public void LogException()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                logger.Log("exception", new NullReferenceException("in exception"));

                string contentOfFile = repo.ReadAllText().Trim();
                AssertLogEntry("exception", "\\{\"exceptionType\":\"System\\.NullReferenceException\",\"exception\":\\{\"message\":\"in exception\",\"data\":\\{\\},\"hResult\":-2147467261\\}\\}", LogLevel.Critical, DataType.Exception, contentOfFile);
            }
        }
Ejemplo n.º 5
0
        public void CreateNewLogWithSingleEntry()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                logger.Warning("Test warning");

                string contentOfFile = repo.ReadAllText()
                                       .Trim();
                AssertLogEntry("Test warning", null, LogLevel.Warning, DataType.Text, contentOfFile);
            }
        }
Ejemplo n.º 6
0
        public void LogBool()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                bool val = true;

                logger.Log(val);

                string contentOfFile = repo.ReadAllText().Trim();
                AssertLogEntry("Boolean", "true", LogLevel.Info, DataType.Object, contentOfFile);
            }
        }
Ejemplo n.º 7
0
        public void LogDouble()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                double val = 0.3;

                logger.Log(val);

                string contentOfFile = repo.ReadAllText().Trim();
                AssertLogEntry("Double", "0.3", LogLevel.Info, DataType.Object, contentOfFile);
            }
        }
Ejemplo n.º 8
0
        public void LogHiddenString()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                object hiddenString = "hello world";

                logger.Log(hiddenString);

                string contentOfFile = repo.ReadAllText().Trim();
                AssertLogEntry("String", "\"hello world\"", LogLevel.Info, DataType.Object, contentOfFile);
            }
        }
Ejemplo n.º 9
0
        public void LogRecursive()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                var list = new List <object>();
                list.Add(list);

                logger.Log(list);

                string contentOfFile = repo.ReadAllText().Trim();
                Assert.Contains("List", contentOfFile);
            }
        }
Ejemplo n.º 10
0
        public void CreateNewLogWithMessageAndExceptionEntries()
        {
            using (var repo = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repo.LogFilePath)
                             .Build();

                logger.Log(new NullReferenceException());

                logger.Warning("Test warning");


                var contentOfFile = repo.ReadAllText()
                                    .Split('\n')
                                    .Select(str => str.Trim())
                                    .ToArray();

                Assert.Contains("NullReferenceException", contentOfFile[0]);
                AssertLogEntry("Test warning", null, LogLevel.Warning, DataType.Text, contentOfFile[1]);
            }
        }
Ejemplo n.º 11
0
        public void ShouldTransgerLog()
        {
            using (var repository = new TemporaryFileSetUp())
            {
                var logger = new JsonLoggerBuilder(repository.LogFilePath)
                             .EnableFileSplitting(100, repository.SplittingFolder)
                             .Build();

                logger.Log("test");

                string contentOfFile = repository.ReadAllText().Trim();

                Assert.False(string.IsNullOrWhiteSpace(contentOfFile));

                logger.Log("testing long message");

                contentOfFile = repository.ReadAllText().Trim();
                Assert.True(string.IsNullOrWhiteSpace(contentOfFile));

                Assert.Single(Directory.EnumerateFiles(repository.SplittingFolder));
            }
        }