public void LogUsesDefaultFormatterWhenConfigIsNull()
        {
            var logLevel = LogLevel.Error;
            var eventId  = Model.Create <EventId>();
            var state    = Guid.NewGuid().ToString();
            var message  = Guid.NewGuid().ToString();

            string Formatter(string logState, Exception?error) => message;

            var categoryName = Guid.NewGuid().ToString();
            var expected     = string.Format(CultureInfo.InvariantCulture,
                                             "{0}{1} [{2}]: {3}",
                                             string.Empty,
                                             logLevel,
                                             eventId.Id,
                                             message);

            var output = Substitute.For <ITestOutputHelper>();

            var sut = new TestOutputLogger(categoryName, output);

            sut.Log(logLevel, eventId, state, null, Formatter);

            output.Received(1).WriteLine(Arg.Any <string>());
            output.Received().WriteLine(expected);
        }
        public void LogWritesMessageUsingSpecifiedLineFormatter()
        {
            var logLevel     = LogLevel.Error;
            var eventId      = Model.Create <EventId>();
            var state        = Guid.NewGuid().ToString();
            var message      = Guid.NewGuid().ToString();
            var exception    = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            var categoryName = Guid.NewGuid().ToString();
            var expected     = Guid.NewGuid().ToString();

            string Formatter(string logState, Exception?error) => message;

            var formatter = Substitute.For <ILogFormatter>();
            var config    = new LoggingConfig {
                Formatter = formatter
            };

            formatter.Format(0, categoryName, logLevel, eventId, message, exception).Returns(expected);

            var output = Substitute.For <ITestOutputHelper>();

            var sut = new TestOutputLogger(categoryName, output, config);

            sut.Log(logLevel, eventId, state, exception, Formatter);

            formatter.Received().Format(0, categoryName, logLevel, eventId, message, exception);

            output.Received().WriteLine(expected);
        }
        public void WritesScopeMessagesUsingFormatter(string?sensitiveValue, string state, string expected)
        {
            var logLevel     = LogLevel.Error;
            var eventId      = Model.Create <EventId>();
            var message      = Guid.NewGuid().ToString();
            var categoryName = Guid.NewGuid().ToString();

            string Formatter(string logState, Exception?error) => message;

            var config = new LoggingConfig();

            if (sensitiveValue != null)
            {
                config.SensitiveValues.Add(sensitiveValue);
            }

            var output = Substitute.For <ITestOutputHelper>();

            var sut = new TestOutputLogger(categoryName, output, config);

            using (sut.BeginScope(state))
            {
                sut.Log(logLevel, eventId, state, null, Formatter);
            }

            output.Received().WriteLine($"<Scope: {expected}>");
            output.Received().WriteLine($"   {logLevel} [{eventId.Id}]: {message}");
            output.Received().WriteLine($"</Scope: {expected}>");
        }
        public void WritesScopeMessagesWithStructuredDataUsingFormatter()
        {
            var logLevel       = LogLevel.Error;
            var eventId        = Model.Create <EventId>();
            var message        = Guid.NewGuid().ToString();
            var sensitiveValue = Guid.NewGuid().ToString();
            var categoryName   = Guid.NewGuid().ToString();
            var config         = new LoggingConfig().Set(x => x.SensitiveValues.Add(sensitiveValue));

            var state = Model.Create <StructuredData>().Set(x => x.FirstName += " " + sensitiveValue);

            var output = Substitute.For <ITestOutputHelper>();

            var sut = new TestOutputLogger(categoryName, output, config);

            using (sut.BeginScope(state))
            {
                sut.Log(logLevel, eventId, message);
            }

            output.DidNotReceive().WriteLine(Arg.Is <string>(x => x.Contains(sensitiveValue)));
        }
        public void LogWritesExceptionTest()
        {
            var logLevel  = LogLevel.Information;
            var eventId   = Model.Create <EventId>();
            var state     = Guid.NewGuid().ToString();
            var message   = Guid.NewGuid().ToString();
            var exception = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            string Formatter(string logState, Exception?error) => message;

            var categoryName = Guid.NewGuid().ToString();

            var output = Substitute.For <ITestOutputHelper>();

            var sut = new TestOutputLogger(categoryName, output);

            sut.Log(logLevel, eventId, state, exception, Formatter);

            output.Received(1).WriteLine(Arg.Any <string>());
            output.Received()
            .WriteLine(Arg.Is <string>(x => x.Contains(exception.ToString(), StringComparison.OrdinalIgnoreCase)));
        }