linux - Serilog 在 Linux 上运行时不创建日志文件

标签 linux asp.net-core serilog

Linux 上运行 ASP.NET Core 应用程序时,我无法启用日志记录。我正在使用 Serilog.Sinks.File(但也尝试使用 RollingFile),并在 appsettings 中定义了以下配置:

"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "Microsoft": "Warning",
    "System": "Warning"
  }
},
"WriteTo": [
  {
    "Name": "File",
    "Args": { "path": "core_log.log", "rollingInterval": "Day" }
  }
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
  "Application": "CORE service"
}  }

我还尝试使用 pathFormat 与 RollingFile sink 一起使用,但没有成功。无论我尝试什么,应用程序都没有创建日志文件。

我还尝试了多种路径变体,例如:/var/test/app/core_log.log 或只是简单的 core_log.log,但我在任何地方都看不到文件。我尝试使用以下方式搜索它:

sudo find / -name '*.log' -print | grep core_log

值得一提的是,在 Windows 上运行相同的应用程序时,一切正常,我可以看到创建的日志文件。

有人在 Linux 上使用 Serilog 时遇到过类似的问题吗?会不会是和权限有关的东西?

最佳答案

刚刚用这个配置解决了同样的问题:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            Log.Logger = new LoggerConfiguration()
                .WriteTo
                .File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs/.log"), rollingInterval: RollingInterval.Day)
                .CreateLogger();
        })
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
            logging.AddSerilog(dispose: true);
        })
        .UseStartup<Startup>();

特别注意 AppDomain.CurrentDomain.BaseDirectory。这将指向您的 Linux 部署文件夹。我想也可以从配置文件中读取文件夹。

我还必须手动创建文件夹,正如 Nicholas 所指出的(谢谢!,不仅如此),但不是文件。

关于linux - Serilog 在 Linux 上运行时不创建日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48251515/

相关文章:

linux - 在 Linux 中使用针对 Windows 制作的 SDK 来适应不同的架构

c# - VS2017 中的 ASP.Net Core : Setting up React. js + webpack + babel

logging - Serilog.Expressions SubLogger 对多个文件的日志级别

linux - 现代cpus如何处理跨页未对齐访问?

linux - 在 CVS 中重命名文件

c# - EF 核心 : drop and recreate database when server restarts

asp.net-core - ASP.Net Core RC2 JWT 承载 token

c# - 如果异步日志记录在 asp.net core 中失败,则调用一个操作

.net-core - 从环境变量设置 Serilog 最低级别

linux - 如果 block 设备已经格式化,则退出 mkfs 命令