public void FileLogger_FunctionName_FromScope()
        {
            var trace = new TestTraceWriter(TraceLevel.Info);

            var factoryMock = new Mock <IFunctionTraceWriterFactory>(MockBehavior.Strict);

            factoryMock
            .Setup(f => f.Create("SomeFunction", null))
            .Returns(trace);

            var logger = new FileLogger("SomeCategory", factoryMock.Object, (c, l) => true);

            // FunctionName comes from scope
            using (logger.BeginScope(new Dictionary <string, object>
            {
                [ScriptConstants.LoggerFunctionNameKey] = "SomeFunction"
            }))
            {
                logger.Log(LogLevel.Information, 0, new FormattedLogValues("Some Message"), null, (s, e) => s.ToString());
            }

            var traceEvent = trace.Traces.Single();

            Assert.Equal(TraceLevel.Info, traceEvent.Level);
            Assert.Equal("Some Message", traceEvent.Message);
            Assert.Equal("SomeCategory", traceEvent.Source);
        }
Esempio n. 2
0
 public void BeginScope_ReturnsDisposable()
 {
     using (var disposable = sut.BeginScope(new object()))
     {
         disposable.Should().NotBeNull();
     }
 }
        public void FileLoggerShouldUse()
        {
            var factory = new LogFileNameFactory(_path, LogFileCycle.OneFile, "test", "log");

            var logFileName = Path.Combine(_path, "test.log");

            if (File.Exists(logFileName))
            {
                File.Delete(logFileName);
            }

            var logger = new FileLogger(factory, "test-logger");

            logger.LogInformation("information-message");
            using (logger.BeginScope("FunctionCall"))
            {
                logger.LogWarning("warning-message");
                logger.LogTrace("trace-message");
            }
            logger.LogError("error-message");

            var logContent = File.ReadAllText(logFileName);

            Assert.Contains("test-logger", logContent);
            Assert.Contains("information-message", logContent);
            Assert.Contains("error-message", logContent);
            Assert.Contains("warning-message", logContent);
            Assert.Contains("trace-message", logContent);
            Assert.Contains("=> FunctionCall", logContent);
        }
Esempio n. 4
0
        public void FileLogger_WritesToFiles()
        {
            FileLogger logger = new FileLogger("Tests.FileLogger", _config, null);

            using (logger.BeginScope(new Dictionary <string, object>
            {
                [ScriptConstants.LoggerFunctionNameKey] = _functionShortName
            }))
            {
                logger.LogInformation("line 1");
                logger.LogInformation("line {number}", 2);
            }

            FileLogger.FlushAllTraceWriters();

            string logFile = Directory.EnumerateFiles(_functionLogPath).Single();

            string[] fileLines = File.ReadAllLines(logFile);
            Assert.Equal(2, fileLines.Length);
            Assert.EndsWith("line 1", fileLines[0]);
            Assert.EndsWith("line 2", fileLines[1]);
        }