有没有办法使用Enterprise库日志记录应用程序块记录方法参数名称,其值和返回类型值。 我在下面提供了一个代码示例。要求是记录它的方法输入参数值及其返回类型值
// 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);
}
有没有人可以解释如何使用企业库日志记录应用程序块来实现它?
答案 0 :(得分:0)
您可以使用OnMethodBoundaryAspect API编写PostSharp来截取您的方法调用 OnMethodBoundaryAspect.OnEntry方法包括MethodExecutionArgs参数,该参数提供有关方法及其参数的所有信息。
请参阅this帖子,了解非常接近您要求的示例日志记录方面实施。
// This method is executed before the execution of target methods of this aspect.
public override void OnEntry( MethodExecutionArgs args )
{
// Build method information to log.
string methodInfo = BuildMethodInformation(args.Arguments);
// continue with your logging...
}
您可以通过MethodExecutionArgs参数的Arguments成员获取方法参数,如下所示:
private string BuildMethodInformation(Arguments arguments)
{
var sb = new StringBuilder();
sb.Append(_methodName);
foreach (var argument in arguments.ToArray())
{
sb.Append(arguments.GetArgument( i ) ?? "null");
}
return sb.ToString();
}
对于方法参数,请检查this或this个样本。它们是为缓存而构建的,但BuildCacheKey / GetCacheKey方法包含获取方法参数信息所需的所有信息。
答案 1 :(得分:0)
您可以通过代码使用EntLib LogCallHandler:
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
container.RegisterType<IService, Service>(
new InterceptionBehavior<PolicyInjectionBehavior>(),
new Interceptor<TransparentProxyInterceptor>());
或者通过配置文件:
<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>
此处,LoggingCallHandler
是服务类的名称空间/程序集。或者,您可以像这样定义类型别名:
<alias alias="Service" type="LoggingCallHandler.Service, LoggingCallHandler"/>
<alias alias="IService" type="LoggingCallHandler.IService, LoggingCallHandler"/>