C# Serilog文件接收器未记录到文件
我正在尝试让Serilog文件接收器在基于的ASP.NETCore2.2应用程序上工作。我无法在应用程序中查看日志。 我错过了什么 Program.cs:C# Serilog文件接收器未记录到文件,c#,.net,asp.net-core,serilog,C#,.net,Asp.net Core,Serilog,我正在尝试让Serilog文件接收器在基于的ASP.NETCore2.2应用程序上工作。我无法在应用程序中查看日志。 我错过了什么 Program.cs: using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Serilog; using System; namespace Scrubber { public class
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;
namespace Scrubber
{
public class Program
{
private static string _environmentName;
public static void Main(string[] args)
{
try
{
var iWebHost = CreateWebHostBuilder(args).Build();
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{_environmentName}.json", optional: true, reloadOnChange: true)
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration.GetSection("Serilog"))
.CreateLogger();
Log.Logger = logger;
Log.Information("Application starting");
iWebHost.Run();
}
catch(Exception exception)
{
Log.Error(exception.ToString());
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, config) =>
{
//config.ClearProviders();
_environmentName = hostingContext.HostingEnvironment.EnvironmentName;
})
.UseStartup<Startup>();
}
}
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Serilog": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log.txt",
"rollingInterval": "Day"
}
}
]
}
}
一个可能的原因是应用程序根本没有加载配置 注意:您可以通过以下方式设置配置:
ConfigurationBuilder
optional:false
// get the real path
// or by reflection
// or by injection,
var path = Directory.GetCurrentDirectory(); // assume the current directory
var configuration = new ConfigurationBuilder()
.SetBasePath(path) // set the right path
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // make it required
.AddJsonFile($"appsettings.{_environmentName}.json", optional: true, reloadOnChange: true)
.Build();
希望有帮助。一个可能的原因是应用程序根本没有加载配置 注意:您可以通过以下方式设置配置:
ConfigurationBuilder
optional:false
// get the real path
// or by reflection
// or by injection,
var path = Directory.GetCurrentDirectory(); // assume the current directory
var configuration = new ConfigurationBuilder()
.SetBasePath(path) // set the right path
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // make it required
.AddJsonFile($"appsettings.{_environmentName}.json", optional: true, reloadOnChange: true)
.Build();
希望能有所帮助。我也在为此写答案
SetBasePath
当然是个问题,但还有第二个问题:它应该是从.Configuration(Configuration)读取的,因为Serilog配置加载程序已经假定了“Serilog”
的一部分,这意味着它找不到任何值。如果您想将其折叠到您的答案中,我将删除我的注释。:)@柯克拉金非常感谢。但我认为最好在这里留下你的精彩评论,因为这表明是你帮助了我们。谢谢柯克·拉金,@itminus,你建议的更改使Program.cs中的应用程序日志工作正常。但是,应用程序没有将Microsoft.Extensions.Logging.ILogger输出重定向到Serilog.Log。我需要在ConfigureServices中注册相同的日志吗?通过添加ILOGingBuilder.AddSerilog(),我可以在整个应用程序中运行日志记录;公共静态IWebHostBuilder CreateWebHostBuilder中的行(字符串[]args)=>WebHost.CreateDefaultBuilder(args).ConfigureLogging((webHostBuilderContext,ILOGingBuilder)=>{ILOGingBuilder.AddSerilog();_environmentName=webHostBuilderContext.HostingEnvironment.environmentName;});我也在写一个答案SetBasePath
当然是个问题,但还有第二个问题:它应该是从.Configuration(Configuration)读取的,因为Serilog配置加载程序已经假定了“Serilog”
的一部分,这意味着它找不到任何值。如果您想将其折叠到您的答案中,我将删除我的注释。:)@柯克拉金非常感谢。但我认为最好在这里留下你的精彩评论,因为这表明是你帮助了我们。谢谢柯克·拉金,@itminus,你建议的更改使Program.cs中的应用程序日志工作正常。但是,应用程序没有将Microsoft.Extensions.Logging.ILogger输出重定向到Serilog.Log。我需要在ConfigureServices中注册相同的日志吗?通过添加ILOGingBuilder.AddSerilog(),我可以在整个应用程序中运行日志记录;公共静态IWebHostBuilder CreateWebHostBuilder中的行(字符串[]args)=>WebHost.CreateDefaultBuilder(args).ConfigureLogging((webHostBuilderContext,ILOGingBuilder)=>{ILOGingBuilder.AddSerilog();_environmentName=webHostBuilderContext.HostingEnvironment.environmentName;});