2

I am configuring ABP framework and want to use Serilog for logging. I have the following configuration in 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);

With this configuration I am getting the log in following format:

{
    "_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
    ]
}

Currently, I am getting the @timestamp, level and the message. By using Log.Logger.Information("Some Template"), we can get some information in the form of fields.

Now, in the ABP framework, there is a log for each event. Can anyone suggest a way of modifying those messages? For example, for each request it shows Request starting HTTP/1.1 GET http://localhost:********** and for each response, it shows Request finished in 7.8262ms 301. Is it possible to modify these message? If yes then how?

I might not be clear for someone. Please comment if need any further clarifications. Thanks in advance.

7
  • Modify to what? Commented Dec 19, 2017 at 16:58
  • For example, I want to fix the fields in MessageTemplate and want to allow them to be null if that data is not available for any log. Like: "fields": { "ElapsedMilliseconds": 144.6361, "StatusCode": 200, "ContentType": "application/javascript", "HostingRequestFinishedLog": "Request finished in 144.6361ms 200 application/javascript", "EventId": { "Id": 2 }, "SourceContext": "Microsoft.AspNetCore.Hosting.Internal.WebHost", "RequestId": "0HLAHOT40NGN8:00000006", "RequestPath": "/swagger/ui/abc.js" }, Commented Jan 2, 2018 at 10:05
  • I don't see what's null. Commented Jan 2, 2018 at 10:51
  • I mean, I want to fix ElapsedMilliseconds, StatusCode, ContentType, and RequestPath to be fixed properties for each log in the system. And any message should come in a field eg. EventLog. Let it be a starting log or finishing log or any other log. According to @Alper's answer, this has been defined by Asp.Net Core which is correct. However, is it possible to override that? Commented Jan 5, 2018 at 9:42
  • What do you mean by "want to fix ... to be fixed properties for each log in the system"? Commented Jan 5, 2018 at 10:17

1 Answer 1

3

You cannnot modify those messages. Because Asp.Net Core framework writes those logs. You can see the relevant line of the code

enter image description here

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.