最佳方式使用反射来记录错误射来、错误、方式

2023-09-07 16:26:18 作者:晚鲸

Log ( MethodBase.GetCurrentMethod().DeclaringType.Name + MethodBase.GetCurrentMethod.Name + "blah blah..."  );

返回类似

MyClass.ThisMethod:错误的等等等等..

MyClass.ThisMethod : Error is blah blah..

现在,

1) MethodBase.GetCurrentMethod()。DeclaringType.Name 能抛的NullReferenceException 的。如何避免这种情况?    或者有什么是要知道其中currentmethod是present类的好方法吗?

1) MethodBase.GetCurrentMethod().DeclaringType.Name is capable of throwing a NullReferenceException. How to avoid this? Or what is the good way to know the class in which the currentmethod is present ?

2)是否有实现相同的任何其他方式?

2) Is there any other way to achieve the same ?

推荐答案

有关错误事件的记录,甚至是应用程序函数或方法完成记录,反射调用的表现通常不是一个问题。特别是因为它通常会是preferable使用反射调用在具有在每个类和方法命名字符串。

For error event logging, or even for application function or method completion logging, the performance of the reflection calls are usually not an issue. Especially since it would usually be preferable to use the reflection calls over having a named string in each class and method.

拨打MethodBase不应该在任何性能的关键部分使用。在这些情况下的反射的值,应事先缓存如果可以使用,或用替代的对象,例如上述的命名字符串

Calls to MethodBase should not be used in any performance critical sections. In those cases the reflection values should be cached prior to use if possible, or alternative objects used, such as the aforementioned named strings.

如。日志(m_zClassName,zMethodName,法Wizbot完成。);

eg. Log (m_zClassName, zMethodName, "Method Wizbot completed.");

基准

在我的i7-2600k每次调用MethodBase.GetCurrentMethod()的基准大约为1600纳秒(0.0016毫秒)。

In benchmarks on my i7-2600k each call to MethodBase.GetCurrentMethod() is approximately 1600 nanoseconds (0.0016 milliseconds).

改进

话虽这么说,在code发表的OP可显著通过的没有的调用MethodBase.GetCurrentMethod()的两次的,性能,因为我们希望改善是同一物体的两个成员。

That being said, the code posted by the OP can be significantly improved for performance by not calling MethodBase.GetCurrentMethod() twice, since what we want are two members of the same object.

该任择议定书的code(有一些格式化)=〜两MethodBase.GetCurrentMethod 3200ns()调用:

The OP's code (with some formatting) = ~3200ns in two MethodBase.GetCurrentMethod() calls:

Log(String.Concat(MethodBase.GetCurrentMethod().DeclaringType.Name, ".", MethodBase.GetCurrentMethod().Name, " : Error is blah blah");

这可以通过传递引用MethodBase对象到事件日志写入方法=〜1600ns在一个MethodBase.GetCurrentMethod()调用执行速度提高了一倍:

Which can perform twice as fast by simply passing the reference to the MethodBase object to the event log writing method = ~1600ns in one MethodBase.GetCurrentMethod() call:

EventLogWrite(MethodBase.GetCurrentMethod(), "Error is blah blah");

和事件日志写入方式:

public static void EventLogWrite(MethodBase methodBase, string zErrorMessage)
{
    string classname = methodBase.DeclaringType.Name;
    string methodname = methodBase.Name;

    string logmessage = String.Concat(classname, ".", methodname, " : ", zErrorMessage);

    // ... write message to event log file
}

另外这种设计不需要键入所有这些String.Concat和字段分隔符,在调用事件日志写入方法的每个实例。

Plus with this design you don't need to type all of that String.Concat and field separators in every instance of the call to the event log writing method.