Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Exception Logging

Tags:

c#

.net

wcf

Is it possible to log exception like ThrowMaxReceivedMessageSizeExceeded from my WCF service. I know that the message size can be increased in the config but I would also like it logging on my side. I have Log4Net running to catch all unhandled exceptions but it seems this is so not getting logged, so is probably handled.

like image 796
Eds Avatar asked Mar 24 '14 13:03

Eds


People also ask

What is exception handling in WCF?

Leverage fault exceptions in WCF to transmit user friendly error messages to the presentation layer when exceptions occur. Exceptions are errors that occur at runtime; exception handling is the technique of handling these runtime errors.

Which of the exception does WCF throw?

Expected exceptions from communication methods on a WCF client include TimeoutException , CommunicationException , and any derived class of CommunicationException . These indicate a problem during communication that can be safely handled by aborting the WCF client and reporting a communication failure.

How can I trace a WCF service call?

WCF tracing is built on top of System. Diagnostics. To use tracing, you should define trace sources in the configuration file or in code. WCF defines a trace source for each WCF assembly.


2 Answers

You need to enable Tracing. That can be sent to Nlog. My XYZ.exe.config has a section which looks like that:

<system.diagnostics>
    <sources>
        <source name="System.ServiceModel" switchValue="Error" propagateActivity="true" >
            <listeners>
                <add name="nlog"/>
            </listeners>
        </source>
        <!--source name="System.ServiceModel.MessageLogging">
            <listeners>
                <add name="nlog" />
            </listeners>
        </source-->
    </sources>
    <sharedListeners>
        <add name="nlog" type="NLog.NLogTraceListener, NLog" />
    </sharedListeners>
</system.diagnostics>

NOTE: I have the NLog config in the XYZ.exe.config as well!

EDIT

I just realized that you were talking about log4net. Not NLog.

If you follow the last link provided by StephaneT and implement the Log4netTraceListener you should be able to use the XYZ.exe.config solution as well.

like image 70
toATwork Avatar answered Sep 18 '22 05:09

toATwork


By default, WCF uses system.diagnostics tracing to log exceptions. See here and here and here

You can redirect system traces to log4net as explained in this question.

like image 25
stombeur Avatar answered Sep 18 '22 05:09

stombeur