public static ServiceProvider CreateTestServices( IServiceProvider applicationServices, Project project, ReportingChannel channel) { var services = new ServiceProvider(applicationServices); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new TestHostLoggerProvider(channel)); services.Add(typeof(ILoggerFactory), loggerFactory); var libraryExporter = applicationServices.GetRequiredService<ILibraryExporter>(); var export = libraryExporter.GetExport(project.Name); var projectReference = export.MetadataReferences .OfType<IMetadataProjectReference>() .Where(r => r.Name == project.Name) .FirstOrDefault(); services.Add( typeof(ISourceInformationProvider), new SourceInformationProvider(projectReference, loggerFactory.CreateLogger<SourceInformationProvider>())); services.Add(typeof(ITestDiscoverySink), new TestDiscoverySink(channel)); services.Add(typeof(ITestExecutionSink), new TestExecutionSink(channel)); return services; }
public async void Invoke_WithNonMatchingPath_IgnoresRequest() { // Arrange var elmStore = new ElmStore(); var factory = new LoggerFactory(); var optionsMock = new Mock<IOptions<ElmOptions>>(); optionsMock .SetupGet(o => o.Options) .Returns(new ElmOptions()); factory.AddProvider(new ElmLoggerProvider(elmStore, optionsMock.Object.Options)); RequestDelegate next = _ => { return Task.FromResult<object>(null); }; var captureMiddleware = new ElmCaptureMiddleware( next, factory, optionsMock.Object); var pageMiddleware = new ElmPageMiddleware( next, optionsMock.Object, elmStore); var contextMock = GetMockContext("/nonmatchingpath"); // Act await captureMiddleware.Invoke(contextMock.Object); await pageMiddleware.Invoke(contextMock.Object); // Assert // Request.Query is used by the ElmPageMiddleware to parse the query parameters contextMock.VerifyGet(c => c.Request.Query, Times.Never()); }
public MigrationsOperations( [NotNull] ILoggerProvider loggerProvider, [NotNull] string assemblyName, [NotNull] string startupAssemblyName, [NotNull] string projectDir, [NotNull] string rootNamespace, [CanBeNull] IServiceProvider dnxServices = null) { Check.NotNull(loggerProvider, nameof(loggerProvider)); Check.NotEmpty(assemblyName, nameof(assemblyName)); Check.NotEmpty(startupAssemblyName, nameof(startupAssemblyName)); Check.NotNull(projectDir, nameof(projectDir)); Check.NotNull(rootNamespace, nameof(rootNamespace)); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(loggerProvider); _loggerProvider = loggerProvider; _logger = new LazyRef<ILogger>(() => loggerFactory.CreateCommandsLogger()); _projectDir = projectDir; _rootNamespace = rootNamespace; _servicesBuilder = new DesignTimeServicesBuilder(dnxServices); _contextOperations = new DbContextOperations( loggerProvider, assemblyName, startupAssemblyName, dnxServices); }
static void Main(string[] args) { ILoggerFactory loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new DiagnosticsLoggerProvider()); loggerFactory.AddConsole(); FooManager manager = new FooManager(loggerFactory); manager.Run(); }
public MigrationTool([NotNull] ILoggerProvider loggerProvider, [NotNull] Assembly assembly) { Check.NotNull(loggerProvider, nameof(loggerProvider)); Check.NotNull(assembly, nameof(assembly)); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(loggerProvider); _loggerProvider = loggerProvider; _logger = new LazyRef<ILogger>(() => loggerFactory.CreateLogger<MigrationTool>()); _assembly = assembly; }
public void Log_AggregatesExceptionsFromMultipleLoggers() { // Arrange var store = new List<string>(); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new CustomLoggerProvider("provider1", ThrowExceptionAt.Log, store)); loggerFactory.AddProvider(new CustomLoggerProvider("provider2", ThrowExceptionAt.Log, store)); var logger = loggerFactory.CreateLogger("Test"); // Act var aggregateException = Assert.Throws<AggregateException>(() => logger.LogInformation("Hello!")); // Assert Assert.Empty(store); Assert.NotNull(aggregateException); Assert.Equal("An error occurred while writing to logger(s).", aggregateException.Message); var exceptions = aggregateException.InnerExceptions; Assert.Equal(2, exceptions.Count); Assert.Equal("provider1.Test-Error occurred while logging data.", exceptions[0].Message); Assert.Equal("provider2.Test-Error occurred while logging data.", exceptions[1].Message); }
public void IsEnabled_IgnoresExceptionInIntermediateLoggers() { // Arrange var store = new List<string>(); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new CustomLoggerProvider("provider1", ThrowExceptionAt.None, store)); loggerFactory.AddProvider(new CustomLoggerProvider("provider2", ThrowExceptionAt.IsEnabled, store)); loggerFactory.AddProvider(new CustomLoggerProvider("provider3", ThrowExceptionAt.None, store)); var logger = loggerFactory.CreateLogger("Test"); // Act var aggregateException = Assert.Throws<AggregateException>(() => logger.LogInformation("Hello!")); // Assert Assert.Equal(new[] { "provider1.Test-Hello!", "provider3.Test-Hello!" }, store); Assert.NotNull(aggregateException); Assert.Equal("An error occurred while writing to logger(s).", aggregateException.Message); Assert.Equal(1, aggregateException.InnerExceptions.Count); var exception = aggregateException.InnerExceptions[0]; Assert.Equal("provider2.Test-Error occurred while checking if logger is enabled.", exception.Message); }
public void BeginScope_IgnoresExceptionInIntermediateLoggersAndThrowsAggregateException() { // Arrange var store = new List<string>(); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new CustomLoggerProvider("provider1", ThrowExceptionAt.None, store)); loggerFactory.AddProvider(new CustomLoggerProvider("provider2", ThrowExceptionAt.BeginScope, store)); loggerFactory.AddProvider(new CustomLoggerProvider("provider3", ThrowExceptionAt.None, store)); var logger = loggerFactory.CreateLogger("Test"); // Act var aggregateException = Assert.Throws<AggregateException>(() => logger.BeginScope("Scope1")); // Assert Assert.Equal(new[] { "provider1.Test-Scope1", "provider3.Test-Scope1" }, store); Assert.NotNull(aggregateException); Assert.Equal("An error occurred while writing to logger(s).", aggregateException.Message); Assert.Equal(1, aggregateException.InnerExceptions.Count); var exception = aggregateException.InnerExceptions[0]; Assert.Equal("provider2.Test-Error occurred while creating scope.", exception.Message); }
public DatabaseTool( [CanBeNull] IServiceProvider serviceProvider, [NotNull] ILoggerProvider loggerProvider) { Check.NotNull(loggerProvider, nameof(loggerProvider)); _serviceProvider = new ServiceProvider(serviceProvider); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(loggerProvider); _logger = new LazyRef<ILogger>(() => loggerFactory.CreateLogger<DatabaseTool>()); _serviceProvider.AddService(typeof(ILogger), _logger.Value); _serviceProvider.AddService(typeof(CSharpCodeGeneratorHelper), new CSharpCodeGeneratorHelper()); _serviceProvider.AddService(typeof(ModelUtilities), new ModelUtilities()); }
public async void Invoke_WithMatchingPath_FulfillsRequest() { // Arrange var elmStore = new ElmStore(); var factory = new LoggerFactory(); var optionsMock = new Mock<IOptions<ElmOptions>>(); optionsMock .SetupGet(o => o.Options) .Returns(new ElmOptions()); factory.AddProvider(new ElmLoggerProvider(elmStore, optionsMock.Object.Options)); RequestDelegate next = _ => { return Task.FromResult<object>(null); }; var captureMiddleware = new ElmCaptureMiddleware( next, factory, optionsMock.Object); var pageMiddleware = new ElmPageMiddleware( next, optionsMock.Object, elmStore); var contextMock = GetMockContext("/Elm"); using (var responseStream = new MemoryStream()) { contextMock .SetupGet(c => c.Response.Body) .Returns(responseStream); contextMock .SetupGet(c => c.ApplicationServices) .Returns(() => null); // Act await captureMiddleware.Invoke(contextMock.Object); await pageMiddleware.Invoke(contextMock.Object); string response = Encoding.UTF8.GetString(responseStream.ToArray()); // Assert contextMock.VerifyGet(c => c.Request.Query, Times.AtLeastOnce()); Assert.True(response.Contains("<title>ASP.NET Logs</title>")); } }
public DatabaseTool( [CanBeNull] IServiceProvider serviceProvider, [NotNull] ILoggerProvider loggerProvider) { Check.NotNull(loggerProvider, nameof(loggerProvider)); _serviceProvider = new ServiceProvider(serviceProvider); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(loggerProvider); _logger = new LazyRef<ILogger>(() => loggerFactory.CreateLogger<DatabaseTool>()); _serviceProvider.AddService(typeof(ILogger), _logger.Value); _serviceProvider.AddService(typeof(IFileService), new FileSystemFileService()); _serviceProvider.AddService(typeof(CSharpCodeGeneratorHelper), new CSharpCodeGeneratorHelper()); _serviceProvider.AddService(typeof(ModelUtilities), new ModelUtilities()); var metadataReferencesProvider = new MetadataReferencesProvider(_serviceProvider); _serviceProvider.AddService(typeof(MetadataReferencesProvider), metadataReferencesProvider); var compilationService = new RoslynCompilationService(); _serviceProvider.AddService(typeof(ITemplating), new RazorTemplating(compilationService, metadataReferencesProvider)); }
private ServiceCollection SetupInitialServices() { var serviceCollection = new ServiceCollection(); #if DNX451 || DNXCORE50 var manifest = _serviceProvider.GetRequiredService<IServiceManifest>(); if (manifest != null) { foreach (var service in manifest.Services) { serviceCollection.AddTransient( service, sp => _serviceProvider.GetService(service)); } } #endif var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(_loggerProvider); var logger = loggerFactory.CreateLogger<DatabaseTool>(); serviceCollection.AddScoped(typeof(ILogger), sp => logger); serviceCollection.AddScoped<IFileService, FileSystemFileService>(); return serviceCollection; }