把旧系统迁移到.Net Core 2.0 日记(2) - 依赖注入/日志NLog
Net Core 大量使用依賴注入(Dependency Inject), 打個比方,我們常用的日志組件有Log4Net,NLog等等.
如果我們要隨時替換日志組件,那么代碼中就不能直接引用某個組件的內容,也不能直接New 某個組件.
而是應該定義一組接口, 然后包裝各個組件,實現這個接口. Net Core 自帶組件容器, 啟動程序時,指定接口對應的實現.
然后在各個頁面/Controller 里, 通過構造函數的參數,把要帶過去的接口,把容器里的對象自動傳過去( 我還不知道是怎么實現的)
各個頁面/Controller ,再調用接口的方法. 所謂的面向接口編程.
Net Core 在Microsoft.Extension.Logging定義了ILogger,ILoggerFactory,ILoggerProvider 這幾個接口, 日志組件實現這個接口就可以被調用了.
上網查了一下,NLog已經實現了這些接口了,你用Nuget找NLog.Web.AspNetCore 就已經包裝好了. 具體文檔,請看這篇文章,非常詳細.
https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2
我們來看一下代碼, 要調用NLog, 在Program.cs里
public static IWebHost BuildWebHost(string[] args) =>
? ? ? ? ? ? WebHost.CreateDefaultBuilder(args)
? ? ? ? ? ? ? ? .UseStartup<Startup>()
? ? ? ? ? ? ? ? ? ?.ConfigureLogging((hostingContext, logging) =>
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?logging.AddConsole();
? ? ? ? ? ? ? ? ? ? ? ?logging.AddDebug();
? ? ? ? ? ? ? ? ? ?})
? ? ? ? ? ? ? ? ? ?.UseNLog()
? ? ? ? ? ? ? ? .Build();
? ? }
調用NLog寫Log
public class HelloMiddleware
? ? {
? ? ? ? private readonly ILogger<HelloMiddleware> _logger;
? ? ? ? private readonly RequestDelegate _next;
? ? ? ? public HelloMiddleware(RequestDelegate next,ILogger<HelloMiddleware> logger)
? ? ? ? {
? ? ? ? ? ? _next = next;
? ? ? ? ? ? _logger = logger;
? ? ? ? }
? ? ? ? public Task Invoke(HttpContext httpContext)
? ? ? ? {
? ? ? ? ? ? httpContext.Response.WriteAsync("Hello in Class Invoke"+ Environment.NewLine);
? ? ? ? ? ? _logger.LogInformation("******hello middleware*********");
? ? ? ? ? ? return _next(httpContext);
? ? ? ? }
? ? }
我們來看看UseNLog()這個方法的代碼,看看做了什么
/// <summary>
? ? ? ? /// Use NLog for Dependency Injected loggers.?
? ? ? ? /// </summary>
? ? ? ? public static IWebHostBuilder UseNLog(this IWebHostBuilder builder)
? ? ? ? {
? ? ? ? ? ? return UseNLog(builder, null);
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// Use NLog for Dependency Injected loggers.?
? ? ? ? /// </summary>
? ? ? ? /// <param name="builder"></param>
? ? ? ? /// <param name="options">Options for logging to NLog with Dependency Injected loggers</param>
? ? ? ? /// <returns></returns>
? ? ? ? public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCoreOptions options)
? ? ? ? {
? ? ? ? ? ? if (builder == null) throw new ArgumentNullException(nameof(builder));
? ? ? ? ? ? options = options ?? NLogAspNetCoreOptions.Default;
? ? ? ? ? ? builder.ConfigureServices(services =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //note: when registering ILoggerFactory, all non NLog stuff and stuff before this will be removed
? ? ? ? ? ? ? ? services.AddSingleton<ILoggerProvider>(serviceProvider =>
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ServiceLocator.ServiceProvider = serviceProvider;
? ? ? ? ? ? ? ? ? ? return new NLogLoggerProvider(options);
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? //note: this one is called before? services.AddSingleton<ILoggerFactory>
? ? ? ? ? ? ? ? if (options.RegisterHttpContextAccessor)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? RegisterHiddenAssembliesForCallSite();
? ? ? ? ? ? });
? ? ? ? ? ? return builder;
? ? ? ? }
而Log4Net 就沒有官方的包裝,只有一個第三方包裝的, 但包裝的不好, 日志的配置不是放在Program.cs, 而是放在StartUp.cs. 我打算有時間再重新包裝一次.
參考這篇文章
http://www.cnblogs.com/drivenwinder/p/8300881.html
相關文章:
.NET Core 2.0使用NLog
ASP.NET Core 2.0 依賴注入
依賴注入和控制反轉
全面理解 ASP.NET Core 依賴注入
原文地址 http://www.cnblogs.com/zitjubiz/p/net_core_daily_2.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
以上是生活随笔為你收集整理的把旧系统迁移到.Net Core 2.0 日记(2) - 依赖注入/日志NLog的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用C# (.NET Core) 实现命
- 下一篇: 开源纯C#工控网关+组态软件(九)定制V