我环顾四周,但我找不到一个有效的解决方案,因为所有的例子看起来都很相似,我认为一定是我忽略了一些明显的东西 . 另请注意,这是我第一个使用.NET Core和Serilog的真实项目 .
问题
我正在尝试将(使用serilog)一些自定义属性记录到 appsettings.json
中定义的数据库中的自己的列中,并通过管道上的中间件添加 . 但是值仍然为NULL,即使在属性XML列中正确填充了相同的项 .
所以我为经过身份验证的用户提供了预期的新属性:
<properties>
<property key="UserId">ff88ddb9-6f5a-4ad5-a2a8-1d016ff99398</property>
<property key="UserName">ernst</property>
<properties>
但是我想将这些值添加到它们自己的列以及一些查询优化中 .
代码
代码是.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 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;
1 回答
最新的稳定版
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
解决了此问题 .