Ejemplo n.º 1
0
        public void LoggerCreatesDirectoryIfItDoesNotExist()
        {
            var path = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "testDir"));

            if (path.Exists)
            {
                path.Delete(true);
            }

            const string fileName   = "test.log";
            var          outputFile = new FileInfo(Path.Combine(path.FullName, fileName));
            var          tested     = new FileLoggerBase(outputFile.FullName)
            {
                CreateDirIfNotExists = true
            };

            try
            {
                tested.LogInfo("test");
                path.Refresh();
                Assert.True(path.Exists);
            }
            finally
            {
                outputFile.Delete();
                path.Delete();
            }
        }
Ejemplo n.º 2
0
        public void FilePathDoesNotChange()
        {
            const string path   = "testPath.test";
            var          tested = new FileLoggerBase(path);

            Assert.Contains(path, tested.PathToLog);
        }
Ejemplo n.º 3
0
        public void AppendTest()
        {
            var myPath = $"{typeof(FileLoggerBaseTest).Namespace}.{nameof(AppendTest)}.log";

            try
            {
                var testedFileLogger = new FileLoggerBase(myPath);
                var expectedError1   = new Exception("testError1");
                var expectedError2   = "testError2";
                var expectedWarning  = "testWarning1";
                var expectedinfo     = "testInfo1";
                testedFileLogger.LogError(expectedError1);
                testedFileLogger.LogError(expectedError2);
                testedFileLogger.LogWarning(expectedWarning);
                testedFileLogger.LogInfo(expectedinfo);
                var received = File.ReadAllText(myPath);
                Assert.True(File.Exists(myPath));
                Assert.Contains(expectedError1.Message, received);
                Assert.Contains(expectedError2, received);
                Assert.Contains(expectedWarning, received);
                Assert.Contains(expectedinfo, received);
                Assert.Contains(MessageSeverity.Error.ToString(), received);
                Assert.Contains(MessageSeverity.Information.ToString(), received);
                Assert.Contains(MessageSeverity.Warning.ToString(), received);
            }
            finally
            {
                File.Delete(myPath);
            }
        }
Ejemplo n.º 4
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello in Passive Debouncer test harness! " +
                              "This is the load test. The file logger will be spammed with the number of messages (random GUIDs) you choose from multiple threads." +
                              " The debouncer role is to keep the messages queued in memory and not flush immidiatly. How many messages would you like to send from the thread pool?");
            Console.Write("Number of messages: ");
            var line = Console.ReadLine();
            int validInteger;

            while (!int.TryParse(line, out validInteger) && !(validInteger > 0))
            {
                Console.WriteLine($"There was a problem with your input: {line} is not a valid integer in this context. Enter any natural number greater than 0.");
                Console.Write("Number of messages: ");
                line = Console.ReadLine();
            }
            Console.Write($"You chose {validInteger}.");
            if (validInteger > 100000)
            {
                Console.Write(" This can take a while:)");
            }
            Console.WriteLine();
            var file = new FileInfo(Path.ChangeExtension(Path.GetRandomFileName(), "txt"));

            Console.WriteLine($"The contents will be written to {file.FullName}. Note, that passive debouncer is 'lazy' and will write the rest on next request.");

            var fileLogger = new FileLoggerBase(file.FullName);
            var watch      = Stopwatch.StartNew();

            using (var passiveDebouncer = new PassiveDebouncer())
                using (var wrapper = new QueuedLoggerWrapper(fileLogger, passiveDebouncer))
                {
                    Parallel.For(0, validInteger, x =>
                    {
                        wrapper.LogInfo(Guid.NewGuid().ToString());
                    });
                    watch.Stop();
                    Console.WriteLine($"Finished in {watch.Elapsed} with {wrapper.Failures} failures, after {wrapper.Requests} log requests");
                }
            Console.WriteLine("Open file? y/n");
            var key = Console.ReadKey();

            if (key.KeyChar == 'y')
            {
                using (var process = Process.Start(new ProcessStartInfo {
                    FileName = fileLogger.OutputFile.FullName, UseShellExecute = true
                }))
                { }
            }
            Console.WriteLine();
            Console.WriteLine("Delete file? y/n");
            key = Console.ReadKey();
            if (key.KeyChar == 'y')
            {
                fileLogger.OutputFile.Delete();
            }
        }
Ejemplo n.º 5
0
        public void CreateFileLogger()
        {
            var myPath = $"{typeof(FileLoggerBaseTest).Namespace}.{nameof(CreateFileLogger)}.log";

            try
            {
                var testedFileLogger = new FileLoggerBase(myPath);
                testedFileLogger.LogInfo("test7013");
                Assert.True(File.Exists(myPath));
            }
            finally
            {
                File.Delete(myPath);
            }
        }
Ejemplo n.º 6
0
        public void LoggerEaseOfUse()
        {
            var myPath   = $"{typeof(FileLoggerBaseTest).Namespace}.{nameof(LoggerEaseOfUse)}.log";
            var expected = "testing ease of use.";
            var myLogger = new FileLoggerBase(myPath);

            try
            {
                myLogger.LogInfo(expected);
                var actual = File.ReadAllText(myPath);
                Assert.Contains(expected, actual);
            }
            finally
            {
                File.Delete(myPath);
            }
        }
Ejemplo n.º 7
0
        public void LogWarning()
        {
            var myPath = $"{typeof(FileLoggerBaseTest).Namespace}.{nameof(LogWarning)}.log";

            try
            {
                var testedFileLogger = new FileLoggerBase(myPath);
                var expected         = "test";
                testedFileLogger.LogWarning(expected);
                var received = File.ReadAllText(myPath);
                Assert.True(File.Exists(myPath));
                Assert.Contains(expected, received);
                Assert.Contains(MessageSeverity.Warning.ToString(), received);
            }
            finally
            {
                File.Delete(myPath);
            }
        }
Ejemplo n.º 8
0
        public void FlushAutoAlwaysReturnsTrue()
        {
            var tested1 = new FileLoggerBase();

            Assert.True(tested1.FlushAuto);
        }
Ejemplo n.º 9
0
        public void DefaultExtensionTest1()
        {
            var tested1 = new FileLoggerBase();

            Assert.True(tested1.PathToLog.EndsWith(".log"), $"{tested1.PathToLog} does not end with \".log\"");
        }
Ejemplo n.º 10
0
        public void IsThreadSafeAlwaysReturnsTrue()
        {
            var tested1 = new FileLoggerBase();

            Assert.True(tested1.IsThreadSafe);
        }