简体   繁体   English

.net 核心控制台应用程序 - serilog 写入 bin/debug 文件夹

[英].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.我正在使用 .net5 控制台应用程序并将 serilog 设置为记录到 Logs/log.txt,但不是写入 rootFolder/Logs/log.txt,而是写入到 bin/debug 文件夹。

EDIT: I attach the code:编辑:我附上代码:

in the appsettings.json it looks like this:在 appsettings.json 它看起来像这样:

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

Program.cs程序.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在 program.cs 文件中试试这个

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和 appsettings 文件

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

}, },

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

相关问题 Net Core 2 Serilog从控制台应用程序注入到Webhost - Net Core 2 Serilog inject from Console App to Webhost appsettings.json 中的哨兵配置与 .Net Core 3 控制台应用程序中的 Serilog - Sentry configuration in appsettings.json with Serilog in .Net Core 3 Console App NET 5 Core 项目上的 MSBuild 生成不同的 bin/x64 和 bin/Debug 文件夹结构 - MSBuild on NET 5 Core projects produces different bin/x64 and bin/Debug folder structures Serilog WriteTo.File() 在控制台应用程序中有效,但在 .net Core 3 Worker Service 中无效 - Serilog WriteTo.File() works in Console App but not in .net Core 3 Worker Service .Net Core Console应用程序 - 调试失败 - 终止与远程端点的连接 - .Net Core Console App - Debug Fails - The connection with the remote endpoint was terminated 从Visual Studio 2017调试.Net Core Console App - Debug .Net Core Console App out of Visual Studio 2017 将.Net Core Console App部署到Raspberry PI和远程调试 - Deploy .Net Core Console App to Raspberry PI and Remote Debug 使用 dotnet run 时如何调试 .NET 核心控制台应用程序 - How to Debug a .NET Core Console App When Using dotnet run 如何加载位于 .NET Core 控制台应用程序文件夹中的程序集 - How to load assemblies located in a folder in .NET Core console app .Net Core app无法将文件写入OSX的任何文件夹 - .Net Core app can't write file to any folder OSX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM