我环顾四周,但找不到有效的解决方案,并且由于所有示例看起来都相似,因此我认为一定是我忽略了明显的内容。还要注意,这是我使用.NET Core和Serilog进行的第一个实际项目。
我试图将一些自定义属性登录(使用serilog)到数据库中自己定义的列中,这些定义在appsettings.json
管道中通过中间件定义并通过中间件添加。但是,即使在属性XML列中正确填充了相同的项目,这些值仍为NULL。
因此,对于身份验证的用户,我具有预期的新属性:
<properties>
<property key="UserId">ff88ddb9-6f5a-4ad5-a2a8-1d016ff99398</property>
<property key="UserName">ernst</property>
<properties>
Run Code Online (Sandbox Code Playgroud)
但是我也想将这些值添加到它们自己的列中,以进行一些查询优化。
代码是.NET Core 2.1+,并且我已经设置了Serilog,如https://github.com/serilog/serilog-aspnetcore/blob/dev/samples/SimpleWebSample/Program.cs所示:
private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.CreateLogger();
try
{
Log.Information($"Starting web host in {Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"} environment, version {Util.Version.Long}");
BuildWebHost(args).Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated …
Run Code Online (Sandbox Code Playgroud)