public void InvalidLineEnding()
    {
        Action action = () => _ = new Log4NetTextFormatter(c => c.UseLineEnding((LineEnding)4));

        action.Should().ThrowExactly <ArgumentOutOfRangeException>()
        .WithMessage("The value of argument 'lineEnding' (4) is invalid for enum type 'LineEnding'.*");
    }
Esempio n. 2
0
    public void NullLogEventThrowsArgumentNullException()
    {
        // Arrange
        var formatter = new Log4NetTextFormatter();

        // Act
        var action = () => formatter.Format(null !, TextWriter.Null);

        // Assert
        action.Should().ThrowExactly <ArgumentNullException>().WithParameterName("logEvent")
        .Which.StackTrace !.TrimStart().Should().StartWith("at Serilog.Formatting.Log4Net.Log4NetTextFormatter.Format");
    }
Esempio n. 3
0
    public Task DefaultFormatProvider()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(messageTemplate: "π = {π}", properties: new LogEventProperty("π", new ScalarValue(3.14m)));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 4
0
    public Task LoggerNameStructureValue()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(properties: new LogEventProperty(Constants.SourceContextPropertyName, new StructureValue(new List <LogEventProperty>())));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 5
0
    public Task LoggerName()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(properties: new LogEventProperty(Constants.SourceContextPropertyName, new ScalarValue("Namespace.Component.Class")));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 6
0
    public Task NullProperty()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(properties: new LogEventProperty("n/a", new ScalarValue(null)));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 7
0
    public Task NoNamespace()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent();
        var formatter = new Log4NetTextFormatter(options => options.UseNoXmlNamespace());

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 8
0
    public Task IndentationSettings(Indentation indentation, byte size)
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent();
        var formatter = new Log4NetTextFormatter(options => options.UseIndentationSettings(new IndentationSettings(indentation, size)));

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output).UseParameters(indentation, size));
    }
Esempio n. 9
0
    public Task XmlElementsLineEnding(LineEnding lineEnding)
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent();
        var formatter = new Log4NetTextFormatter(options => options.UseLineEnding(lineEnding));

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output).UseParameters(lineEnding));
    }
Esempio n. 10
0
    public Task MessageCDataMode(CDataMode mode, bool needsEscaping)
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(messageTemplate: needsEscaping ? ">> Hello from Serilog <<" : "Hello from Serilog");
        var formatter = new Log4NetTextFormatter(options => options.UseCDataMode(mode));

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output).UseParameters(mode, needsEscaping));
    }
Esempio n. 11
0
    public Task LogEventLevel(LogEventLevel level)
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(level);
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output).UseParameters(level));
    }
Esempio n. 12
0
    public Task MachineNameProperty(string?machineName)
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(properties: new LogEventProperty(MachineNameEnricher.MachineNamePropertyName, new ScalarValue(machineName)));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output).UseParameters(machineName));
    }
Esempio n. 13
0
    public Task MachineNamePropertyStructureValue()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(properties: new LogEventProperty(MachineNameEnricher.MachineNamePropertyName, new StructureValue(new List <LogEventProperty>())));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 14
0
    public Task ThreadIdProperty(int?threadId)
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(properties: new LogEventProperty(ThreadIdEnricher.ThreadIdPropertyName, new ScalarValue(threadId)));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output).UseParameters(threadId));
    }
Esempio n. 15
0
    public Task Exception()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(exception: new Exception("An error occurred").SetStackTrace(@"  at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 16
0
    public Task ExceptionFormatter()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(exception: new Exception("An error occurred"));
        var formatter = new Log4NetTextFormatter(options => options.UseExceptionFormatter(e => $"Type = {e.GetType().FullName}\nMessage = {e.Message}"));

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 17
0
    public void InvalidLogEventLevelThrowsArgumentOutOfRangeException()
    {
        // Arrange
        var logEvent  = CreateLogEvent((LogEventLevel)(-1));
        var formatter = new Log4NetTextFormatter();

        // Act
        var action = () => formatter.Format(logEvent, TextWriter.Null);

        // Assert
        action.Should().ThrowExactly <ArgumentOutOfRangeException>()
        .And.Message.Should().StartWith("The value of argument 'level' (-1) is invalid for enum type 'LogEventLevel'.");
    }
Esempio n. 18
0
    public Task CustomLogEventPropertyValue()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(properties: new LogEventProperty("Custom", new CustomLogEventPropertyValue("CustomValue")));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 19
0
    public Task DomainAndUserNameProperty(string?environmentUserName)
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent(properties: new LogEventProperty(EnvironmentUserNameEnricher.EnvironmentUserNamePropertyName, new ScalarValue(environmentUserName)));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output).UseParameters(environmentUserName));
    }
