c# - NLog 和单元测试

标签 c# visual-studio unit-testing nlog

我正在为我的记录器使用 NLog。当我运行 .exe 时记录时一切正常,甚至通过 Visual Studio 调试时,NLog 仍将写入文件。

但是,如果我运行一个通过单元测试调用记录器的对象,则会创建该文件,但它是空的。是否需要将额外的设置/规则添加到配置中,以便 NLog 在单元测试下写入文件?

我可以为此模拟 NLog 而没有日志转储,但在我决定只模拟 NLog 之前,我想看看我是否可以让它工作。尽管这只发生在单元测试中并且在其他情况下有效,但这是我的配置和日志记录代码。我省略了文件名。

public static void Info(string app, string m)  => EngineLogger.Logger.Info($"{app} : {m}");



<targets>
    <target name="infoFile"
            xsi:type="File"
            layout="${date:format=yyyy-MM-dd HH\:mm\:ss} ${pad:padding=5:inner=${level:uppercase=true}} ${logger} ${message}"
            fileName="leftoutForQuestion"
            keepFileOpen="false"
            encoding="iso-8859-2" />
    <target name="errorFile"
            xsi:type="File"
            layout="${date:format=yyyy-MM-dd HH\:mm\:ss} ${pad:padding=5:inner=${level:uppercase=true}} ${logger} ${message}"
            fileName="leftOutForQuestion"
            keepFileOpen="false"
            encoding="iso-8859-2" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" maxlevel="Info" writeTo="infoFile" />
    <logger name="*" minlevel="Warn" maxlevel="Fatal" writeTo="errorFile" />
  </rules>

这是来自内部日志的错误:

Error Error has been raised. Exception: System.Runtime.InteropServices.COMException (0x800700A1): The specified path is invalid. (Exception from HRESULT: 0x800700A1)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at NLog.Internal.FileAppenders.BaseFileAppender.WindowsCreateFile(String fileName, Boolean allowFileSharedWriting)
   at NLog.Internal.FileAppenders.BaseFileAppender.TryCreateFileStream(Boolean allowFileSharedWriting)
   at NLog.Internal.FileAppenders.BaseFileAppender.CreateFileStream(Boolean allowFileSharedWriting)
   at NLog.Internal.FileAppenders.RetryingMultiProcessFileAppender.Write(Byte[] bytes)
   at NLog.Targets.FileTarget.WriteToFile(String fileName, LogEventInfo logEvent, Byte[] bytes, Boolean justData)
   at NLog.Targets.FileTarget.ProcessLogEvent(LogEventInfo logEvent, String fileName, Byte[] bytesToWrite)
   at NLog.Targets.FileTarget.Write(LogEventInfo logEvent)
   at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)

最佳答案

我发现所有配置都位于单元测试项目的 app.config 中,而不是 NLog.Config 文件。请务必先声明配置部分,然后再声明 NLog 配置。下面是我的示例 app.config

    <configuration>
      <configSections>

         <section name="nlog"
                    type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>


      <target name="file" xsi:type="File"
             layout="${longdate} ${logger} ${message}"
             fileName="${basedir}/logs/${shortdate}.log" />

    </targets>
   <rules>
       <logger name="*" minlevel="Trace" writeTo="file"/>

    </rules>

  </nlog>
</configuration>

关于c# - NLog 和单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39476927/

相关文章:

c# - DataGridView 的替代品

c# - 在 Visual Studio 中崩溃后清空源文件?

visual-studio - 未安装项目应用程序

c++ - 尝试在 C++ 中为堆栈实现模板(但惨遭失败)

javascript - 我们可以模拟打印模式(媒体查询打印)进行单元测试吗?

c# - 使用继承自 IdentityDbContext 的上下文对 Entity Framework 6 进行单元测试

c# - 如何从查询 c# 中获取有限数量的输出

asp.net - Application Insights 将 ConnectedService.json 文件添加到我的项目中,这有什么作用?

angularjs - 如何在 Jasmine 测试中测试 $scope.$on 事件?

c# - 合并具有公共(public)元素的数组