繁体   English   中英

Serilog ASP.Net Core 不在 MacOS 中将日志写入文件,但在 Windows 中工作

[英]Serilog ASP .Net Core not writing logs to file in MacOS but works in 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>();
                });

并且在我使用的任何 class 中,我正在创建一个通用的记录器实例,并且使用是这样的

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,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM