Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:19:00+00:00 2026-06-15T06:19:00+00:00

I have a requirement to log each method call in a WCF service, and

  • 0

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:

[OperationContract]
public ResultBase<int> 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<int>("Oops, the request failed", ex);
    }
    MyInfrastructure.LogEnd("Add", parameters);
}

Is there a way I can encapsulate all this logic into an attribute MyServiceLoggingBehaviorAttribute, which I could apply to the service class (or methods) like this:

[ServiceContract]
[MyServiceLoggingBehavior]
public class MyService
{
}

Note #1

I realize that this can be done using Aspect-oriented programming, 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.

Note #2

Note that Silverlight applications are the primary consumers of the service.

Note #3

WCF trace logging 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:19:01+00:00Added an answer on June 15, 2026 at 6:19 am

    Yes, it is possible to encapsulate this kind of logging, using the extensibility points built into WCF. There are actually multiple possible approaches. The one I’m describing here adds an IServiceBehavior, which uses a custom IOperationInvoker, and does not require any web.config modifications.

    There are three parts to this.

    1. Create an implementation of IOperationInvoker, which wraps the method invocation in the required logging and error-handling.
    2. Create an implementation of IOperationBehavior that applies the invoker from step 1.
    3. Create an IServiceBehavior, which inherits from Attribute, and applies the behavior from step 2.

    Step 1 – IOperationInvoker

    The crux of IOperationInvoker is the Invoke method. My class wraps the base invoker in a try-catch block:

    public class LoggingOperationInvoker : IOperationInvoker
    {
        IOperationInvoker _baseInvoker;
        string _operationName;
    
        public LoggingOperationInvoker(IOperationInvoker baseInvoker, DispatchOperation operation)
        {
            _baseInvoker = baseInvoker;
            _operationName = operation.Name;
        }
    
        // (TODO stub implementations)
    
        public object Invoke(object instance, object[] inputs, out object[] outputs)
        {
            MyInfrastructure.LogStart(_operationName, inputs);
            try
            {
                return _baseInvoker.Invoke(instance, inputs, out outputs);
            }
            catch (Exception ex)
            {
                MyInfrastructure.LogError(_operationName, inputs, ex);
                return null;
            }
            MyInfrastructure.LogEnd("Add", parameters);
        }
    }
    

    Step 2 – IOperationBehavior

    The implementation of IOperationBehavior simply applies the custom dispatcher to the operation.

    public class LoggingOperationBehavior : IOperationBehavior
    {
        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.Invoker = new LoggingOperationInvoker(dispatchOperation.Invoker, dispatchOperation);
        }
    
        // (TODO stub implementations)
    }
    

    Step 3 – IServiceBehavior

    This implementation of IServiceBehavior applies the operation behavior to the service; it should inherit from Attribute so that it can be applied as an attribute to the WCF service class. The implementation for this is standard.

    public class ServiceLoggingBehavior : Attribute, IServiceBehavior
    {
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
            {
                foreach (OperationDescription operation in endpoint.Contract.Operations)
                {
                    IOperationBehavior behavior = new LoggingOperationBehavior();
                    operation.Behaviors.Add(behavior);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to create a sysDesk log file. In this requirement I
I have a requirement to write to a log file on reception of any
We have a requirement to log IP address information of all users who use
I have one requirement that i have to place the log file in the
I have requirement to disable copy/paste/cut operations on a textbox. For this purpose I
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
I have this requirement that I need to replace URL in CSS, so far
I have a requirement to modify a simple windows service written in win32 C
I have to meet the requirement to log on a database all the requests
I have requirement where some times I would like to load children as well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.