Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF service attribute to log method calls and exceptions
    text
    copied!<p>I have a requirement to log each method call in a WCF service, and any exceptions thrown. This has led to a lot of redundant code, because each method needs to include boilerplate similar to this:</p> <pre><code>[OperationContract] public ResultBase&lt;int&gt; Add(int x, int y) { var parameters = new object[] { x, y } MyInfrastructure.LogStart("Add", parameters); try { // actual method body goes here } catch (Exception ex) { MyInfrastructure.LogError("Add", parameters, ex); return new ResultBase&lt;int&gt;("Oops, the request failed", ex); } MyInfrastructure.LogEnd("Add", parameters); } </code></pre> <p>Is there a way I can encapsulate all this logic into an attribute <code>MyServiceLoggingBehaviorAttribute</code>, which I could apply to the service class (or methods) like this:</p> <pre><code>[ServiceContract] [MyServiceLoggingBehavior] public class MyService { } </code></pre> <hr/> <p><strong>Note #1</strong> </p> <p>I realize that this can be done using <a href="https://stackoverflow.com/questions/4133569/how-to-log-method-calls-on-targets-marked-with-an-attribute">Aspect-oriented programming</a>, but in C# the only way to do this is to modify bytecode, which requires the use of a third-party product like PostSharp. I would like to avoid using commercial libraries.</p> <p><strong>Note #2</strong></p> <p>Note that Silverlight applications are the primary consumers of the service.</p> <p><strong>Note #3</strong></p> <p><a href="https://stackoverflow.com/questions/1178256/log-wcf-service-calls-with-parameter-information">WCF trace logging</a> is a good option in some cases, but it doesn't work here because, as noted above, I need to inspect, and in the case of an exception change, the return value.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload