NLog NETCore 3.0 Porting
NLog NETCore 3.0 Porting
這里簡單整理一下,NETCore 3.0 + NLog工程移植過程中遇到的問題。
Configuration
以下代碼才能編譯通過using Microsoft.Extensions.Hosting;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } }使用 IWebHostEnvironment 替換原來的 IHostEnvironment
NLog 對應(yīng)
NLog簡介
比較的詳細(xì)的可以參考:
https://blog.csdn.net/sd7o95o/article/details/81350638
StartUp
NetCore新版本需要使用新的ILoggingBuilder對Log進(jìn)行構(gòu)建,3.0以前是從ILoggerFactory開始構(gòu)建。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) namespace Microsoft.Extensions.Logging{ // // An interface for configuring logging providers. public interface ILoggingBuilder { // // Gets the Microsoft.Extensions.DependencyInjection.IServiceCollection where Logging // services are configured. IServiceCollection Services { get; } }}[Obsolete("Instead use ILoggingBuilder.AddNLog() or IHostBuilder.UseNLog()")]public static ILoggerFactory AddNLog(this ILoggerFactory factory);其中可以在
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging((ILoggingBuilder logBuilder) => { logBuilder.AddNLog(); logBuilder.AddConsole(); //logBuilder.confi NLog.LogManager.LoadConfiguration("mynlog.config"); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseUrls("http://*:5012"); webBuilder.UseStartup<Startup>(); });可以看出,這里的Host是通用的了,不一定只用于WebApp,可以用到Console App的開發(fā)中。
可以看到Log的配置文件應(yīng)該是沒有變化的,在Bin目錄下也可以到看如下的Log文件輸出
過時的IHostingEnvironment與IApplicationLifetime對象從.NET Core 3.0開始,IHostingEnvironment與IApplicationLifetime已被標(biāo)記為“過時(Obsolete)”,這意味著在后續(xù)的.NET版本中,將不再繼續(xù)支持這兩個接口。如果我們在Startup的Configure方法中使用了這兩個對象,那么最好也將這兩個接口分別替換為:IWebHostEnvironment和IHostApplicationLifetime。
Console App
支持3.0之后,通過以下構(gòu)建一個LoggerFactory 用于非WebApp的Log輸出。之前的方法在3.0中,沒有提供了AddConsole的定義了
var myfactory = new LoggerFactory(); myfactory.AddConsole(); myfactory.AddNLog();分析IWebHostBuilder 與IHostBuilder的關(guān)系,兩者沒有繼承關(guān)系。兩者還有一定的相似之處,前者的保留更多的是為了兼容NetCore2.0 的構(gòu)建過程。
這里的Build是否應(yīng)該會刪除了呢?還是在內(nèi)部還會調(diào)用?
可以參考WebHost,寫一個針對Console App的構(gòu)建封裝。
可以看到在Host Build的過程中已經(jīng)加入了很多的Service,如下有Config、Lifetime、IOptions、Logger等其中,比較重要是IHost也能獲取到。
如下代碼可以看到Log的配置過程,以及Service配置的地方,注意在這里是獲取不到對應(yīng)的Service的實(shí)例的。這里還沒有進(jìn)行構(gòu)建。要Build之后才能獲取到。
Main函數(shù)里的代碼,Build后注冊一ApplicationStarted 消息,對應(yīng)也有Stopped的消息。這里可以封裝起來。
static void Main(string[] args) {CancellationToken cancellationToken = new CancellationToken();cancellationToken.Register(() => { Console.WriteLine("App Stopped"); });var dd = CreateHostBuilder(args).Build();var lifeTime = dd.Services.GetService(typeof(IHostApplicationLifetime)) as IHostApplicationLifetime; lifeTime.ApplicationStarted.Register(() => {Task.Run(() =>{console.WriteLine("Hello World! In a Task"); }); });dd.StartAsync(cancellationToken);Console.WriteLine("Hello World!"); }NLog 配置文件(無變化)
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Warn" internalLogFile="internal-nlog.txt"> <!--define various log targets--> <targets> <!--write logs to file--> <target xsi:type="File" name="allfile" fileName="nlog-all-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /><target xsi:type="File" name="ownFile-web" fileName="nlog-my-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="Null" name="blackhole" /> </targets> <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /><!--Skip Microsoft logs and so log only own logs--> <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> </rules> </nlog>參考
https://www.cnblogs.com/runningsmallguo/p/11617165.html
總結(jié)
以上是生活随笔為你收集整理的NLog NETCore 3.0 Porting的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PE新物种:从投基金到投管理机构,详解G
- 下一篇: CodeLite开发Gtk 语法检查报错