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

时间:2017-11-20 10:04:56

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

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

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

private void button1_Click(object sender, EventArgs e)
{
    new Task(() => RunCommand()).Start();
}

private void RunCommand()
{
    var host = "hostname";
    var username = "username";
    var password = "password";

    using (var client = new SshClient(host, username, password))
    {
        client.Connect();
        // If the command2 depend on an environment modified by command1,
        // execute them like this.
        // If not, use separate CreateCommand calls.
        var cmd = client.CreateCommand("command1; command2");

        var result = cmd.BeginExecute();

        using (var reader =
                   new StreamReader(cmd.OutputStream, Encoding.UTF8, true, 1024, true))
        {
            while (!result.IsCompleted || !reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (line != null)
                {
                    textBox1.Invoke(
                        (MethodInvoker)(() =>
                            textBox1.AppendText(line + Environment.NewLine)));
                }
            }
        }

        cmd.EndExecute(result);
    }
}

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

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

    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();
    }

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

有什么想法吗?

祝你好运 罗布

3 个答案:

答案 0 :(得分:0)

  

但我的应用程序不知道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+(目前为测试版)

答案 1 :(得分:0)

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

jdbc:h2:~/PATH_TO_DATABASE

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

答案 2 :(得分:0)

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

  

var logger = NLog.Web.LogBuilder.ConfigureNLog(&#34; nlog.config&#34;)。GetCurrentClassLogger();

应该是

  

var logger = NLog.Web.NLogBuilder.ConfigureNLog(&#34; nlog.config&#34;)。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();
}

}