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'.*"); }
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"); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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'."); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }