How to log methods params and values using exception handling and logging application blocks

Topics: Exception Handling Application Block, Logging Application Block
Mar 2, 2012 at 1:34 AM


We are using Unity along with Exception handling and Logging application blocks in our WCF Services. We would like to log the method parameters and their values when there is an exception. Can you please provide some guidance or link on how to do this?



Mar 3, 2012 at 3:56 PM

It sounds like you need to use PolicyInjection (or Interception).  Enterprise Library has an exception handling call handler as well as a logging call handler that will log method parameters.  Unfortunately, they don't do exactly what you want but it wouldn't be too much work to create your own call handler.

Some links that could help: Creating Interception Policy Injection Call Handlers,   Creating Interception Handler AttributesCreating Policy Injection Matching Rules.

So as a rough start your handler might look something like this:

public class MyCallHandler2 : ICallHandler
    public MyCallHandler2()

    private LogEntry GetLogEntry(IMethodInvocation input)
        LogEntry logEntry = new LogEntry();

        logEntry.EventId = 100;
        logEntry.Priority = 0;
        logEntry.Severity = TraceEventType.Information;
        logEntry.Title = "Exception Method Args";

        Dictionary<string, object> parameters = new Dictionary<string, object>();
        for (int i = 0; i < input.Arguments.Count; ++i)
            parameters[input.Arguments.GetParameterInfo(i).Name] = input.Arguments[i];

        parameters["CallStack"] = Environment.StackTrace;
        parameters["TypeName"] = input.Target.GetType().FullName;
        parameters["MethodName"] = input.MethodBase.Name;

        logEntry.ExtendedProperties = parameters;

        return logEntry;

    IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        IMethodReturn result = getNext()(input, getNext);

        if (result.Exception != null)
            var logEntry = GetLogEntry(input);

        return result;

    int ICallHandler.Order { get; set; }

Randy Levy
Enterprise Library support engineer