Is there any way to log method parameter name , its value and return type value using Enterprise library logging application block. I have provided a code sample below. The requirement is to log it's 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 using Enterprise library logging application block?
You can use EntLib LogCallHandler by code:
Or by config file:
Here,
LoggingCallHandler
is namespace/assembly for your service class. Alternatively, you can define your type alias like this:See this or this discussion for full configuration including logging block configuration.