[英]Using NLog with MSTest (.NET Core 2.0)
我有使用.NET 4.7构建的解决方案。 该解决方案有两个项目:
我正在尝试将此解决方案迁移为使用.NET Standard。 核心。
我已经成功地将类库转移到了.NET Standard 2.0项目中。 我还将单元测试项目转移到了.NET Core 2.0 mstest项目中 。 一切都会编译。 我可以按预期运行测试。 但是,没有日志通过NLog写入。
在我的解决方案的.NET 4.7版本中,当我运行单元测试时,实际的日志文件是通过NLog编写的。 但是,在新的.NET Standard中| 核心实现,此日志文件未写入。 我的mstest项目中有以下两个文件:
App.config中
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="c:\temp\console-example-internal.log"
internalLogLevel="Info" >
<targets>
<target name="myLogs" xsi:type="File" deleteOldFileOnStartup="true" fileName="${basedir}/logs/MyLogs.json" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="${message}" />
<target name="traceLogs" xsi:type="File" deleteOldFileOnStartup="true" fileName="${basedir}/logs/trace.log" createDirs="true" keepFileOpen="true" encoding="utf-8" layout="[${longdate}] ${message}${exception:format=ToString}"></target>
</targets>
<rules>
<logger name="MyLogger" minlevel="Info" writeTo="myLogs" />
<logger name="TraceLogger" minlevel="Trace" writeTo="traceLogs" />
</rules>
</nlog>
在我的mstest项目中,我还添加了对以下内容的引用:
我按照此处概述的步骤进行操作。 除此之外,我没有执行第2步之后的任何操作。我是否需要mstest项目中的东西? 如果是这样,在哪里? 对于已经起作用的东西,似乎还有很多其他东西。
在运行单元测试时,很难找到nlog.config,因为实现会将DLL(大多数情况下不是nlog.config)移动到临时文件夹。
建议从单元测试项目中的代码配置NLog。 ( 如何操作 )
另一种选择是手动加载NLog配置文件。 您需要提供的路径,nlog.config到XmlLoggingConfiguration
构造器:
LogManager.Configuration = new XmlLoggingConfiguration(pathToNLogConfig);
离题,但相关:
在单元测试中,建议不要将事件日志写入文件中,而是写入内存中,例如Memory target 。 它使单元测试更加强大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.