public void MicrosoftExtensionsLoggerLogExceptionsToTrace() { var listener = new DelayAsyncTraceListener(0); var services = new ServiceCollection(); services.AddLogging(options => { options.UseDecosDiagnostics(); }); services.AddTraceSourceLogging(options => { options.AddTraceListener(listener); }); var provider = services.BuildServiceProvider(); var logger = provider.GetRequiredService <ILogger <DecosDiagnosticsServiceCollectionExtensionsTests> >(); try { throw new Exception("Test exception"); } catch (Exception ex) { logger.LogError(ex, "test"); } Assert.IsTrue(listener.Invocations.OfType <LogData>().Any(x => x.Data is Exception)); }
public void MicrosoftExtensionsLoggerLogsMessageTemplatesWithoutDataToTrace() { var listener = new DelayAsyncTraceListener(0); var services = new ServiceCollection(); services.AddLogging(options => { options.UseDecosDiagnostics(); }); services.AddTraceSourceLogging(options => { options.AddTraceListener(listener); }); var provider = services.BuildServiceProvider(); var logger = provider.GetRequiredService <ILogger <DecosDiagnosticsServiceCollectionExtensionsTests> >(); var p1 = 1; var p2 = 2; logger.LogInformation("Test {p1} {p2}", p1, p2); var logData = listener.Invocations.OfType <LogData>().SingleOrDefault(); Assert.AreEqual("Test 1 2", logData.ToString()); }
public void ApplicationPerformsGracefulShutdownAfterInjection() { var listener = new DelayAsyncTraceListener(500); var services = new ServiceCollection(); services.AddSingleton <IHostApplicationLifetime>(new DummyApplicationLifetime(10)); services.AddTraceSourceLogging(options => { options.AddTraceListener(listener); }); var provider = services.BuildServiceProvider(); var log = provider.GetRequiredService <ILog <DecosDiagnosticsServiceCollectionExtensionsTests> >(); for (int i = 0; i < 10; i++) { log.Info("Test"); } Assert.AreNotEqual(0, listener.QueueCount); var handler = provider.GetRequiredService <ApplicationShutdownHandler>(); var lifeTime = provider.GetRequiredService <IHostApplicationLifetime>(); lifeTime.StopApplication(); Assert.AreEqual(0, listener.QueueCount); }
public void MicrosoftExtensionsLoggerLogsMessageTemplatesWithSerializableData() { const int p1 = 1; const int p2 = 2; var listener = new DelayAsyncTraceListener(0); var services = new ServiceCollection(); services.AddLogging(options => { options.UseDecosDiagnostics(); }); services.AddTraceSourceLogging(options => { options.AddTraceListener(listener); }); var provider = services.BuildServiceProvider(); var logger = provider.GetRequiredService <ILogger <DecosDiagnosticsServiceCollectionExtensionsTests> >(); logger.LogInformation("Test {p1} {p2}", p1, p2); var logData = listener.Invocations.OfType <LogData>().SingleOrDefault(); var json = Newtonsoft.Json.JsonConvert.SerializeObject(logData.Data); Assert.AreEqual($"{{\"p1\":{p1},\"p2\":{p2}}}", json); }
public void MicrosoftExtensionsLoggerLogsStructuredMessagesToTrace() { var listener = new DelayAsyncTraceListener(0); var services = new ServiceCollection(); services.AddLogging(options => { options.UseDecosDiagnostics(); }); services.AddTraceSourceLogging(options => { options.AddTraceListener(listener); }); var provider = services.BuildServiceProvider(); var logger = provider.GetRequiredService <ILogger <DecosDiagnosticsServiceCollectionExtensionsTests> >(); // Very contrived example but still possible within the interface logger.Log(Microsoft.Extensions.Logging.LogLevel.Information, 0, new Dictionary <string, object>() { ["p1"] = 1, ["p2"] = 2 }, null, (state, ex) => "Test"); var logData = listener.Invocations.OfType <LogData>().SingleOrDefault(); var data = (logData.Data as IEnumerable <KeyValuePair <string, object> >)?.ToArray(); CollectionAssert.Contains(data, new KeyValuePair <string, object>("p1", 1)); CollectionAssert.Contains(data, new KeyValuePair <string, object>("p2", 2)); }
public async Task FactoryShutdownCanBeCancelled() { var listener = new DelayAsyncTraceListener(100); var factory = new LogFactoryBuilder() .UseTraceSource() .AddTraceListener(listener) .Build(); var log = factory.Create("Test"); for (int i = 0; i < 10; i++) { log.Info(i); } var cancellationTokenSource = new CancellationTokenSource(250); try { await factory.ShutdownAsync(cancellationTokenSource.Token); } catch (OperationCanceledException) { } Assert.AreNotEqual(0, listener.QueueCount); }
public async Task FactoryOnlyHooksOncePerAsyncListener() { var listener = new DelayAsyncTraceListener(5); var factory = new LogFactoryBuilder() .UseTraceSource() .AddTraceListener(listener) .Build(); for (int i = 0; i < 100; i++) { var log = factory.Create($"Logger{i}"); log.Info($"{i}"); } Assert.AreEqual(1, ((TraceSourceLogFactory)factory)._shutdownTasks.Count); }
public void TraceListenerWithHigherMinimumLevelDoesNotGetCalled() { var traceListener = new DelayAsyncTraceListener(-1); var factory = new LogFactoryBuilder() .UseTraceSource() .AddTraceListener(traceListener, LogLevel.Warning) .SetMinimumLogLevel(LogLevel.Information) .Build(); var log = (TraceSourceLog)factory.Create("Test"); Assert.IsTrue(log.IsEnabled(LogLevel.Information)); log.Info("Test"); Assert.IsFalse(traceListener.TraceCalled); }
public void BuilderRunsAsyncListeners() { var listener = new DelayAsyncTraceListener(-1); var factory = new LogFactoryBuilder() .UseTraceSource() .AddTraceListener(listener) .Build(); var log = factory.Create("Test"); for (int i = 0; i < 100; i++) { log.Info(i); } Assert.IsTrue(listener.ProcessQueueAsyncCalled); }
public async Task FactoryAllowsForGracefulShutdown() { var listener = new DelayAsyncTraceListener(100); var factory = new LogFactoryBuilder() .UseTraceSource() .AddTraceListener(listener) .Build(); var log = factory.Create("Test"); for (int i = 0; i < 10; i++) { log.Info(i); } await factory.ShutdownAsync(); Assert.AreEqual(0, listener.QueueCount); }
public async Task AsyncTraceListenerDoesNotEmptyQueueWhenAborting() { var listener = new DelayAsyncTraceListener(Delay); for (int i = 0; i < EventCount; i++) { listener.WriteLine(i); } var cancellation = new CancellationTokenSource(Delay); try { await listener.ProcessQueueAsync(CancellationToken.None, cancellation.Token); } catch (OperationCanceledException) { } Assert.AreNotEqual(0, listener.QueueCount); }
public async Task AsyncTraceListenerEmptiesQueueBeforeShuttingDown() { var listener = new DelayAsyncTraceListener(Delay); for (int i = 0; i < EventCount; i++) { listener.WriteLine(i); } var shutdown = new CancellationTokenSource(Delay); var cancellation = new CancellationTokenSource(Delay * (EventCount + 1)); try { await listener.ProcessQueueAsync(shutdown.Token, cancellation.Token); } catch (OperationCanceledException) { } Assert.AreEqual(0, listener.QueueCount); }
public void MicrosoftExtensionsLoggerLogsMessagesToTrace() { var listener = new DelayAsyncTraceListener(0); var services = new ServiceCollection(); services.AddLogging(options => { options.UseDecosDiagnostics(); }); services.AddTraceSourceLogging(options => { options.AddTraceListener(listener); }); var provider = services.BuildServiceProvider(); var logger = provider.GetRequiredService <ILogger <DecosDiagnosticsServiceCollectionExtensionsTests> >(); logger.LogInformation("Test"); Assert.IsTrue(listener.Invocations.Contains("Test")); }