Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NLog未记录调试消息_C#_Logging_.net Core_Nlog - Fatal编程技术网

C# NLog未记录调试消息

C# NLog未记录调试消息,c#,logging,.net-core,nlog,C#,Logging,.net Core,Nlog,我在.NET Core 3.1辅助服务项目中记录调试级别消息时遇到问题。我的文件目标或控制台均未记录调试级别的消息。信息级事件按预期编写。我已经检查了一个,以确保所有这些框都已选中 NLog.config <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001

我在.NET Core 3.1辅助服务项目中记录调试级别消息时遇到问题。我的文件目标或控制台均未记录调试级别的消息。信息级事件按预期编写。我已经检查了一个,以确保所有这些框都已选中

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="%appdata%\FileTransferService\nlog-internal.log">

  <variable name="logdir" value="${specialfolder:folder=ApplicationData}/FileTransferService"/>
  <variable name="stdlayout" value="${longdate}|${level:uppercase=true}|${message:exceptionSeparator=|}${exception:format=ToString}"/>

  <targets>  
    <target name="default" xsi:type="AsyncWrapper" overflowAction="Block">
      <target name="defaultlog" xsi:type="File"
              fileName="${logdir}/dftslog.txt"
              layout="${stdlayout}"
              archiveEvery="Month" concurrentWrites="false"/>
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="default"/>
  </rules>
</nlog>
我尝试将Microsoft和Microsoft.Hosting.Lifetime都设置为调试,但没有效果

Program.CreateHostBuilder方法

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseWindowsService()
        .ConfigureLogging((context, logBuilder) => {
            logBuilder.ClearProviders();
            logBuilder.AddConfiguration(context.Configuration.GetSection("Logging"));
            logBuilder.SetMinimumLevel(LogLevel.Debug);
            logBuilder.AddConsole();
            logBuilder.AddNLog();
        })
        .ConfigureServices((hostContext, services) => {
            services.AddHostedService<Worker>();
            services.AddTransient(typeof(ITransferService), typeof(TransferService));
        });
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureLogging((上下文,logBuilder)=>{
logBuilder.ClearProviders();
logBuilder.AddConfiguration(context.Configuration.GetSection(“日志”);
logBuilder.SetMinimumLevel(LogLevel.Debug);
logBuilder.AddConsole();
logBuilder.AddNLog();
})
.ConfigureServices((主机上下文,服务)=>{
services.AddHostedService();
服务。AddTransient(类型为(ITransferService),类型为(TransferService));
});
以下是一些补充说明:

  • 我尝试调用SetMinimumLevel,但没有效果
  • NLog.config设置为始终复制
  • 我在没有AddConsole的情况下得到相同的结果。这实际上是在我看到调试消息没有被记录之后添加的

    • 问题似乎出在Visual Studio 2019上

      尽管问题本身仍然存在,但当我在VisualStudio之外运行应用程序时,调试级别的事件会同时写入控制台和平面文件。这不是由于调试器造成的,因为如果我在VisualStudio中没有调试器的情况下运行调试器,它会表现出相同的行为


      我查看了VisualStudio设置并进行了快速搜索,但找不到任何相关内容。这就是说,知道这些事件将在Visual Studio之外记录可以在必要的程度上解决我的问题。

      问题似乎出在Visual Studio 2019上

      尽管问题本身仍然存在,但当我在VisualStudio之外运行应用程序时,调试级别的事件会同时写入控制台和平面文件。这不是由于调试器造成的,因为如果我在VisualStudio中没有调试器的情况下运行调试器,它会表现出相同的行为


      我查看了VisualStudio设置并进行了快速搜索,但找不到任何相关内容。也就是说,知道这些事件将在Visual Studio之外记录可以在必要的程度上解决我的问题。

      要添加到已接受的答案中(没有足够的分数进行评论):

      同样的问题。但当我使用VS2019时,它也可以在VS2019中正常工作

      private static Logger _logger = LogManager.GetCurrentClassLogger();
      
      与DI相比,我更喜欢使用它来获取日志(主要是因为我喜欢编写.Warn(…)而不是.LogWarning(…)


      我仍然为NLog设置了DI,因此外部/ms库将使用它添加到可接受的答案中(没有足够的评论点数):

      同样的问题。但当我使用VS2019时,它也能在VS2019中正常工作

      private static Logger _logger = LogManager.GetCurrentClassLogger();
      
      与DI相比,我更喜欢使用它来获取日志(主要是因为我喜欢编写.Warn(…)而不是.LogWarning(…)


      我仍然为NLog设置DI,因此当将NLog与Microsoft扩展日志(MEL)一起使用时,外部/ms库将使用它,然后NLog将成为MEL日志工厂中的日志提供商

      这意味着在MEL LoggerFactory中配置的任何筛选都将始终获胜,与NLog.config无关

      MEL LoggerFactory将自动从
      appsettings.json
      (以及任何特定于环境的
      appsettings.development.json
      appsettings.production.json
      )加载其筛选

      即使调用了
      SetMinimumLevel(LogLevel.Debug)
      ,那么
      appsettings.json
      中的任何配置都将覆盖此设置


      另请参见:

      将NLog与Microsoft扩展日志(MEL)一起使用时,NLog将成为MEL日志工厂中的日志提供程序

      这意味着在MEL LoggerFactory中配置的任何筛选都将始终获胜,与NLog.config无关

      MEL LoggerFactory将自动从
      appsettings.json
      (以及任何特定于环境的
      appsettings.development.json
      appsettings.production.json
      )加载其筛选

      即使调用了
      SetMinimumLevel(LogLevel.Debug)
      ,那么
      appsettings.json
      中的任何配置都将覆盖此设置


      另请参见:

      此示例应用程序在日志记录以调试和跟踪方面没有问题:不幸的是,存储库的内容没有解决任何问题。我将在后续评论中详细介绍我尝试的内容。尽管存储库是Rolf K.提供的.NET Core 2,但我认为它可能仍然有帮助。我立即注意到他能够在生成器中使用UseNLog而不是AddNLog。这是因为我添加了包NLog.Extensions.Logging而不是NLog.Extensions.Hosting。我还简化了配置,所以它所做的只是调用SetMinimumLevel。问题仍然存在。接下来,我完全删除了appsettings.json的内容。这并没有解决问题问题也是。我还添加了对LogManager的调用。在finally块中关闭,但我不认为这是实质性的。可能您可以在此处创建一个新问题,并附加无法记录日志的示例应用程序的源代码。可能解决方案在此答案中?此示例应用程序在记录调试和跟踪方面没有问题:不幸的是,存储库的内容没有解决任何问题。我将在后续评论中详细介绍我尝试的内容。虽然存储库是Rolf K.提供的.NET Core 2,但我认为它可能仍然有用。我立即注意到他能够在生成器中使用UseNLog而不是AddNLog。结果是beca使用我的ADD