简体   繁体   English

Serilog 日志记录到控制台,但不记录到文件或数据库

[英]Serilog logs to console, but not to file or database

I'm creating a web API with ASP.NET Core, but I'm having some trouble getting Serilog to work.我正在使用 ASP.NET 内核创建 web API,但我在让 Serilog 工作时遇到了一些麻烦。 It will output to the console fine.它将 output 到控制台罚款。 However, when I tell it to output to a file, it will create a file, but not enter any logs into it.但是,当我告诉它 output 到一个文件时,它会创建一个文件,但不会在其中输入任何日志。 The same happens when I try to use a PostgreSQL database.当我尝试使用 PostgreSQL 数据库时也会发生同样的情况。 Any ideas on what is wrong?关于什么是错的任何想法?

appsettings.json appsettings.json

{
  "AllowedHosts": "*",

  "Serilog": {
    "MinimumLevel": "Information",
    "Using": [ "Serilog.Sinks.PostgreSQL" ],
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "File",
        "Args": {
          "path": "Serilogs\\AppLogs.log"
        }
      },
      {
        "Name": "PostgreSQL",
        "Args": {
          "connectionString": "Server=fake_server;Port=5432;Database=filter_endpoints;User Id=postgres;Password=fake_password;",
          "tableName": "Logs",
          "needAutoCreateTable": true
        }
      }
    ]
  },

Startup.cs启动.cs

    public Startup(IConfiguration configuration)
        {
            var configurationBuilder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddJsonFile("appsettings.Development.json")
                .AddEnvironmentVariables();

             Log.Logger = new LoggerConfiguration().
              Enrich.FromLogContext().
              ReadFrom.Configuration(configuration).
              CreateLogger();


            Configuration = configurationBuilder.Build();
        }

Edit I got it to work with the file.编辑我让它与文件一起工作。 Apparently, I had had a second block of configuring code like the one in Startup.cs in Program.cs.显然,我有第二个配置代码块,例如 Program.cs 中的 Startup.cs 中的代码。 I took it out, and it is now righting to the file.我把它拿出来,它现在正对着文件。

I'm still having some trouble with getting it to work with the database, though.不过,我仍然无法让它与数据库一起使用。 I added that into the sample code.我将其添加到示例代码中。 I replaced some values in the connection string with sample values for security.为了安全起见,我用示例值替换了连接字符串中的一些值。 I know the connection string is good since the database table was created.我知道连接字符串很好,因为创建了数据库表。

Edit I finally got this to work.编辑我终于让它工作了。 I did have a slight problem with my code.我的代码确实有一点问题。 I believe I should have been using the Serilog.Syncs.PostgrSQL.Configuration package on top of the mail PostgreSQL package.我相信我应该在邮件 PostgreSQL package 之上使用 Serilog.Syncs.PostgrSQL.Configuration package。 I'm not sure if that was a contribution factor or not.我不确定这是否是一个贡献因素。

The main problem was that for some reason the sync did not work with the latest versions of NpgSQL (I found that in a Github issue for the sync).主要问题是由于某种原因,同步不适用于最新版本的 NpgSQL(我在同步的 Github 问题中发现了这一点)。 I downgraded NpgSQL for now.我现在降级了 NpgSQL。 Hopefully they get that fixed soon.希望他们能尽快解决这个问题。

Thank you to everyone who tried to help me out.感谢所有试图帮助我的人。

Have you tried to tell Serilog which sinks it should use?您是否尝试过告诉 Serilog 它应该使用哪些接收器? :-) :-)

"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],

From Serilog documentation: https://github.com/serilog/serilog-settings-configuration#serilogsettingsconfiguration--来自 Serilog 文档: https://github.com/serilog/serilog-settings-configuration#serilogsettingsconfiguration--

I finally got this to work.我终于让这个工作了。 I did have a slight problem with my code.我的代码确实有一点问题。 I believe I should have been using the Serilog.Syncs.PostgrSQL.Configuration package on top of the mail PostgreSQL package.我相信我应该在邮件 PostgreSQL package 之上使用 Serilog.Syncs.PostgrSQL.Configuration package。 I'm not sure if that was a contribution factor or not.我不确定这是否是一个贡献因素。

The main problem was that for some reason the sync did not work with the latest versions of NpgSQL (I found that in a Github issue for the sync).主要问题是由于某种原因,同步不适用于最新版本的 NpgSQL(我在同步的 Github 问题中发现了这一点)。 I downgraded NpgSQL for now.我现在降级了 NpgSQL。 Hopefully they get that fixed soon.希望他们能尽快解决这个问题。

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

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