我环顾四周,但找不到有效的解决方案,并且由于所有示例看起来都相似,所以我认为这一定是我忽略了明显的内容。 还要注意,这是我使用.NET Core和Serilog进行的第一个实际项目。
我正尝试将一些自定义属性(使用serilog)记录到appsettings.json
中定义的数据库中自己的列中,并通过中间件添加到管道中。但是,即使在属性XML列中正确填写了相同的项目,这些值仍为NULL。
因此,对于身份验证的用户,我具有预期的新属性:
<properties>
<property key="UserId">ff88ddb9-6f5a-4ad5-a2a8-1d016ff99398</property>
<property key="UserName">ernst</property>
<properties>
但是我也想将这些值也添加到它们自己的列中,以进行一些查询优化。
代码是.NET Core 2.1+,并且我已经按照https://github.com/serilog/serilog-aspnetcore/blob/dev/samples/SimpleWebSample/Program.cs所示设置了Serilog:
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 unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
private static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseConfiguration(Configuration)
.UseSerilog()
.CaptureStartupErrors(true)
.Build();
要添加信息,我在中间件中使用LogContext.PushProperty
。
配置的相关部分:
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Debug",
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=localhost;Database=MyDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
"tableName": "SeriLog_Custom",
"autoCreateSqlTable": true,
"restrictedToMinimumLevel": "Information",
"columnOptionsSection": {
"customColumns": [
{
"ColumnName": "UserId",
"DataType": "nvarchar",
"DataLength": 450,
"AllowNull": true
},
{
"ColumnName": "UserName",
"DataType": "nvarchar",
"DataLength": 256,
"AllowNull": true
}
]
}
}
}
]
}
此外,除了不填充自定义列外,数据库也以标准方式创建,而没有这些列,因此我手动添加了它们。
这可能指向配置问题?
创建表的SQL:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SeriLog_Custom](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Message] [nvarchar](max) NULL,
[MessageTemplate] [nvarchar](max) NULL,
[Level] [nvarchar](128) NULL,
[TimeStamp] [datetime] NOT NULL,
[Exception] [nvarchar](max) NULL,
[Properties] [xml] NULL,
[UserId] [nvarchar](450) NULL,
[UserName] [nvarchar](256) NULL,
CONSTRAINT [PK_SeriLog_Custom] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
现在,我可以通过以下方式获得所需的信息:
SELECT [Id]
,[Message]
,[MessageTemplate]
,[Level]
,[TimeStamp]
,[Exception]
,[UserId]
,[UserName]
,[Properties].value('(//property[@key="UserName"]/node())[1]', 'nvarchar(max)') as UserName
,[Properties]
FROM [ECSControl].[dbo].[SeriLog_ECMonitor]
where [Properties].value('(//property[@key="UserName"]/node())[1]', 'nvarchar(max)') = 'ernst'
order by id desc;
但是我想通过以下方式访问它:
SELECT [Id]
,[Message]
,[MessageTemplate]
,[Level]
,[TimeStamp]
,[Exception]
,[UserId]
,[UserName]
,[Properties]
FROM [ECSControl].[dbo].[SeriLog_ECMonitor]
where UserName = 'ernst'
order by id desc;
答案 0 :(得分:5)
Serilog.Sinks.MSSqlServer (5.1.2)
的最新稳定版本不支持Microsoft.Extensions.Configuration
。与Serilog.Settings.Configuration (2.6.1)
(ref)的最新稳定版本相同。
更新到预发行版本Serilog.Sinks.MSSqlServer 5.1.3-dev-00204
和Serilog.Settings.Configuration 3.0.0-dev-00119
解决了此问题。