使用自定义字段进行弹性搜索的Serilog配置

问题描述 投票:2回答:1

我正在配置ABP框架,并希望使用Serilog进行日志记录。我在Startup.cs中有以下配置。

var logger = new LoggerConfiguration()
    .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("https://xxxxxxxxxx.com"))
    {
        AutoRegisterTemplate = true,
        TemplateName = "app-log",
        IndexFormat = "app-log-{0:yyyy.MM.dd}",
        CustomFormatter = new ElasticsearchJsonFormatter()
    })
    .WriteTo.File("Serilogs.txt")
    .MinimumLevel.Information()
    .CreateLogger();
Log.Logger = logger;
option.AddSerilog(logger);

使用此配置,我将以下列格式获取日志:

{
    "_index": "app-log-2017.12.18",
    "_type": "logevent",
    "_id": "******",
    "_version": 1,
    "_score": null,
    "_source": {
        "@timestamp": "2017-12-18T15:34:54.3417552+05:30",
        "level": "Information",
        "messageTemplate": "{HostingRequestStartingLog:l}",
        "fields": {
            "Protocol": "HTTP/1.1",
            "Method": "GET",
            "ContentType": null,
            "ContentLength": null,
            "Scheme": "http",
            "Host": "localhost:21021",
            "PathBase": "",
            "Path": "/swagger/",
            "QueryString": "",
            "HostingRequestStartingLog": "Request starting HTTP/1.1 GET http://localhost:21021/swagger/  ",
            "EventId": {
                "Id": 1
            },
            "SourceContext": "Microsoft.AspNetCore.Hosting.Internal.WebHost",
            "RequestId": "****:****",
            "RequestPath": "/swhaggfgggdefrf/"
        },
        "renderings": {
            "HostingRequestStartingLog": [
                {
                    "Format": "l",
                    "Rendering": "Request starting HTTP/1.1 GET http://localhost:21021/swagger/  "
                }
            ]
        }
    },
    "fields": {
        "@timestamp": [
            1513591494341
        ]
    },
    "sort": [
        1513591494341
    ]
}

目前,我正在获取@timestamp,级别和消息。通过使用Log.Logger.Information("Some Template"),我们可以以字段的形式获得一些信息。

现在,在ABP框架中,每个事件都有一个日志。任何人都可以提出修改这些消息的方法吗?例如,对于每个请求,它显示请求启动HTTP / 1.1 GET http://localhost:**********,对于每个响应,它显示请求在7.8262ms 301完成。是否可以修改这些消息?如果是,那怎么样?

我可能不清楚某人。如果需要进一步澄清,请发表评论。提前致谢。

c# elasticsearch .net-core serilog aspnetboilerplate
1个回答
2
投票

你无法修改这些消息。因为Asp.Net Core框架会写入这些日志。您可以看到代码的相关行

enter image description here

https://github.com/aspnet/Hosting/blob/d5ec0859e5496f83ca32bf8bfb68ce3c3efe832f/src/Microsoft.AspNetCore.Hosting/Internal/HostingRequestFinishedLog.cs#L53

© www.soinside.com 2019 - 2024. All rights reserved.