Serilog不将信息记录到SQL Server,但仅记录错误
我正在研究ASP.NET Core 6.0项目,我已经使用Serilog实施了日志记录。我已经将其配置为在SQL Server表中插入记录,但仅记录错误和致命错误。我还需要记录信息,请建议我是否需要更改代码来记录Serilog支持的所有类型日志。
c#代码: -
public static void Main(string[] args)
{
try
{
ConfigureLogging();
var host = CreateHostBuilder(args).Build();
Log.Information("App starting");
host.Run();
}
catch (Exception exception)
{
Log.Fatal(exception, "Error starting - Main method");
}
}
private static void ConfigureLogging()private static void ConfigureLogging()
{
try
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
//.MinimumLevel.Warning()
//.Filter.ByExcluding((le) => le.Level == LogEventLevel.Information)
.CreateLogger();
}
catch (Exception exception)
{
Log.Fatal(exception, "Error starting - ConfigureLogging method");
}
finally
{
//Log.CloseAndFlush(); //no need to close bcz we are using it as a singleton instance.
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<Serilog.ILogger>(Log.Logger);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHangfireJobs hangfireJobs, IHttpContextAccessor httpContextAccessor, DBContext dbContext, Serilog.ILogger logger, IServiceProvider serviceProvider, IAntiforgery antiforgery)
{
}
appsettings.json
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Warning",
"Override": {
//"Microsoft": "Warning", // auto write MS warnings
//"System": "Warning" // auto write System warnings
}
},
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=tcp:xyz.database.windows.net,1433;Initial Catalog=abc;Persist Security Info=False;User ID=sql;Password=123456;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
"tableName": "APILogs",
"schemaName": "dbo",
"autoCreateSqlTable": true,
"columnOptionsSection": {
"addStandardColumns": [ "LogEvent" ],
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
"customColumns": [
{
"ColumnName": "APIPath",
"DataType": "varchar",
"DataLength": 150
},
{
"ColumnName": "ControllerName",
"DataType": "varchar",
"DataLength": 50
},
{
"ColumnName": "MethodName",
"DataType": "varchar",
"DataLength": 80
},
{
"ColumnName": "LineNumber",
"DataType": "int"
}
]
},
"timeStamp": {
"columnName": "TimeStamp",
"convertToUtc": true
},
"restrictedToMinimumLevel": "Warning"
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithProcessId",
"WithThreadId"
]
},
I am working on an asp.net core 6.0 project, I have implemented logging using Serilog. I have configured it to insert records in SQL Server table, but it logs only errors and fatal errors. I need to log information as well, please suggest if I need to change my code to log all types of logs supported by Serilog.
C# Code: -
public static void Main(string[] args)
{
try
{
ConfigureLogging();
var host = CreateHostBuilder(args).Build();
Log.Information("App starting");
host.Run();
}
catch (Exception exception)
{
Log.Fatal(exception, "Error starting - Main method");
}
}
private static void ConfigureLogging()private static void ConfigureLogging()
{
try
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
//.MinimumLevel.Warning()
//.Filter.ByExcluding((le) => le.Level == LogEventLevel.Information)
.CreateLogger();
}
catch (Exception exception)
{
Log.Fatal(exception, "Error starting - ConfigureLogging method");
}
finally
{
//Log.CloseAndFlush(); //no need to close bcz we are using it as a singleton instance.
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<Serilog.ILogger>(Log.Logger);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHangfireJobs hangfireJobs, IHttpContextAccessor httpContextAccessor, DBContext dbContext, Serilog.ILogger logger, IServiceProvider serviceProvider, IAntiforgery antiforgery)
{
}
appSettings.json
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Warning",
"Override": {
//"Microsoft": "Warning", // auto write MS warnings
//"System": "Warning" // auto write System warnings
}
},
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=tcp:xyz.database.windows.net,1433;Initial Catalog=abc;Persist Security Info=False;User ID=sql;Password=123456;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
"tableName": "APILogs",
"schemaName": "dbo",
"autoCreateSqlTable": true,
"columnOptionsSection": {
"addStandardColumns": [ "LogEvent" ],
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
"customColumns": [
{
"ColumnName": "APIPath",
"DataType": "varchar",
"DataLength": 150
},
{
"ColumnName": "ControllerName",
"DataType": "varchar",
"DataLength": 50
},
{
"ColumnName": "MethodName",
"DataType": "varchar",
"DataLength": 80
},
{
"ColumnName": "LineNumber",
"DataType": "int"
}
]
},
"timeStamp": {
"columnName": "TimeStamp",
"convertToUtc": true
},
"restrictedToMinimumLevel": "Warning"
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithProcessId",
"WithThreadId"
]
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在appsettings.json中,请更改
到
那时,它将开始记录信息和以下(信息,警告,错误,关键)
In AppSettings.json, please change
TO
Then it will start logging Information and below (Information, Warning,Error, Critical)