简体   繁体   English

Serilog没有使用.NET Core 2.2写入文件

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

PS The code below works fine. PS下面的代码工作正常。 The problem was between the monitor and the chair. 问题出在显示器和椅子之间。 Answer by @jpgrassi sent me in the right direction to resolve it. 回答@jpgrassi让我朝着正确的方向解决它。

I have the following in program.cs: 我在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>();

}

And in appsettings.json: 在appsettings.json中:

{
  "Serilog": {

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

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

  "AllowedHosts": "*"
}

In the controller, I write out a log _logger.LogWarning("foo"); 在控制器中,我写出了一个log _logger.LogWarning("foo"); , but the file is not written out anywhere, there aren't any error that I can see. ,但文件没有在任何地方写出,没有任何错误,我可以看到。

I've imported following packages from Nuget: Serilog, Serilog.AspNetCore, Serilog.Settings.Configuration, Serilog.Sinks.RollingFile. 我从Nuget导入了以下包:Serilog,Serilog.AspNetCore,Serilog.Settings.Configuration,Serilog.Sinks.RollingFile。

What am I missing? 我错过了什么?

I posted the original answer assuming the problem was the lack of the Using property in the Serilog configuration. 我发布了原始答案,假设问题是Serilog配置中缺少Using属性。 I always had it in my projects and it was the only thing missing in OP's settings. 我总是在我的项目中使用它,这是OP设置中唯一缺少的东西。 But, after posting the answer I tried different configuration alternatives, including the same as in the question and the logs were still being produced. 但是,在发布答案之后,我尝试了不同的配置备选方案,包括与问题中相同的内容,并且仍在生成日志。 Reading more, I found out that the Using property is not necessary. 阅读更多,我发现不需要Using属性。 From the serilog-settings-configuration package: serilog-settings-configuration包中:

(This package implements a convention using DependencyContext to find any package with Serilog anywhere in the name and pulls configuration methods from it, so the Using example above is redundant.) Source: https://github.com/serilog/serilog-settings-configuration (这个包使用DependencyContext实现一个约定,在名称中的任何地方查找任何包含Serilog的包,并从中提取配置方法,因此上面的使用示例是多余的。)来源: https//github.com/serilog/serilog-settings-组态

As it turns out (see question comments) the real issue was log file location and write permissions. 事实证明(参见问题评论)真正的问题是日志文件位置和写入权限。 Code-wise everything was already working. 代码方面的一切都已经有效了。

I'll keep the original answer here, as a matter of history, but its content does not actually fix anything, as nothing was broken in the first place. 作为历史问题,我会在这里保留原始答案,但其内容实际上并没有解决任何问题,因为首先没有任何内容被破坏。


Original answer: 原始答案:

I believe the problem is because you did not specify in your appsettings to actually use the RollingFile sink. 我认为问题是因为您没有在appsettings指定实际使用RollingFile器。 I just quickly created a new app and it works: 我刚刚创建了一个新的应用程序,它的工作原理:

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

Using the following NuGet packages: 使用以下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>

This logs to a file in the root directory of the app log-20190304.txt . 这将记录到应用程序log-20190304.txt的根目录中的文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM