c# - 日志记录:使用 try finally block 来记录方法开始和结束是否可以接受?

标签 c# java logging

考虑需要记录具有大量返回语句的方法的情况, 而不是做,

    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/

相关文章:

c# - C# 应该有多重继承吗?

C# 在 txt 文件的开头写入一个零宽度的不间断空格

java - 使用Java生成pdf : Making fields bold

Windows 批处理脚本 : Showing date, 时间和日志中的所有输出

c# - 如何检查两个字典是否包含相同的值?

c# - BouncyCaSTLe C# PublicKey 不同于 GnuPG

java - 在端口 80 上尝试 https 时 tomcat8 处于 100% cpu

java - 如何从父类(super class)方法返回子类引用

logging - 如果应用程序使用 NLog,为什么要使用 ELMAH

ios - 是否有使用 crashlytics 从 ios 发送非致命崩溃的解决方案?