在asp.net core 2.0 Web应用程序中使用NLog

问题描述 投票:6回答:1

哪个是在asp.net core 2.0 Web应用程序中使用Nlog的最佳方式

我找到了很多不同的解决方案如何配置。这是其中两个。还有其他更好的方法吗?

A)在启动服务器之前创建记录器:

 public class Program
{
    public static void Main(string[] args)
    {    
        // NLog: setup the logger first to catch all errors
        var logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();    
        try
        {
            logger.Debug("init main");
            BuildWebHost(args).Run();
        }
        catch (Exception e)
        {
            //NLog: catch setup errors
            logger.Error(e, "Stopped program because of exception");
            throw;
        }    
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>().UseNLog() // use NLog for DI Logger
            .Build();
}

B)配置内部启动

public class Startup
    {
        public Startup(IHostingEnvironment env, IConfiguration configuration)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();            
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {            
            services.AddMvc();                            
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddNLog();
            loggerFactory.ConfigureNLog("nlog.config");

            LogManager.Configuration.Variables["connectionString"] = Configuration.GetConnectionString("myDb");

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }
c# nlog asp.net-core-2.0
1个回答
4
投票

有关于此的维基文档:

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

要注入连接字符串之类的自定义数据,只需创建并注册自定义布局渲染器:

https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer

或者在启动时将连接字符串放入NLog-Global-Diagnostic-Context:

https://github.com/NLog/NLog/wiki/Var-Layout-Renderer

也许这样的地方NLog.config使用${gdc:connectionString}

var myConnectionString = Configuration.GetConnectionString("myDb");
NLog.GlobalDiagnosticsContext.Set("connectionString", myConnectionString);
var logFactory = NLogBuilder.ConfigureNLog("NLog.config"); // Uses ${gdc:connectionString}
var logger = logFactory.GetCurrentClassLogger();
logger.Info("Hello World");

另见https://github.com/NLog/NLog/wiki/Gdc-Layout-Renderer

更新 - $ {configsetting}

NLog.Extension.Logging ver。 1.4现在支持${configsetting},因此NLog可以直接从appsettings.json读取设置,而无需使用NLog变量。见https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer

© www.soinside.com 2019 - 2024. All rights reserved.