public void LogWrapper() { var options = new LogDNAOptions("key", LogLevel.Critical); var mockClient = new Mock <IApiClient>(); mockClient.Setup(x => x.AddLine(It.IsAny <LogLine>())).Callback <LogLine>(line => { var detail = GetDetail(line.Content); Assert.Equal("name", line.Filename); Assert.Equal("message", detail.Message); Assert.Equal("CRITICAL", detail.Level); Assert.NotNull(detail.Value); var value = ((JObject)detail.Value).ToObject <KeyValuePair <string, string> >(); Assert.Equal("key", value.Key); Assert.Equal("value", value.Value); }); var client = mockClient.Object; var logger = new LogDNALogger("name", client, options); logger.Log( LogLevel.Critical, new EventId(), new FormattedLogValues("message", new Wrapper(new KeyValuePair <string, string>("key", "value"))), null, null ); }
public void LogSimpleType() { var options = new LogDNAOptions("key", LogLevel.Critical); var mockClient = new Mock <IApiClient>(); mockClient.Setup(x => x.AddLine(It.IsAny <LogLine>())).Callback <LogLine>(line => { var detail = GetDetail(line.Content); Assert.Equal("name", line.Filename); Assert.Equal("message", detail.Message); Assert.Equal("CRITICAL", detail.Level); Assert.Null(detail.Value); }); var client = mockClient.Object; var logger = new LogDNALogger("name", client, options); logger.Log( LogLevel.Critical, new EventId(), new FormattedLogValues("message", null), null, null ); }
public void ConstructorNullClientThrowsException() { Assert.Throws <ArgumentNullException>(() => { var unused = new LogDNALogger("name", null, new LogDNAOptions("key")); }); }
async Task DoStuff3(LogDNALogger logger) { using (var unused = logger.BeginScope("DoStuff3")) { await Task.Delay(1500); } }
async Task DoStuff2(LogDNALogger logger) { using (var unused = logger.BeginScope("DoStuff2")) { await DoStuff3(logger); } }
public void ConstructorNullOptionsThrowsException() { var mockClient = new Mock <IApiClient>(); var client = mockClient.Object; Assert.Throws <ArgumentNullException>(() => { var unused = new LogDNALogger("name", client, null); }); }
public void CheckEnabled() { // Arrange var options = new LogDNAOptions("key", LogLevel.Debug) .AddNamespace("Microsoft.Something", LogLevel.Warning) .AddNamespace("Newtonsoft.Json", LogLevel.Critical); var mockClient = new Mock <IApiClient>(); var client = mockClient.Object; var logger = new LogDNALogger("Foo", client, options); // Should log debug Assert.True(logger.IsEnabled(LogLevel.Debug)); // Should log critical Assert.True(logger.IsEnabled(LogLevel.Critical)); // Shouldn't log trace Assert.False(logger.IsEnabled(LogLevel.Trace)); // Shouldn't log none Assert.False(logger.IsEnabled(LogLevel.None)); logger = new LogDNALogger("Microsoft.Something.Blah", client, options); // Shouldn't log debug Assert.False(logger.IsEnabled(LogLevel.Debug)); // Should log critical Assert.True(logger.IsEnabled(LogLevel.Critical)); // Shouldn't log trace Assert.False(logger.IsEnabled(LogLevel.Trace)); // Shouldn't log none Assert.False(logger.IsEnabled(LogLevel.None)); logger = new LogDNALogger("Newtonsoft.Json", client, options); // Shouldn't log debug Assert.False(logger.IsEnabled(LogLevel.Debug)); // Should log critical Assert.True(logger.IsEnabled(LogLevel.Critical)); // Shouldn't log trace Assert.False(logger.IsEnabled(LogLevel.Trace)); // Shouldn't log none Assert.False(logger.IsEnabled(LogLevel.None)); }
public void LogException() { var options = new LogDNAOptions("key", LogLevel.Trace) { MaxInnerExceptionDepth = 3 }; var mockClient = new Mock <IApiClient>(); mockClient.Setup(x => x.AddLine(It.IsAny <LogLine>())).Callback <LogLine>(line => { Assert.Contains("Level 17", line.Content); Assert.DoesNotContain("Level 16", line.Content); }); var client = mockClient.Object; var logger = new LogDNALogger("name", client, options); var ex = CreateTwentyLevelException(); logger.LogError(ex, ex.Message); }
public async void TestScopes() { var scopes = new List <string>(); var options = new LogDNAOptions("key", LogLevel.Trace); var mockClient = new Mock <IApiClient>(); mockClient.Setup(x => x.AddLine(It.IsAny <LogLine>())).Callback <LogLine>(line => { var detail = GetDetail(line.Content); if (!string.IsNullOrEmpty(detail.Scope)) { scopes.Add(detail.Scope); } }); var client = mockClient.Object; var logger = new LogDNALogger("name", client, options); logger.LogDebug("Starting up.."); var tasks = new List <Task> { DoStuff1(logger), DoStuff2(logger) }; await Task.WhenAll(tasks.ToArray()); Assert.Equal(6, scopes.Count); Assert.Equal("DoStuff1", scopes[0]); Assert.Equal("DoStuff2", scopes[1]); Assert.Equal("DoStuff3", scopes[2]); Assert.Equal("DoStuff1", scopes[3]); Assert.Equal("DoStuff3", scopes[4]); Assert.Equal("DoStuff2", scopes[5]); logger.LogDebug("Finished!"); }