繁体   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