public void SerializesEventCorrectly() { var formatter = LogSettingsHelper.GetFormatter(); var scopeProvider = new LoggerExternalScopeProvider(); var logger = new TestLogger(scopeProvider, formatter, new DateTime(2021, 09, 13, 10, 40, 57)); scopeProvider.Push(new Dictionary <string, object> { { "OtherProperty", 62 } }); scopeProvider.Push("Some other value"); logger.Log( LogLevel.Debug, new InvalidOperationException("Oops, just a test!"), "This is a test with a {Value}", 123); var expected = @"{""@t"":""2021-09-13T10:40:57.0000000Z"",""@m"":""This is a test with a 123"",""@l"":""Debug"",""@x"":""System.InvalidOperationException: Oops, just a test!"",""Value"":123,""OtherProperty"":62,""Scopes"":[""Some other value""]""@i"":""a9a87aee"",""ddsource"":""csharp"",""service"":""MyTestService"",""host"":""some_host""}"; logger.Logs.Should().ContainSingle(expected); }
public void DoesntAddPropertiesThatAreAlreadyAddedTwice() { var formatter = LogSettingsHelper.GetFormatter(); var scopeProvider = new LoggerExternalScopeProvider(); var logger = new TestLogger(scopeProvider, formatter, new DateTime(2021, 09, 13, 10, 40, 57)); var properties = new Dictionary <string, object> { { "Host", nameof(DoesntAddPropertiesThatAreAlreadyAddedTwice) + "host" }, { "dd_service", nameof(DoesntAddPropertiesThatAreAlreadyAddedTwice) + "service" }, { "ddsource", nameof(DoesntAddPropertiesThatAreAlreadyAddedTwice) + "source" }, { "ddtags", nameof(DoesntAddPropertiesThatAreAlreadyAddedTwice) + ":tag" }, }; using (scopeProvider.Push(properties)) using (scopeProvider.Push("Another value")) { logger.Log( LogLevel.Debug, new InvalidOperationException("Oops, just a test!"), "This is a test with a {Value} and a {Host}", 123, "some host"); } var log = logger.Logs.Should().ContainSingle().Subject; var json = JObject.Parse(log); // should have all the added properties foreach (var property in properties) { json.Properties() .Where(x => string.Equals(property.Key, x.Name, StringComparison.OrdinalIgnoreCase)) .Should() .ContainSingle() .Which.Value.ToString() .Should() .Be(property.Value.ToString()); } }
public void Log_CorrectScopeInformation() { var formatter = CreateFormatter(new GoogleCloudConsoleFormatterOptions { IncludeScopes = true }); var logEntry = new LogEntry <string>(LogLevel.Information, "LogCategory", new EventId(1), "test", exception: null, (state, exception) => state); var writer = new StringWriter { NewLine = "\n" }; var scopeProvider = new LoggerExternalScopeProvider(); scopeProvider.Push("Inner Scope"); formatter.Write(logEntry, scopeProvider: scopeProvider, writer); var expectedJson = "{\"message\":\"test\",\"severity\":\"INFO\",\"scopes\":[\"Inner Scope\"]}\n"; var actualJson = writer.ToString(); Assert.Equal(expectedJson, actualJson); }
public IDisposable BeginScope <TState>(TState state) => _scopeProvider.Push(state);
/// <summary> /// /// </summary> /// <typeparam name="TState"></typeparam> /// <param name="state"></param> /// <returns></returns> public IDisposable BeginScope <TState>(TState state) { LoggerInformation[] loggers = this.Loggers; bool flag = loggers == null; IDisposable result; if (flag) { result = NullScope.Instance; } else { LoggerExternalScopeProvider scopeProvider = this._loggerFactory.ScopeProvider; int scopeCount = this._scopeCount; bool flag2 = scopeProvider != null; if (flag2) { bool flag3 = scopeCount == 0; if (flag3) { return(scopeProvider.Push(state)); } scopeCount++; } Logger.Scope scope = new Logger.Scope(scopeCount); List <Exception> exceptions = null; foreach (LoggerInformation loggerInformation in loggers) { bool externalScope = loggerInformation.ExternalScope; if (!externalScope) { try { scopeCount--; bool flag4 = scopeCount >= 0; if (flag4) { IDisposable disposable = loggerInformation.Logger.BeginScope <TState>(state); scope.SetDisposable(scopeCount, disposable); } } catch (Exception ex) { bool flag5 = exceptions == null; if (flag5) { exceptions = new List <Exception>(); } exceptions.Add(ex); } } } bool flag6 = scopeProvider != null; if (flag6) { scope.SetDisposable(0, scopeProvider.Push(state)); } bool flag7 = exceptions != null && exceptions.Count > 0; if (flag7) { throw new AggregateException("An error occurred while writing to logger(s).", exceptions); } result = scope; } return(result); }
public IDisposable Push(object state) { return(ExternalScopeProvider.Push(state)); }
public IDisposable BeginScope <TState>(TState state) { return(_scopeProvider.Push(state)); }