BuildWebHost之前的.NET Core NLog日志记录

[英].NET Core NLog logging before BuildWebHost


I use NLog and I have created my own log provider for my application. I set the configuration up with NLog in the StartUp class.

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

I will show you how the Program.cs look right now.

我将向您展示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();
    }

The logging works fine except that I need to log earlier in the main class. Any ideas how I get my application logger before building the web host?

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

I have seen example, https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2 , where I should be able to do:

我见过示例,https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2,我应该可以这样做:

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

But my application don't know what NLogBuilder is though I have the NLog nuget packages installed.

但我的应用程序不知道NLogBuilder是什么,虽然我安装了NLog nuget包。

Any ideas?

Best regards Rob

最好的问候Rob

3 个解决方案

#1


0  

But my application don't know what NLogBuilder is though I have the NLog nuget packages installed.

但我的应用程序不知道NLogBuilder是什么,虽然我安装了NLog nuget包。

Check your dependencies, you need:

检查您的依赖项,您需要:

  • NLog.Web.AspNetCore 4.5+ (currently beta04)
  • NLog.Web.AspNetCore 4.5+(目前为beta04)

  • NLog 4.4.12+ (e.g. NLog 4.5 beta) - no NLog 5! - if not using .NET standard 1
  • NLog 4.4.12+(例如NLog 4.5 beta) - 没有NLog 5! - 如果不使用.NET标准1

  • If you're using .NET standard 1 (not .NET standard 2) - use NLog 5+ (currently beta)
  • 如果您使用的是.NET标准1(非.NET标准2) - 请使用NLog 5+(目前为测试版)

#2


0  

Rolf's answer does not work because of a subtle difference.

由于微妙的差异,罗尔夫的答案不起作用。

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

var logger = NLog.Web.LogBuilder.ConfigureNLog(“nlog.config”)。GetCurrentClassLogger();

should be

var logger = NLog.Web.NLogBuilder.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


0  

See updated Wiki for how to activate logging, before BuildWebHost (Update to NLog 4.5.0 RTM or newer)

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

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


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2017/11/20/8a89f5afb797b7a8e1bd5a1fa29961e6.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号