简体   繁体   中英

ASP NET core Serilog : Different files for Request, Response and Exception logs

I am using Serilog for logging and not able to differentiate different kinds of log in different files.

My Requirements are:-

1) Request.log (Daily Rotation)
2) Response.log (Daily Rotation)
3) Exception.log (Daily Rotation)

One method is to create a middleware for Request and Response, and Default file for Exception log.

Is there any other way in whcih I can do following task?

A more common approach is to have a single file.

The reason is that you can correlate the log entries more easily. The way you are proposing, one request coming in that goes through your pipeline and fails (someone throws) and writes a response to the client would have related messages in 3 different files. That would make it harder to debug.

You can use a request id and set it to the scope/context of that request. That way you can augment your log entries. Every log entry related to that request (request/response/errors) will have the same RequestId. This way you can see what Request ended up generating that error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM