serilog-web / classic

[Discontinued] Serilog web request logging and enrichment for classic ASP.NET applications
Apache License 2.0
79 stars 32 forks source link

Customize HttpRequestEventMessageTemplate #78

Closed pfaustinopt closed 3 weeks ago

pfaustinopt commented 3 years ago

Hello,

First of all, great project you have here.

I'm working on a classic web with ASMX services and I was wondering if there is any chance to customize the HttpRequestEventMessageTemplate to add the "SOAPAction" so I can easily identify which ASMX method is being invoked.

Best regards, Pedro

tsimbalar commented 3 years ago

Hi @pfaustinopt ,

Changing the HttpRequestEventMessageTemplate wouldn't really help, I think, because the properties referenced in the template need to match the parameters we pass here :

https://github.com/serilog-web/classic/blob/f07c97a0204811161f2545bc27aeae3d721ff9da/src/SerilogWeb.Classic/Classic/WebRequestLoggingHandler.cs#L65-L73

Where is this SOAPAction available ? Is it from a HTTP header ? What you might want is actually an new Enricher which would capture this from the HTTP Header, and attach it as a property to logged events ?

Something similar to https://github.com/serilog-web/classic/blob/master/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlReferrerEnricher.cs maybe ?

pfaustinopt commented 3 years ago

Where is this SOAPAction available ? Is it from a HTTP header ? What you might want is actually an new Enricher which would capture this from the HTTP Header, and attach it as a property to logged events ?

Something similar to https://github.com/serilog-web/classic/blob/master/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestUrlReferrerEnricher.cs maybe ?

Yes, it is a header and I've already implemented a new Enricher, just like you suggested. However, I simply decide to log the HttpRequestEventMessageTemplate to a text file, I will not have access to the properties that I add to my Enricher.

As a suggestion changing the HttpRequestEventMessageTemplate, there could be a method (e.g. SetMessageTemplate()) that would receive the message template and an array of params. If we set a custom message template, you would use the custom message and the custom params. Perhaps I can make a pull request with the suggestion?

tsimbalar commented 3 years ago

🤔 I think you'd use an Enricher to attach a property to all log events ... Then, in you Sink you would tweak the "message template" so that it always includes the property you want. The goal is that you don't have to change anything in the place where you generate the log entry.

see https://github.com/serilog/serilog-sinks-file#controlling-event-formatting if you are using Serilog.Sinks.File for instance

pfaustinopt commented 3 years ago

The default format doesn't include the properties that you attach by using an Enricher: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"

To have a log file that shows all the properties you have attached, you need to use this: .WriteTo.File(new CompactJsonFormatter(), "log.txt")

However, this has the disadvantage of not being so user-friendly to read. That's what I was trying to accomplish by modifying the message template, including the SOAP Action in the message so I could see something like this: 2018-07-06 09:02:17.148 +10:00 [INF] HTTP GET /servicexpto.asmx with soap action "xpto" responded 200 in 1994 ms

I'm using Seq sink and there I have no problems because I can explore all the properties that I use in the Enricher, but I was trying to get a readable message in the log files that easily allows me to track the SOAP action that is being invoked.

tsimbalar commented 3 years ago

Oh ok, now I get it.

Not sure i'm really up to changing this library for this edge-case, though, to be honest. It's been used for quite a while and it's probably the first time this request has come up, so it's probably not a change that we would want to do and have to maintain.

In Seq, by the way, isn't it enough to "display property as a column" ? I know I used it a lot in the past for Controller URLs and so on. see https://docs.datalust.co/docs/signals#showing-properties-as-columns

pfaustinopt commented 3 years ago

In Seq, by the way, isn't it enough to "display property as a column" ? I know I used it a lot in the past for Controller URLs and so on. see https://docs.datalust.co/docs/signals#showing-properties-as-columns

Yes, in Seq this is what I already do.

tsimbalar commented 3 weeks ago

This project is no longer being maintained and we are going to archive the SerilogWeb Repositories.

see: