Log method parameters and return type.

Topics: Logging Application Block
Apr 5, 2013 at 3:11 PM
Is there any way to log method parameter name , its value and return type value using login application block

I have provided a code sample below. I have requirement to log a methods input parameters value and its return type value
// Complex Types
public class UserDetails
{
    public string UserName { get; set; }
    public int UserAge { get; set; }
    public string UserAddress { get; set; }
}
public class User
{
    public string UserId { get; set; }
    public string Pwd { get; set; }
}

//Interface
public interface IService
{
    UserDetails GetUserDetails(User ReqUser);
}

//Imp
public class Service : IService
    {

        [LogCallHandler(Categories = new string[] { "General" }, LogBeforeCall = true, LogAfterCall = true ,
         BeforeMessage = "This occurs before the call to the target object",AfterMessage="This occured after method call",IncludeParameters=true)]
        public UserDetails GetUserDetails(User ReqUser)
        {
            UserDetails oUD = new UserDetails();
            oUD.UserName = "hhh" + ReqUser.UserId;
            oUD.UserAge = 100;
            oUD.UserAddress = "HHHHHHHHHHHHHHHHHHHHHHH";
            return oUD;
        }

        #endregion
    }

//Usage
private void button2_Click(object sender, EventArgs e)
{
    IUnityContainer container = new UnityContainer().LoadConfiguration();
    container.AddNewExtension<EnterpriseLibraryCoreExtension>();
    IService service = container.Resolve<IService>();
    User nUser = new User();
    nUser.UserId = "TTTTT";
    nUser.Pwd = "XXXXX";
    UserDetails mm = service.GetUserDetails(nUser);

}
Could anyone please explain how to implement this?
Apr 7, 2013 at 7:56 AM
Edited May 1, 2013 at 9:29 PM
I'm assuming that you want to use the the LogCallHandler. You didn't post the configuration but it could look something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
        fileName="rolling.log" formatter="Text Formatter" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
        template="Type: {property(TypeName)}&#xA;CallTime: {property(CallTime)}&#xA;Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Method Parameters: {dictionary({key} - {value}{newline})}&#xA;Return Value: {property(ReturnValue)}&#xA;Method Name: {property(MethodName)}&#xA;"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>
  <unity>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <namespace name="LoggingCallHandler" />
    <assembly  name="LoggingCallHandler" />
    <container>
      <extension type="Interception" />
      <extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />
      <register type="IService" mapTo="Service">
        <interceptor type="TransparentProxyInterceptor" />
        <policyInjection />
      </register>
    </container>
  </unity>
</configuration>
Then you would see output like this:
Type: LoggingCallHandler.Service
CallTime: 00:00:00.0007047
Timestamp: 4/7/2013 6:45:45 AM
Message: This occured after method call
Category: General
Priority: -1
EventId: 0
Severity: Information
Title:Call Logging
Machine: LAURENTIA
App Domain: LoggingCallHandler.vshost.exe
ProcessId: 11596
Process Name: C:\EntLib\Questions\LoggingCallHandler_439252\LoggingCallHandler\bin\Debug\LoggingCallHandler.vshost.exe
Thread Name: 
Win32 ThreadId:10264
Method Parameters: ReqUser - LoggingCallHandler.User
Return Value: LoggingCallHandler.UserDetails
Method Name: GetUserDetails
----------------------------------------
The LogCallHandler simply calls ToString() on parameters and return values so you should override ToString() on your classes.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to