0

I'm new to Serilog, and trying to configure logging to SQL Server. My Program.cs looks like this...

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Serilog;

namespace MyWeb {
  public class Program {
    public static void Main(string[] args) =>
      CreateWebHostBuilder(args).Build().Run();

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

...and the Serilog section of appSettings.json looks like this...

"Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Error",
        "System": "Error"
      }
    },
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "myconnstr",
          "tableName": "Logs"
        }
      }
    ]
  },

The section in appSettings.Development.json looks like this...

  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Fatal",
        "System": "Fatal"
      }
    },
    "WriteTo": [
      {
        "Name": "Debug"
      },
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Data Source=.;Initial Catalog=MyWebDb;Trusted_Connection=True;MultipleActiveResultSets=true",
          "tableName": "Logs"
        }
      }
    ]
  }

I manually created the Logs table with this SQL...

CREATE TABLE [Logs] (
   [Id] int IDENTITY(1,1) NOT NULL,
   [Message] nvarchar(max) NULL,
   [MessageTemplate] nvarchar(max) NULL,
   [Level] nvarchar(128) NULL,
   [TimeStamp] datetimeoffset(7) NOT NULL,  
   [Exception] nvarchar(max) NULL,
   [Properties] xml NULL,
   [LogEvent] nvarchar(max) NULL
   CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED ([Id] ASC) 
) 

When I run the web site in VS with the above settings, logging works fine. If I change the connection string in appSettings.Development.json to the production connection string, then run in VS, everything seems to work fine, no exceptions reported, but nothing is written to the Logs table on the production database.

I tried copying the code from this SO answer, and tried the advice in this SO answer, but the result was always the same. When I use the local connection string it works fine, if I use the production one, nothing is logged.

I'm using the same connection string for Serilog as I use for the rest of the site, and all other database access works fine, so it doesn't look like a permissions problem.

Anyone any ideas what I'm doing wrong? Thanks

I'm using ASP.NET Core 2.2 and Serilog versions as follows...

  • Serilog.AspNetCore Version 2.1.1
  • Serilog.Settings.Configuration Version 3.1.0
  • Serilog.Sinks.File Version 4.0.0
  • Serilog.Sinks.MSSqlServer Version 5.1.2
Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106

2 Answers2

1

Make sure that you use [dbo] schema in your database. If you have a different schema, update your configuration like this:

"Name": "MSSqlServer",
"Args": {
      "connectionString": "Your connection string",
      "schemaName": "your schema name",
      "tableName": "Logs"
 }
Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
0

Check the permissions on the production server and make sure the connected user has the proper permissions.

Also, Serilog does not report logging problems by default. Add SelfLog.Enable() if you want to enable diagnostics.

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
ctaleck
  • 1,658
  • 11
  • 20
  • Forgot to mention that the Logs table is in the same database as the rest of the tables, and I'm using the same connection string for the main app as for the logging. Everything else works fine, so I don't think it's a permissions problem. As for the code you suggested, where will that log? I only have FTP access to the server, will I be able to see this output? Thanks – Avrohom Yisroel Jun 20 '19 at 12:47
  • With only FTP access, I recommend changing the SelfLog to write to a local text file and then access the log file. – ctaleck Jun 21 '19 at 16:10
  • I originally wrote a text file, but couldn't download it as it was locked. I then found I could log to a database, and preferred that option, as much for the superior ability to filter the data. I would still like to do that. I just can't work out why I can access the database for everything else, but Serilog fails. – Avrohom Yisroel Jun 21 '19 at 16:14