2

I want to log every request xml message in my WCF service project to database. Please suggest me which is the best and preferred approach.

1) Using idispatchmessageinspector interface

http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector(v=VS.90).aspx

OR

2) writing Custom SQL database trace listener?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Bala
  • 333
  • 1
  • 5
  • 13

3 Answers3

2

You would have to write a custom WCF Trace Listener.

Look here for some help: http://www.enusbaum.com/blog/2007/05/19/creating-a-custom-listener-for-your-wcf-application-in-c/ and http://weblogs.thinktecture.com/cweyer/2009/06/custom-tracelistener-writing-trace-messages-to-the-net-services-service-bus.html

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • I need to do this in PROD as well.. Is there any overhead in enabling tracing in PROD? My requirement is as simple as just logging all xml request to a DB table in WCF application. Any problem in using idispatchmessageinspector? It looks simple! – Bala Jul 29 '11 at 06:32
  • IDispatchMessageInspector's purpose is not logging or tracing, but of course you can use it for that. If you do the logging on a separate thread (see the Task Parallel Library) then there should be almost zero overhead. – Roy Dictus Jul 29 '11 at 07:03
  • Thanks for the reply, MSDN says, "There are a large number of scenarios that require intercepting messages prior to invoking the operation for which it is destined. For example, you can log incoming application messages or perform some feature based on a message header." I am planning to do the logging in async way, which i feel should solve my problem. I feel Tracing may be have some overhead as it is meant for some diagonistic purposes! let me know your thoughts... – Bala Jul 29 '11 at 11:59
  • To be in particular(in separate thread), i will use ThreadPool.QueueUserWorkItem() log the request. – Bala Jul 29 '11 at 12:07
  • Could you please answer http://stackoverflow.com/questions/9702379/queuing-in-oneway-wcf-messages-using-windows-service-and-sql-server ? – LCJ Mar 15 '12 at 13:38
  • The first link is dead. – mutex Nov 05 '15 at 01:57
0

The best approach usually depends on various factors, but in your case it seems that using message inspectors would be a preferred approach as this is what it was build for i.e to capture WCF messages and do whatever you want do with them.

SQL trace is to trace SQL messages (communication between SQL server and SQL client applications) and not WCF messages.

Ankur
  • 33,367
  • 2
  • 46
  • 72
0

I think using idispatchmessageinspector interface and use ThreadPool.QueueUserWorkItem() to read the request and log into database in AfterReceiveRequest event will be better when compared to using Tracing. I feel Tracing may be have some overhead as it is meant for some diagonistic purposes and do not want to enable tracing in prod permanently.

Is there any issues using idispatchmessageinspector and ThreadPool.QueueUserWorkItem() for logging the request message to database?

Thanks!

Bala

GDP
  • 8,109
  • 6
  • 45
  • 82
Bala
  • 1