2012-12-05 57 views
2

我有一些自定义异常继承自具有自定义属性的异常我可以记录自定义的异常属性吗?

[Serializable] 
public class MyException : Exception 
{ 
    public MyException(string message, string myProperty) 
    { 
     MyProperty = myProperty 
    } 

    public string MyProperty {get; set;} 
} 

我想自动添加每个自定义属性到我的log4net日志。有没有内置的方式来做到这一点,或者我是否需要创建某种自定义appender?

回答

1

您可以添加自定义字段中使用GlobalContext.Properties到log4net的,就像这样:

GlobalContext.Properties["MyProperty"] = customException.MyProperty; 

然后,在你的appender配置,你可以用%property{MyProperty}访问该自定义字段。

+0

我可以在'XmlLayoutSchemaLog4j'中使用它吗? – hellboy

0

您可以覆盖自定义异常中的.ToString()方法,并将输出附加到基础.ToString()方法。

相关问题