[尝试在此处关注文档https://github.com/serilog/serilog-extensions-hosting
在我的项目中引用了这些软件包
<PackageReference Include="Serilog" Version="2.8.0" /> <PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
[当我尝试添加UseSeriLog行时,无法识别它
错误CS1061'IHostBuilder'不包含针对“ UseSeriLog”,并且不接受任何可访问的扩展方法“ UseSeriLog”可以找到类型为“ IHostBuilder”的第一个参数(您是缺少using指令或程序集引用?)
我的主要方法:
private static async Task Main(string[] args)
{
var isService = !(Debugger.IsAttached || args.Contains("--console"));
Log.Logger = new LoggerConfiguration()
.WriteTo.File(@"d:\temp\consoleapp.log")
.CreateLogger();
var hostBuilder = new HostBuilder()
.ConfigureHostConfiguration(configHost => { configHost.AddCommandLine(args); })
.UseContentRoot(@"D:\Services\BldgRunner\")
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddCommandLine(args);
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
config.AddEnvironmentVariables();
})
.ConfigureServices((hostContext, services) =>
{
//Inject additional services as needed
services.AddHostedService<JobRunner>();
})
**.UseSeriLog()**
.Build();
if (isService)
{
await hostBuilder.RunAsServiceAsync();
}
else
{
await hostBuilder.RunConsoleAsync();
}
}
您需要安装Serilog.Extensions.Hosting
以与通用主机一起使用。
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.0.0" />