I have a very simple wcf server. When a client uses an “operation” contract, I want to log this in a log file.
I came up with two solutions for this but I doubt they are good solutions and need some hints from the experts here.
My first solution:
instead of creating the host with typeof(serverclass), I use an instance of serverclass.
serverclass has an event and I attach an event handler to it that writes into the log.
The problem with this solution is it requires extra care with mult-threading, re-entrant, multi-session, per call settings … etc.
My second solution:
use a static delegate inside serverclass and still create the host with typeof(serverclass). This way I can assign the logging function to the static delegate.
I don’t feel this is the best way and I really appreciate comments or hints.
Thank you.
You may use WCF behaviour extensions to intercept the call and then write the log file.
There are various ways to intercept it by implementing various extension interfaces but message inspector is one of them.
Have a look here.