我无法从IIS托管的.net core 2.2应用程序获取Seq中的日志语句。当我调试并使用IISExpres运行应用程序时,将显示日志。但是,当应用程序从真实IIS的应用程序池中运行时,只有RequestLogging日志才会显示在Seq中。我想念我的日志。
这是我所做的:
已安装的Nuget软件包Serilog,Serilog.Aspnetcore和Serilog.Sinks.Seq。
已删除所有Microsoft.Extensions.Logging和引用。
遵循Serilog.Aspnetcore Github页面上的示例,在Program.cs中实现了内联配置:
public class Program
{
...
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog((hostingContext, loggerConfiguration) =>
loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
, preserveStaticLogger: true, writeToProviders: false);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
....
app.UseHttpsRedirection();
app.UseSerilogRequestLogging();
app.UseMvc();
}
{
"Serilog": {
"Using": [ "Serilog.Sinks.Seq" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext" ],
"WriteTo": [
{
"Name": "Seq",
"Args": {
"apiKey": "iVPcw1...",
"serverUrl": "http://localhost:5341"
}
}
]
},
...
}
public class SomeService : ISomeService
{
private readonly ILogger logger;
...
public SomeService( ...injections...) {
logger = Log.ForContext<SomeService>();
...
}
...
public async Task ServiceMethod(Data d)
{
logger.Information("ServiceMethod called with {@d}");
}
...
}
您在UseSerilog()
的参数中:
preserveStaticLogger: true
这指示UseSerilog()
不要设置/修改静态Log.Logger
实例,这意味着您在以下情况下使用它:
logger = Log.ForContext<SomeService>();
您将获得默认的,未配置的,不执行任何操作的记录器。
设置preserveStaticLogger: false
应解决此问题。