简体   繁体   中英

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. 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.

Startup.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

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

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 \. 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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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