【问题标题】:Show NLog output in debug window of asp.net core app在 asp.net 核心应用程序的调试窗口中显示 NLog 输出
【发布时间】:2019-07-09 03:23:52
【问题描述】:

是否可以在 Visual Studio 2017 调试窗口中显示 NLog(或内置调试器)正在记录什么?

我已将 NLog 设置为输出到文件,但对于开发而言,能够在调试窗口中查看调试消息会非常方便。我可以看到有关如何使用控制台执行此操作的文章,但对于 asp.net 项目,没有任何控制台输出,只有调试窗口。

【问题讨论】:

    标签: asp.net-core visual-studio-2017 nlog


    【解决方案1】:

    简单的解决方案就是使用OutputDebugString-target(NetCore 支持)

    https://github.com/NLog/NLog/wiki/OutputDebugString-target

    例子:

    <targets>
      <target name="debugger" xsi:type="OutputDebugString" layout="${logger}::${message}"/>
    </targets>
    
    <rules>
      <logger name="*" minlevel="Trace" writeTo="debugger" />
    </rules>
    

    另一种可以使用xsi:type="debugger":

    https://github.com/NLog/NLog/wiki/Debugger-target

    【讨论】:

      【解决方案2】:

      是的,对于 Asp.Net Core,有内置的记录器提供程序,如 ConsoleDebug 将日志写入 Output Window

      如果您使用WebHost.CreateDefaultBuilder(args),它将使用内置提供程序ConsoleDebug,您可以通过Output window->Asp.NET Core Web Server查看输出一个干净的结果。

      对于来自NLogGetting started with ASP.NET Core 2,它使用下面的代码清除所有其他日志提供程序。

      public static IWebHost BuildWebHost(string[] args) =>
      WebHost.CreateDefaultBuilder(args)
          .UseStartup<Startup>()
          .ConfigureLogging(logging =>
          {
              logging.ClearProviders();
              logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
          })
          .UseNLog()  // NLog: setup NLog for Dependency injection
          .Build();
      

      如果你还需要登录调试窗口,你可以修改如下代码:

              public static IWebHost BuildWebHost(string[] args) =>
              WebHost.CreateDefaultBuilder(args)
                  .UseStartup<Startup>()
                  .ConfigureLogging(logger => {
                      logger.AddNLog();
                      //logger.AddConsole(); //UnComment out this line if you did not use CreateDefaultBuilder
                  })
                  .Build();
      

      【讨论】:

        猜你喜欢
        • 2021-09-20
        • 2016-08-31
        • 2021-07-03
        • 2012-10-18
        • 2019-10-14
        • 1970-01-01
        • 2010-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多