Reputation: 9460
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...
Upvotes: 0
Views: 1574
Reputation: 1838
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"
}
Upvotes: 1
Reputation: 1665
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));
Upvotes: 0