简体   繁体   中英

.net core console app - serilog write to bin/debug folder

i am using .net5 console app and set serilog to log to Logs/log.txt but instead of write to rootFolder/Logs/log.txt it write to bin/debug folder.

EDIT: I attach the code:

in the appsettings.json it looks like this:

   {
      "Name": "File",
       "Args": {
        "path": "Logs\log.txt",
      }
   }

Program.cs

static void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appSettings.json", true, true)
                .Build();
 
            var serviceCollection = new ServiceCollection()
                .AddLogging(builder => builder.AddSerilog(
                    new LoggerConfiguration()
                        .ReadFrom.Configuration(configuration)
                        .CreateLogger()))
                .BuildServiceProvider();

            var logger = serviceCollection.GetService<ILogger<Program>>();


            logger.LogInformation("hello from Serilog");
            Console.ReadLine(); 
        }

any idea please?

Try this in program.cs file

Log.Logger = new LoggerConfiguration()
                        .Enrich.FromLogContext()
                        .Enrich.WithProperty("Application", "MyApp")
                        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                        .MinimumLevel.Override("System", LogEventLevel.Warning)
                    .WriteTo.File("Logs/log.log", 
                    outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
                    rollingInterval: RollingInterval.Hour)
                    .CreateLogger();

And appsettings file

 "Logging": {
"LogLevel": {
  "Default": "Information",
  "Microsoft": "Warning",
  "Microsoft.Hosting.Lifetime": "Information"
}

},

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