private void Initialize(SerilogInputConfiguration inputConfiguration, IHealthReporter healthReporter) { this.healthReporter = healthReporter; this.inputConfiguration = inputConfiguration; this.subject = new EventFlowSubject <EventData>(); this.valueSerializer = this.inputConfiguration.UseSerilogDepthLevel ? (Func <LogEventPropertyValue, object>) this.ToRawValue : this.ToRawScalar; }
/// <summary> /// Creates an instance of <see cref="SerilogInput"/> /// </summary> /// <param name="inputConfiguration">A configuration to be used to configure the input</param> /// <param name="healthReporter">A health reporter through which the input can report errors.</param> public SerilogInput(SerilogInputConfiguration inputConfiguration, IHealthReporter healthReporter) { Requires.NotNull(inputConfiguration, nameof(inputConfiguration)); Requires.NotNull(healthReporter, nameof(healthReporter)); Initialize(inputConfiguration, healthReporter); }
public void ObservesDepthLimitWhileDestructuringNestedArrays() { var healthReporterMock = new Mock <IHealthReporter>(); var observer = new Mock <IObserver <EventData> >(); IDictionary <string, object> spiedPayload = null; observer.Setup(p => p.OnNext(It.IsAny <EventData>())) .Callback <EventData>((p) => { spiedPayload = p.Payload; }); SerilogInputConfiguration configuration = new SerilogInputConfiguration() { UseSerilogDepthLevel = true }; using (var serilogInput = new SerilogInput(configuration, healthReporterMock.Object)) using (serilogInput.Subscribe(observer.Object)) { var logger = new LoggerConfiguration().WriteTo.Sink(serilogInput).Destructure.ToMaximumDepth(4).CreateLogger(); var structure = new { A = "alpha", B = new[] { new { Y = "yankee", Z = new[] { new { Alpha = "A" } } }, new { Y = "zulu", Z = new[] { new { Alpha = "B" } } } } }; logger.Information("Here is {@AStructure}", structure); } Assert.Equal("alpha", ((IDictionary <string, object>)spiedPayload["AStructure"])["A"]); object b = ((IDictionary <string, object>)spiedPayload["AStructure"])["B"]; Assert.True(b is object[]); Assert.Equal(2, ((object[])b).Length); var bb = (IDictionary <string, object>)((object[])b)[0]; Assert.Equal("yankee", bb["Y"]); object z = bb["Z"]; Assert.True(z is object[]); Assert.Collection((object[])z, z_el => Assert.Null(z_el)); }
public void CanSerializeCircularObjectGraphs() { var charlie = new EntityWithChildren() { Name = "Charlie" }; var bravo = new EntityWithChildren("Bravo", new EntityWithChildren[] { charlie }); var alpha = new EntityWithChildren("Alpha", new EntityWithChildren[] { bravo }); charlie.Children.Add(alpha); // Establish a cycle Alpha -> Bravo -> Charlie -> Alpha // Also form a cycle of direct "Sibling" references the other way round, i.e Charlie -> Bravo -> Alpha -> Charlie charlie.Sibling = bravo; bravo.Sibling = alpha; alpha.Sibling = charlie; var healthReporterMock = new Mock <IHealthReporter>(); var observer = new Mock <IObserver <EventData> >(); IDictionary <string, object> spiedPayload = null; observer.Setup(p => p.OnNext(It.IsAny <EventData>())) .Callback <EventData>((p) => { spiedPayload = p.Payload; }); SerilogInputConfiguration configuration = new SerilogInputConfiguration() { UseSerilogDepthLevel = true }; const int MaxDepth = 10; using (var serilogInput = new SerilogInput(configuration, healthReporterMock.Object)) using (serilogInput.Subscribe(observer.Object)) { var logger = new LoggerConfiguration().WriteTo.Sink(serilogInput).Destructure.ToMaximumDepth(MaxDepth).CreateLogger(); logger.Information("Here is an {@entity}", alpha); } // The fact that the loging of alpha succeeds at all (instead of going into an infinite loop) is already a good sign :-) IDictionary <string, object> entity = (IDictionary <string, object>)spiedPayload["entity"]; object[] children = null; int depth = 0; while (entity != null && entity.ContainsKey("Children")) { children = entity["Children"] as object[]; entity = null; if (children != null && children.Length > 0) { entity = children[0] as IDictionary <string, object>; } depth++; } // Every child increases depth by two because the Children property is and array. Assert.Equal(MaxDepth / 2, depth); }
/// <summary> /// Construct a <see cref="SerilogInput"/>. /// </summary> /// <param name="configuration">A configuration to be used to configure the input</param> /// <param name="healthReporter">A health reporter through which the input can report errors.</param> public SerilogInput(IConfiguration configuration, IHealthReporter healthReporter) { Requires.NotNull(configuration, nameof(configuration)); Requires.NotNull(healthReporter, nameof(healthReporter)); var inputConfiguration = new SerilogInputConfiguration(); try { configuration.Bind(inputConfiguration); } catch { healthReporter.ReportProblem($"Invalid {nameof(SerilogInputConfiguration)} configuration encountered: '{configuration}'", EventFlowContextIdentifiers.Configuration); throw; } Initialize(inputConfiguration, healthReporter); }
public void UsesSerilogMaxDestructuringDepth() { // Create object structure 5 levels deep var bravo = new EntityWithChildren() { Name = "Bravo" }; var alpha = new EntityWithChildren("Alpha", new EntityWithChildren[] { bravo }); var healthReporterMock = new Mock <IHealthReporter>(); var observer = new Mock <IObserver <EventData> >(); IDictionary <string, object> spiedPayload = null; observer.Setup(p => p.OnNext(It.IsAny <EventData>())) .Callback <EventData>((p) => { spiedPayload = p.Payload; }); SerilogInputConfiguration configuration = new SerilogInputConfiguration() { UseSerilogDepthLevel = true }; using (var serilogInput = new SerilogInput(configuration, healthReporterMock.Object)) using (serilogInput.Subscribe(observer.Object)) { var logger = new LoggerConfiguration().WriteTo.Sink(serilogInput).Destructure.ToMaximumDepth(2).CreateLogger(); logger.Information("Here is an {@entity}", alpha); } // At depth 2 there should be no children (only a null child placeholder) var e = (IDictionary <string, object>)spiedPayload["entity"]; Assert.Equal("Alpha", e["Name"]); Assert.Collection((object[])e["Children"], c => Assert.Null(c)); using (var serilogInput = new SerilogInput(configuration, healthReporterMock.Object)) using (serilogInput.Subscribe(observer.Object)) { var logger = new LoggerConfiguration().WriteTo.Sink(serilogInput).Destructure.ToMaximumDepth(3).CreateLogger(); logger.Information("Here is an {@entity}", alpha); } // At depth 3 there should be one child of Alpha, but all its properties should be blank Assert.Equal("Alpha", ((IDictionary <string, object>)spiedPayload["entity"])["Name"]); var childrenOfAlpha = ((IDictionary <string, object>)spiedPayload["entity"])["Children"] as object[]; Assert.Single(childrenOfAlpha); var b = childrenOfAlpha.First() as IDictionary <string, object>; Assert.True(b.ContainsKey("Name")); Assert.Null(b["Name"]); Assert.True(b.ContainsKey("Children")); Assert.Null(b["Children"]); using (var serilogInput = new SerilogInput(configuration, healthReporterMock.Object)) using (serilogInput.Subscribe(observer.Object)) { var logger = new LoggerConfiguration().WriteTo.Sink(serilogInput).Destructure.ToMaximumDepth(4).CreateLogger(); logger.Information("Here is an {@entity}", alpha); } // At depth 4 the child of Alpha (Bravo) should have its properties set, "Name" in particular. Assert.Equal("Alpha", ((IDictionary <string, object>)spiedPayload["entity"])["Name"]); childrenOfAlpha = ((IDictionary <string, object>)spiedPayload["entity"])["Children"] as object[]; Assert.Single(childrenOfAlpha); b = childrenOfAlpha.First() as IDictionary <string, object>; Assert.Equal("Bravo", b["Name"]); }