Esempio n. 20
0
    public Task SequenceProperty()
    {
        // Arrange
        using var output = new StringWriter();
        var values    = new LogEventPropertyValue[] { new ScalarValue(1), new ScalarValue("two"), CreateDictionary() };
        var logEvent  = CreateLogEvent(properties: new LogEventProperty("Sequence", new SequenceValue(values)));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 21
0
    public Task TwoEvents()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent  = CreateLogEvent();
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 22
0
    public Task TwoProperties()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent = CreateLogEvent(properties: new[] {
            new LogEventProperty("one", new ScalarValue(1)),
            new LogEventProperty("two", new ScalarValue(2)),
        });
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 23
0
    public Task FilterProperty()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent = CreateLogEvent(properties: new[] {
            new LogEventProperty("one", new ScalarValue(1)),
            new LogEventProperty("two", new ScalarValue(2)),
        });
        var formatter = new Log4NetTextFormatter(options => options.UsePropertyFilter((_, propertyName) => propertyName != "one"));

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 24
0
    public Task ExplicitFormatProvider()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent       = CreateLogEvent(messageTemplate: "π = {π}", properties: new LogEventProperty("π", new ScalarValue(3.14m)));
        var formatProvider = new NumberFormatInfo {
            NumberDecimalSeparator = ","
        };
        var formatter = new Log4NetTextFormatter(options => options.UseFormatProvider(formatProvider));

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 25
0
    public Task Log4JCompatibility()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent = CreateLogEvent(
            exception: new Exception("An error occurred").SetStackTrace(@"  at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"),
            properties: new LogEventProperty("π", new ScalarValue(3.14m))
            );
        var formatter = new Log4NetTextFormatter(c => c.UseLog4JCompatibility());

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 26
0
    public Task DictionaryProperty()
    {
        // Arrange
        using var output = new StringWriter();
        var values = new[]
        {
            new KeyValuePair <ScalarValue, LogEventPropertyValue>(new ScalarValue(1), new ScalarValue("one")),
            new KeyValuePair <ScalarValue, LogEventPropertyValue>(new ScalarValue("two"), new ScalarValue(2)),
            new KeyValuePair <ScalarValue, LogEventPropertyValue>(new ScalarValue("dictionary"), CreateDictionary()),
        };
        var logEvent  = CreateLogEvent(properties: new LogEventProperty("Dictionary", new DictionaryValue(values)));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 27
0
    public Task StructureProperty()
    {
        // Arrange
        using var output = new StringWriter();
        var values = new[]
        {
            new LogEventProperty("1", new ScalarValue("one")),
            new LogEventProperty("two", new ScalarValue(2)),
            new LogEventProperty("dictionary", CreateDictionary()),
        };
        var logEvent  = CreateLogEvent(properties: new LogEventProperty("Structure", new StructureValue(values)));
        var formatter = new Log4NetTextFormatter();

        // Act
        formatter.Format(logEvent, output);

        // Assert
        return(Verify(output));
    }
Esempio n. 28
0
    public Task FilterPropertyThrowing()
    {
        // Arrange
        using var output = new StringWriter();
        var logEvent = CreateLogEvent(properties: new[] {
            new LogEventProperty("one", new ScalarValue(1)),
            new LogEventProperty("two", new ScalarValue(2)),
        });
        var formatter = new Log4NetTextFormatter(options => options.UsePropertyFilter((_, propertyName) =>
        {
            if (propertyName == "one")
            {
                return(true);
            }
            throw new InvalidOperationException($"Can't handle property '{propertyName}'.");
        }));

        // Act
        formatter.Format(logEvent, output);

        // Assert
        SelfLogValue.Should().Contain("[Serilog.Formatting.Log4Net.Log4NetTextFormatter] An exception was thrown while filtering property 'two'.");
        return(Verify(output));
    }