簡體   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