Esempio n. 1
0
        public TestHelper()
            : base(typeof(TestHelper).Assembly)
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Connection.RemoteIpAddress = IPAddress.Parse("127.0.0.1");
            _httpContextAccessor = Mock.Of <IHttpContextAccessor>(x => x.HttpContext == httpContext);
            _ipResolver          = new AspNetCoreIpResolver(_httpContextAccessor);

            string contentRoot = Assembly.GetExecutingAssembly().GetRootDirectorySafe();

            // The mock for IWebHostEnvironment has caused a good few issues.
            // We can UseContentRoot, UseWebRoot etc on the host builder instead.
            // possibly going down rabbit holes though as would need to cleanup all usages of
            // GetHostingEnvironment & GetWebHostEnvironment.
            var hostEnvironment = new Mock <IWebHostEnvironment>();

            // This must be the assembly name for the WebApplicationFactory to work.
            hostEnvironment.Setup(x => x.ApplicationName).Returns(GetType().Assembly.GetName().Name);
            hostEnvironment.Setup(x => x.ContentRootPath).Returns(() => contentRoot);
            hostEnvironment.Setup(x => x.ContentRootFileProvider).Returns(() => new PhysicalFileProvider(contentRoot));
            hostEnvironment.Setup(x => x.WebRootPath).Returns(() => WorkingDirectory);
            hostEnvironment.Setup(x => x.WebRootFileProvider).Returns(() => new PhysicalFileProvider(WorkingDirectory));
            hostEnvironment.Setup(x => x.EnvironmentName).Returns("Tests");

            // We also need to expose it as the obsolete interface since netcore's WebApplicationFactory casts it.
            hostEnvironment.As <Microsoft.AspNetCore.Hosting.IHostingEnvironment>();

            _hostEnvironment = hostEnvironment.Object;

            _hostingLifetime     = new AspNetCoreApplicationShutdownRegistry(Mock.Of <IHostApplicationLifetime>());
            ConsoleLoggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
            ProfilingLogger      = new ProfilingLogger(ConsoleLoggerFactory.CreateLogger <ProfilingLogger>(), Profiler);
        }
        public void CreateSqlServerLoggerTest()
        {
            var logger = new SeriLogger(new Mock <ApplicationLoggerSettings>().Object);
            var config = logger.ConfigureSerilog();

            var result = ConsoleLoggerFactory.CreateLogger(config);

            Assert.NotNull(result);
        }