. net 6 Serilog没有创建日志表



我正试图在我的一个演示应用程序(使用。net 6)中使用serilog实现日志记录。我保留了autocreatesqtable";true"在appSettings。

appSettings.json

{
"ConnectionStrings": {
"DefaultConnection": "Server=Test;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"System": "Warning",
"System.Net.Http.HttpClient": "Warning"
}
}
},
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [ "FromLogContext", "WithMachineName", "WithClientIp", "WithClientAgent" ],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=Test;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;",
"sinkOptionsSection": {
"tableName": "LogsTable",          
"autoCreateSqlTable": true          
},
"restrictedToMinimumLevel": "Information",
"columnOptionsSection": {
"primaryKeyColumnName": "Id",
"addStandardColumns": [ "LogEvent", "SourceContext" ],
"additionalColumns": [
{
"ColumnName": "ClientIP",
"PropertyName": "ClientIp",
"DataType": "nvarchar"
}            
]
}
}
}
]
}

Program.cs

using Serilog;
using Serilog.Events;
var builder = WebApplication.CreateBuilder(args);

#region Configure serilog
builder.Logging.ClearProviders();
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.MinimumLevel.Override("Microsoft", LogEventLevel.Error)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Error)
.MinimumLevel.Override("Serilog", LogEventLevel.Error)
.Enrich.FromLogContext()
.Enrich.WithClientIp()
.Enrich.WithClientAgent()
.CreateLogger();
Log.Logger = logger;
builder.Logging.AddSerilog(logger);
builder.Host.UseSerilog();
// Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));
#endregion

// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSerilogRequestLogging();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
下面是我的nuget包:
Serilog.AspNetCore - 6.0
Serilog.Settings.Configuration - 3.3.0
Serilog.Sinks.MSSqlServer - 5.7.1
Serilog.Enrichers.ClientInfo - 1.2.0

我测试了连接字符串,它工作正常,但不知何故serilog没有创建表。我不明白我错在哪里。

有谁能帮我一下吗?

问题出在你的appsettings.json。

using、enrich等部分都是out of seriog部分:

{
"ConnectionStrings": {
"DefaultConnection": "Server=Test;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Serilog": {
"Using": [ "Serilog.Enrichers.ClientInfo","Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"System": "Warning",
"System.Net.Http.HttpClient": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithClientIp", "WithClientAgent" ],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=Test;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;",
"sinkOptionsSection": {
"tableName": "LogsTable",          
"autoCreateSqlTable": true          
},
"restrictedToMinimumLevel": "Information",
"columnOptionsSection": {
"primaryKeyColumnName": "Id",
"addStandardColumns": [ "LogEvent", "SourceContext" ],
"additionalColumns": [
{
"ColumnName": "ClientIP",
"PropertyName": "ClientIp",
"DataType": "nvarchar"
}            
]
}
}
}
]
}
}

相关内容

  • 没有找到相关文章

最新更新