我正在尝试在我的一个演示应用程序中使用 serilog 实现日志记录(使用 .net 6)。我正在登录到 sql 数据库。我在 appSettings.json 中将“autoCreateSqlTable”保持为“true”,但 serilog 没有在 sql 中创建表。
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" } ] } } } ] }
程序.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 和其他部分不在 serilog 部分中:
{ "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" } ] } } } ] } }