考虑需要记录具有大量返回语句的方法的情况, 而不是做,
if(condition1)
{
calculation here
do log
return a
}
else if(condition2)
{
calculation here
do log
return b
}
else
{
calculation here
do log
return c
}
如果日志语句相同,是不是这样记录比较好?
try
{
if(condition1)
{
calculation here
return a
}
else if(condition2)
{
calculation here
return b
}
else
{
calculation here
return c
}
}
finally
{
do log
}
如果我们创建一个 try finally block 只是为了记录日志,会有什么影响吗?最佳实践是什么?
最佳答案
为什么不像这样最后返回
var returnValue
if(condition1)
{
calculation here
returnValue = a
}
else if(condition2)
{
calculation here
returnValue = b
}
else
{
calculation here
returnValue = c
}
do log
return returnValue
关于c# - 日志记录:使用 try finally block 来记录方法开始和结束是否可以接受?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14731715/