2011-09-12 69 views
1

说我有一个类似这样的类。日志函数调用c中的参数值和返回值#

public static class Config 
{ 
    public static string GetAppSetting(string key) 
    { 
     return ConfigurationManager.AppSettings[key].ToString(); 
    } 
} 

,我想每次调用此方法的关键参数&返回值一起记录。

的唯一代码的变化我想提出的是这样的:

[Log] 
public static class Config 
{ 
    public static string GetAppSetting(string key) 
    { 
     return ConfigurationManager.AppSettings[key].ToString(); 
    } 
} 

我将最有可能使用log4net的记录从日志属性的调用。这怎么能实现?

在此先感谢!

+0

这可能重复好一:http://stackoverflow.com/questions/4133569/how-to-log-method-calls-on-targets-marked-with-an-attribute –

回答

2

您可以使用像PostSharp这样的工具来创建日志记录方面。

3

据我所知,你可以做到这一点的唯一途径是通过与作为PostSharp这样的库面向方面的编程。

0

这也许是可能的唯一方法是重写为具有[Log]属性的所有类所生成/编译IL代码。为此,您需要编写一个工具来分析和操作代码,并将其注册为“Post build event”(在Visual Studio中 - > Project settings)。

对于这样的单声道塞西尔工作可能是一个很大的帮助:http://www.mono-project.com/Cecil

但大多数propably您的重写代码,改变方法签名的东西像

public static string GetAppSetting(string key) 
{ 
    var result = ConfigurationManager.AppSettings[key].ToString(); 

    Trace.TraceInformation(String.Format("Config.GetAppSetting - Key: {0}, Result: {1}", key, result)); 

    return result; 
}