簡體   English   中英

Serilog沒有使用.NET Core 2.2寫入文件

[英]Serilog not writing to file with .NET Core 2.2

PS下面的代碼工作正常。 問題出在顯示器和椅子之間。 回答@jpgrassi讓我朝着正確的方向解決它。

我在program.cs中有以下內容:

public class Program {
    public static void Main(string[] args) {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })
            .UseStartup<Startup>();

}

在appsettings.json中:

{
  "Serilog": {

    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Default": "Information",
        "Microsoft": "Information",
        "System": "Information"
      }
    },

    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "log-{Date}.txt",
        }
      }
    ]
  },

  "AllowedHosts": "*"
}

在控制器中,我寫出了一個log _logger.LogWarning("foo"); ,但文件沒有在任何地方寫出,沒有任何錯誤,我可以看到。

我從Nuget導入了以下包:Serilog,Serilog.AspNetCore,Serilog.Settings.Configuration,Serilog.Sinks.RollingFile。

我錯過了什么?

我發布了原始答案,假設問題是Serilog配置中缺少Using屬性。 我總是在我的項目中使用它,這是OP設置中唯一缺少的東西。 但是,在發布答案之后,我嘗試了不同的配置備選方案,包括與問題中相同的內容,並且仍在生成日志。 閱讀更多,我發現不需要Using屬性。 serilog-settings-configuration包中:

(這個包使用DependencyContext實現一個約定,在名稱中的任何地方查找任何包含Serilog的包,並從中提取配置方法,因此上面的使用示例是多余的。)來源: https//github.com/serilog/serilog-settings-組態

事實證明(參見問題評論)真正的問題是日志文件位置和寫入權限。 代碼方面的一切都已經有效了。

作為歷史問題,我會在這里保留原始答案,但其內容實際上並沒有解決任何問題,因為首先沒有任何內容被破壞。


原始答案:

我認為問題是因為您沒有在appsettings指定實際使用RollingFile器。 我剛剛創建了一個新的應用程序,它的工作原理:

  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.RollingFile" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "RollingFile",
        "Args": { "pathFormat": "log-{Date}.txt" }
      }
    ],
    "Properties": {
      "Application": "Sample"
    }
  }

使用以下NuGet包:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
    <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
    <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
  </ItemGroup>

這將記錄到應用程序log-20190304.txt的根目錄中的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM