【发布时间】:2012-03-15 00:24:31
【问题描述】:
每当我们需要异常处理时,我们都会用 try/catch 块包围,但是当我们包围 try/catch 块时,Visual Studio 会这样给出。
try
{
}
Catch (Exception e)
{
// here we need to write code for logging every time manually.
}
我们能否使用我们自己的异常记录代码自动执行 catch 块的过程,这将始终相同。我的意思是,每当我环绕方法时,可以自动将我们自己的日志记录代码包含到异常块中吗?是否有任何工具或解决方法。
1234563 ,在 catch 块中,它将包含我们所有的日志记录代码。
可能是这样的
Catch(Exception e ) // i am telling it for a function, not as global catch handler.
{ // logging at method/function level. for method which we need to log
ourLogging obj = new Ourlogging(); // these two line should added automaticaly,
obj.Publish(e); //as when user surrounded with try/catch block ?
obj = null;
}
- 如果有任何 Visual Studio 插件或一些解决方法,或者如果任何机构有一些工具或脚本来完成这项任务,这将对许多开发人员有所帮助。
我希望每个人都能理解我的问题。
【问题讨论】:
-
您似乎想要大规模记录并吞下异常。您可能有这样做的理由,但总的来说这不是一个好主意。一个好的策略是在调用堆栈的根部捕获异常并相应地对它们做出反应(包括日志记录)。这样做不需要太多的异常处理程序,您需要自动化来创建它们。
-
@MartinLiversage 不在全球范围内,我在说每种方法。我们写函数的地方。我说的是一些功能
-
好吧,我在这里闻到了异常反模式的味道,但也许有一些关于你的架构我不知道的地方? devlicio.us/blogs/billy_mccafferty/archive/2007/11/30/…
-
我们使用 3 层架构。我想要什么,每当我们在方法中用 try/catch 块包围时,我都需要放置日志记录代码。
标签: c# asp.net .net visual-studio try-catch