Ang*_*ker 34 .net c# .net-core asp.net-core asp.net-core-2.2
我将我的项目升级到.NET Core 2.2.x并得到了关于以下代码的过时警告 - 两行:
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
Run Code Online (Sandbox Code Playgroud)
修复的建议是The recommended alternative is AddConsole(this ILoggingBuilder builder).我以为这就是我正在使用的东西.
我在这里错过了什么?
Mic*_*ber 70
我今天遇到了同样的问题.
从Startup.cs中删除您的日志记录配置,然后转到Program.cs文件并添加如下内容:
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.Build();
Run Code Online (Sandbox Code Playgroud)
这使用了'builder',因为变量'logging'是IloggingBuilder(而你的代码仍在使用ILoggerFactory)
更新:我刚尝试的另一种方法是保留在Startup.cs中,但将日志记录内容从"Configure"方法移动到"ConfigureServices",如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConfiguration(Configuration.GetSection("Logging"));
loggingBuilder.AddConsole();
loggingBuilder.AddDebug();
});
}
Run Code Online (Sandbox Code Playgroud)
或许可以减少Program.cs的污染......
meJ*_*rew 12
当我将日志代码从 .Net Core 2.1 更新到 3.0 时,我收到了同样的警告。MSDN上记录了推荐的升级方法。
就我而言,我试图为控制台获取 LoggerFactory 的实例,这在 .Net Core 3.0 中非常简单:
using (var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()))
{
// use loggerFactory
}
Run Code Online (Sandbox Code Playgroud)
该文档的建议使用AddConsole(this ILoggingBuilder builder)是正确的,但要使其正常工作,您需要添加对NuGet包的引用Microsoft.Extensions.Logging.Console。
不用担心-这是有史以来最愚蠢的事情!
注意
下面的代码示例使用2.2版中已过时的ConsoleLoggerProvider构造函数。3.0版中将提供适当的替代过时的日志记录API。同时,可以忽略并禁止显示警告。
万一您以为忘记了过时的意思-您没有!不用担心,现在就忽略它-或取消警告(对不起,我没有相应的代码)。
(希望他们对这样做的原因做出更好的解释-这就是我的意思。)
| 归档时间: |
|
| 查看次数: |
13962 次 |
| 最近记录: |