вход в приложение с помощью Serilog
Не удалось получить значение свойства Serilog> SourceContext с помощью кода. Свойство SourceContext не создается для поля «Свойства» в таблице журнала. Попытка получить значение SourceContext для каждого метода, т. Е. Api, службы, репозитория, используя общий метод глобально вместо получения значения SourceContext в каждом методе.
Используя приведенные ниже строки кода
appsetting.json
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "WithOpenTracingContext", "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId", "WithCorrelationId" ],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "connection string",
"autoCreateSqlTable ": true,
"schemaName": "dbo",
"tableName": "LogTable",
"restrictedToMinimumLevel": "Information",
"batchPostingLimit": 50,
"period": "0.00:00:30",
"columnOptionsSection": {
"removeStandardColumns": [ "LogEvent", "MessageTemplate" ],
"customColumns": [
{
"ColumnName": "ClientIp",
"DataType": "VARCHAR",
"AllowNull": true,
"DataLength": 50,
"NonClusteredIndex": true
},
{
"ColumnName": "UserId",
"DataType": "INT",
"AllowNull": true
},
{
"ColumnName": "SessionId",
"DataType": "VARCHAR",
"AllowNull": true,
"DataLength": 50,
"NonClusteredIndex": true
}
]
}
}
}
],
"Using": [ "Serilog.Settings.Configuration" ]
}
configuration.cs
public static IConfigurationRoot ConfigureSqlLogger(this IConfigurationRoot _configuration)
{
LoggerConfiguration config =
new LoggerConfiguration()
.Enrich.WithClientIp()
.Enrich.WithClientAgent()
.ReadFrom.Configuration(_configuration);
Log.Logger = config.CreateLogger();
return _configuration;
}
Startup.cs
public Startup(IWebHostEnvironment environment)
{
try
{
var builder = new ConfigurationBuilder()
.SetBasePath(environment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
Configuration.ConfigureSqlLogger();
}
catch (Exception ex)
{
throw ex;
}
}