0

有没有办法使用企业库日志记录应用程序块来记录方法参数名称、其值和返回类型值。我在下面提供了一个代码示例。要求是记录它的方法输入参数值及其返回类型值

// 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);

}

谁能解释一下如何使用企业库日志记录应用程序块来实现这一点?

4

2 回答 2

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"/>

有关完整配置,包括日志块配置,请参阅讨论。

于 2013-04-10T21:37:12.213 回答
0

您可以编写一个OnMethodBoundaryAspect来使用PostSharp API拦截您的方法调用。
OnMethodBoundaryAspect.OnEntry方法包括MethodExecutionArgs参数,该参数提供您需要的有关方法及其参数的所有信息。

有关非常接近您的要求的示例日志记录方面实现, 请参阅这篇文章。

// 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();
}

对于方法参数,请检查示例。它们是为缓存而构建的,但 BuildCacheKey/GetCacheKey 方法包括获取方法的参数信息所需的所有信息。

于 2013-04-10T07:18:16.620 回答