尝试遵循本教程:https://nblumhardt.com/2019/10/serilog-in-aspnetcore-3/
我设法将 Serilog 添加到我的应用程序(.NET Core 3.1)
但我无法修改 CreateHostBuilder 函数,因为它无法识别 UseSerilog() 函数
我已经检查了解决方案,但每个人似乎都使用该代码
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
namespace MyApi
{
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting up");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application start-up failed");
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< unknown function
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
我在这里缺少什么?
感谢您的帮助
Alex Lyalka 提供的答案:包裹丢失
dotnet add package Serilog.AspNetCore
你必须使用 Serilog.Extensions.Hosting 包。
我已升级到.NET 8。还升级了 Serilog.AspNetCore(8.0.3) Serilog.Extensions.Hosting (8.0.0) 但我收到了上述错误。
'IWebHostBuilder' does not contain a definition for 'UseSerilog'
我的代码: “ webBuilder.UseStartup() .UseConfiguration(配置) .UseSerilog();"