c# - Serilog ASP .Net Core 不在 MacOS 中将日志写入文件但在 Windows 中工作
问题描述
我已经在我的 asp .net core 3.1 项目中配置了 serilog。它在 Windows 中运行良好,但是当我在 Mac 上运行它时,我没有创建任何日志文件。所以我通过写入控制台进行了检查,它运行良好,但仅在 mac 中,在 windows 中写入文件也不能正常工作两者都工作正常。
启动.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(new CompactJsonFormatter(), "Logs\\log.json", rollingInterval: RollingInterval.Minute,shared:true,
restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
}
程序.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
在我使用的任何一个类中,我都在创建一个通用的记录器实例,并且使用的是这样的
public AuthService(ILogger<AuthService> logger)
{
_logger = logger;
}
and in
some function(){
_logger.LogInformation("Got Access Token, Now returning!");
_logger.LogError("Oops! got an error");
_logger.LogWarning("Warning from service");
}
解决方案
我记得在 Mac OS 上工作了很长时间,它不喜欢双斜杠 \。试试这样的东西怎么样?
string logFolder = "Logs";
var logPath = Path.Combine(logFolder, "log.json");
.WriteTo.File(new CompactJsonFormatter(), logPath, rollingInterval: RollingInterval.Minute,shared:true,
....
或者
.WriteTo.File(new CompactJsonFormatter(), @"Logs\log.json", rollingInterval: RollingInterval.Minute,shared:true,