【发布时间】:2020-04-09 00:36:41
【问题描述】:
我在我的 NLog 配置中添加了一些 aspnet 布局渲染器,我的网站一切正常,但是当我尝试运行我的集成测试时,它们失败并显示以下错误消息:
NLog.NLogConfigurationException:在 NLog.Targets.DatabaseParameterInfo 上设置属性“布局”时出错 ----> System.ArgumentException:找不到 LayoutRenderer:'aspnet-request-cookie'。不包括 NLog.Web 吗?
我最初的直觉是听从他们的建议,将 NLog.Web.AspNetCore 包添加到我的集成测试项目中。那没有任何作用。
这是我的集成测试设置的样子:
[OneTimeSetUp]
public static void BaseOneTimeSetUp()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
Assume.That(environment, Is.AnyOf("Development", "CI"), "Integration tests can only run in one of those environments");
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IHostingEnvironment>(p =>
{
var mock = new Mock<IHostingEnvironment>();
mock.SetupGet(m => m.ContentRootPath).Returns(TestContext.CurrentContext.TestDirectory);
mock.SetupGet(m => m.EnvironmentName).Returns(environment);
return mock.Object;
});
Startup.AddConfiguration(serviceCollection); //throws NLogConfigurationException
}
这是我对 Startup AddConfiguration 方法的实现:
public static void AddConfiguration(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(s =>
{
var env = s.GetService<IHostingEnvironment>();
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
var config = builder.Build();
// This next line throws the NLogConfigurationException
LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));
return config;
});
}
【问题讨论】:
标签: asp.net-core integration-testing moq nlog