【问题标题】:Log method parameters and return type using Enterprise library logging application block使用企业库日志记录应用程序块记录方法参数和返回类型
【发布时间】:2013-04-05 14:25:10
【问题描述】:

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

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

}

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

【问题讨论】:

    标签: enterprise-library entlib-logging


    【解决方案1】:

    您可以编写一个OnMethodBoundaryAspect 来使用PostSharp API 拦截您的方法调用。
    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();
    }
    

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

    【讨论】:

    • 感谢哈桑提供的信息。我们的计划是用 entlib 替换 PostSharp。所以你的答案对我不起作用。
    • 我明白了。由于我不知道您的 PostSharp 约束,我认为您仍然可以在 PostSharp 方面中使用 EntLib logwriter。然后,如果我这次猜对了您的需求,您可以按照我的下一个答案来实现使用 LogCallHandler(因为 cmets 中的字符限制)。
    【解决方案2】:

    您可以通过代码使用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"/>
    

    请参阅thisthis 讨论以了解完整配置,包括日志记录块配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      相关资源
      最近更新 更多