简体   繁体   English

将NLog与MSTest(.NET Core 2.0)结合使用

[英]Using NLog with MSTest (.NET Core 2.0)

I have a solution I built using .NET 4.7. 我有使用.NET 4.7构建的解决方案。 This solution has two projects: 该解决方案有两个项目:

  • A class library (.NET 4.7.2) 一个类库(.NET 4.7.2)
  • A Unit Test Project (.NET Framework) 单元测试项目(.NET Framework)

I'm trying to migrate this solution to use .NET Standard | 我正在尝试将此解决方案迁移为使用.NET Standard。 Core. 核心。

I have successfully transferred the class library to a .NET Standard 2.0 project. 我已经成功地将类库转移到了.NET Standard 2.0项目中。 I have also transferred the Unit Test Project to a .NET Core 2.0 mstest project . 我还将单元测试项目转移到了.NET Core 2.0 mstest项目中 Everything compiles. 一切都会编译。 I can run my tests as expected. 我可以按预期运行测试。 However, no logs are getting written via NLog. 但是,没有日志通过NLog写入。

In my .NET 4.7 version of the solution, when I ran the unit tests, an actual log file was written via NLog. 在我的解决方案的.NET 4.7版本中,当我运行单元测试时,实际的日志文件是通过NLog编写的。 However, in the new .NET Standard | 但是,在新的.NET Standard中| Core implementation, this log file is not getting written. 核心实现,此日志文件未写入。 I have the following two files in my mstest project: 我的mstest项目中有以下两个文件:

App.config App.config中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>

nlog.config 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>

In my mstest project, I've also added references to: 在我的mstest项目中,我还添加了对以下内容的引用:

  • Microsoft.Extensions.DependencyInjection Microsoft.Extensions.DependencyInjection
  • NLog NLOG
  • NLog.Extensions.Logging NLog.Extensions.Logging

I followed the steps outlined here . 我按照此处概述的步骤进行操作。 Except, I didn't do anything beyond step 2. Do I need that stuff for a mstest project? 除此之外,我没有执行第2步之后的任何操作。我是否需要mstest项目中的东西? If so, where? 如果是这样,在哪里? It seems like a lot of additional stuff for something that was already working. 对于已经起作用的东西,似乎还有很多其他东西。

When running unit tests, it's hard to find the nlog.config as implementations will move the DLLs (and most of the time not the nlog.config) to temporary folders. 在运行单元测试时,很难找到nlog.config,因为实现会将DLL(大多数情况下不是nlog.config)移动到临时文件夹。

It's recommend to configure NLog from code in an unit test project. 建议从单元测试项目中的代码配置NLog。 ( how-to here ) 如何操作

Another option is to load the NLog config file manually. 另一种选择是手动加载NLog配置文件。 You need to provide the path to nlog.config to the XmlLoggingConfiguration contructor: 您需要提供的路径,nlog.config到XmlLoggingConfiguration构造器:

LogManager.Configuration = new XmlLoggingConfiguration(pathToNLogConfig);

Off topic but related: 离题,但相关:

In the unit test it's recommend to write the eventlogs not to a file, but in memory, eg the Memory target . 在单元测试中,建议不要将事件日志写入文件中,而是写入内存中,例如Memory target It makes the unit test more robust. 它使单元测试更加强大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM