【问题标题】:.NET Core NLog logging before BuildWebHostBuildWebHost 之前的 .NET Core NLog 日志记录
【发布时间】:2017-11-20 10:04:55
【问题描述】:

我使用 NLog,并为我的应用程序创建了自己的日志提供程序。我在 StartUp 类中使用 NLog 设置了配置。

我将向您展示 Program.cs 现在的样子。

    public static void Main(string[] args)
    {
        var configurationDic = CreateConfigurationDictionary(); 
      // In here it can throw an exception therefore I need to be able to log here

        var webHost = BuildWebHost(configurationDic, args);

        var applicationLogger = webHost.Services.GetService<IMyTestLogProvider>().ApplicationLogger;

        applicationLogger.LogInformation("*************** Start up ***************");

        webHost.Run();
    }

除了我需要在主类中更早地登录之外,日志记录工作正常。在构建网络主机之前如何获取应用程序记录器的任何想法?

我已经看到了示例 https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2 ,我应该能够做到:

var logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();

但我的应用程序不知道 NLogBuilder 是什么,尽管我已经安装了 NLog nuget 包。

有什么想法吗?

最好的问候 抢

【问题讨论】:

  • 您究竟在做什么需要记录的网络主机并且不能延迟到主机实际初始化时?如果那里发生了很多事情,这可能表明您的 web 应用程序做的太多而与 web 应用程序无关。跨度>
  • 通知nuget.org/packages/NLog.Web.AspNetCore/4.5.0-rc1已经发布。它应该为 net461 启用 NLogBuilder。

标签: c# asp.net asp.net-mvc asp.net-core nlog


【解决方案1】:

但我的应用程序不知道 NLogBuilder 是什么,尽管我已经安装了 NLog nuget 包。

检查你的依赖,你需要:

  • NLog.Web.AspNetCore 4.5+(目前 beta04)
  • NLog 4.4.12+(例如 NLog 4.5 beta)- 没有 NLog 5! - 如果不使用 .NET 标准 1
  • 如果您使用的是 .NET 标准 1(不是 .NET 标准 2)- 使用 NLog 5+(当前为测试版)

【讨论】:

    【解决方案2】:

    由于细微的差别,Rolf 的回答不起作用。

    var logger = NLog.Web.LogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

    应该是

    var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

    public static void Main(string[] args)
    {
        // NLog: setup the logger first to catch all errors
        var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
        try
        {
            logger.Debug("init main");
            BuildWebHost(args).Run(); 
        }
        catch (Exception ex)
        {
            //NLog: catch setup errors
            logger.Error(ex, "Stopped program because of exception");
            throw;
        }
        finally
        {
             // Ensure to flush and stop internal timers/threads before application-exit(Avoid segmentation fault on Linux)
        NLog.LogManager.Shutdown();
    }
    

    }

    【讨论】:

      【解决方案3】:

      在 BuildWebHost(更新到 NLog 4.5.0 RTM 或更新版本)之前,请参阅更新的 Wiki 以了解如何激活日志记录

      public static void Main(string[] args)
      {
          // NLog: setup the logger first to catch all errors
          var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
          try
          {
              logger.Debug("init main");
              BuildWebHost(args).Run(); 
          }
          catch (Exception ex)
          {
              //NLog: catch setup errors
              logger.Error(ex, "Stopped program because of exception");
              throw;
          }
          finally
          {
              // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
              NLog.LogManager.Shutdown();
          }
      }
      

      https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2#4-update-programcs

      【讨论】:

      • 不起作用!正如原始海报所示“我的应用程序不知道 NLogBuilder 是什么”。我有 nlog 4.5.6,核心 2.0.3,即使这是一个 api/mvc 我也尝试添加 nlog.web.aspnetcore 4.5.4。
      • @ginalster 感谢您指出错误。更新了代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      相关资源
      最近更新 更多