我正在使用该Gelf.Extensions.Logging
包将日志信息从 asp.net core 3 网站发送到 graylog,并且我想要其他字段,例如当前用户名、客户端 ip、用户代理。
在 startup.cs 中,我有以下内容,但我不知道如何将数据添加到AdditionalFields
我想要的 - 我什至不确定这是否是正确的地方,因为我看不到如何注入http 上下文在这个早期阶段。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.ConfigureLogging((context, builder) => builder.AddGelf(options =>
{
options.AdditionalFields["machine_name"] = Environment.MachineName;
options.AdditionalFields["environment_name"] = context.HostingEnvironment.EnvironmentName;
options.AdditionalFields["app_version"] = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
}));
});
我的 appsettings.json 是这样的:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"System": "Warning"
},
"GELF": {
"Host": "graylog",
"Port": 12202,
"Protocol": "HTTP",
"LogSource": "DataEntry",
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"System": "Warning"
}
}
},
"AllowedHosts": "*",
/* snip */
}