简体   繁体   中英

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. It will output to the console fine. However, when I tell it to output to a file, it will create a file, but not enter any logs into it. The same happens when I try to use a PostgreSQL database. Any ideas on what is wrong?

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

    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. 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. 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). I downgraded NpgSQL for now. 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? :-)

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

From Serilog documentation: 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. 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). I downgraded NpgSQL for now. Hopefully they get that fixed soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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