簡體   English   中英

使用企業庫日志記錄應用程序塊記錄方法參數和返回類型

[英]Log method parameters and return type using Enterprise library logging application block

有什么方法可以使用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);

}

誰能解釋一下如何使用企業庫日志記錄應用程序塊來實現它?

您可以編寫一個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方法包含獲取方法的自變量信息所需的所有信息。

您可以通過以下代碼使用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"/>

請參見討論全配置,包括日志記錄塊配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM