简体   繁体   English

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

[英]Serilog ASP .Net Core not writing logs to file in MacOS but works in Windows

I've configured serilog in my asp .net core 3.1 project.我已经在我的 asp .net core 3.1 项目中配置了 serilog。 It is working fine in windows but when I ran it on Mac, I didn't get any log files created.So I checked by Writing to Console, it worked fine but writing to file is not working that too in mac only, in windows both works fine.它在 windows 中运行良好,但是当我在 Mac 上运行它时,我没有创建任何日志文件。所以我通过写入控制台进行了检查,它运行良好,但仅在 mac 中写入文件也不起作用,在 windows两者都工作正常。

Startup.cs启动.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();
        }

Program.cs程序.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

and in whichever class i use I'm creating a generic instance of logger and using is like this并且在我使用的任何 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");
}

I remember working a long time with Mac OS and it does not like the double slash \.我记得在 Mac OS 上工作了很长时间,它不喜欢双斜杠 \。 How about trying something like this?试试这样的东西怎么样?

    string logFolder = "Logs";
    var logPath = Path.Combine(logFolder, "log.json");
    .WriteTo.File(new CompactJsonFormatter(), logPath, rollingInterval: RollingInterval.Minute,shared:true,
....

OR或者

.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