记录实例数据 [英] Logging instance data

查看:79
本文介绍了记录实例数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时在考虑应该从 ActionExecutingContext 实例中例外地包括哪些字段,我认为拥有一个帮助程序不是很好,它将产生可读的文本表示形式实例数据?

I was thinking which fields I should include in exception from ActionExecutingContext instance and I thought wouldn't it be great to have a helper, that would produce readable text representation of instance data?

我知道,我不会在所有情况下都很完美-我的意思是说它可能做得太深了,它可能不会输出默认情况下需要的所有东西,但是它将在许多情况下比没有任何数据的异常消息更好。

I know, that I won't be perfect in all cases - I mean it may go too deep, it may not output everything is needed by default, but it would be better in many cases than just exception message without any data.

有人在任何开源项目中看到过这样的东西吗?

Has anyone written such thing, saw in any open source project?

推荐答案

您可以使用反射轻松获取对象的所有属性的值:

You can easily get the values of all properties of an object using reflection:

public static void LogPublicProperties(object obj)
{
    foreach (var propertyInfo in d.GetType().GetProperties())
        Console.WriteLine("{0} = {1}",
            propertyInfo.Name,
            propertyInfo.GetValue(d, null));
}

如果要记录字段,请使用 Type .GetFields()而不是 GetProperties()

If you want to log fields, then use Type.GetFields() instead of GetProperties().

这篇关于记录实例数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