如何处理 SharePoint 异常?
我是 SharePoint 新手,所以我想我需要如何处理异常? 当我编写自定义代码时,我是否必须检查它们,或者如果抛出它们,它们会自动记录并且不会破坏应用程序?
如果没有,那么我如何记录它们?
谢谢你!
编辑:我应该如何记录这些异常?
I`m new to SharePoint, so I guess how do I need to handle exceptions? When I write custom code do I have to check for them or maybe, if they are thrown, they automatically get logged and don't break the app?
If not, then how do I log them?
Thank you!
Edit: And how should I log those exceptions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用此写入日志: http://msdn.microsoft.com/ en-us/library/aa979522.aspx
除非您处理异常,否则应用程序很可能会崩溃。 某些错误(例如事件接收器中生成的错误)不会“破坏”应用程序(终止事件接收器除外)。 除非您更改了中央管理中的设置,否则这些错误以及所有其他未捕获的错误将最终记录在日志中。
Use this to write to the log: http://msdn.microsoft.com/en-us/library/aa979522.aspx
Unless you handle your exceptions the application will most likely break. Some errors such as those generated in event receivers will not "break" the application (except for terminating the event receiver). Those, along with all other uncatched errors will end up in the log unless you've changed the settings in central admin.
异常的一般规则是你应该只捕获你知道如何正确处理自己的异常 - 我不知道 sharepoint 本身是否记录未处理的异常,但我认为它将未处理的异常转换为各种 Web 服务中适当的 SOAP 异常,因此很有可能您不会使整个应用程序崩溃。
The general rule with exceptions is you should only catch the exceptions you know how to handle yourself correctly - I don't know if sharepoint logs unhandled exceptions itself, but I think it converts unhandled exceptions into the appropriate SOAP exception in the various web services so it's a good bet that you won't crash the whole app.
我们允许所有异常冒泡,并使用自定义 HTTP 模块来处理所有异常。 从这里,我们记录到 ULS 日志和跟踪日志。 这主要来自 SharePoint MVP Chris O'brien 的代码
仅当我们认为可以为异常添加额外值时,我们才会捕获异常,然后重新抛出错误,允许 http 模块拾取它。
We allow all our exceptions to bubble up and use a custom HTTP module to handle all exceptions. From here, we log to the ULS logs and also to the trace logs. This is mostly lifted from SharePoint MVP Chris O'brien's Code
We only catch exceptions if we feel we can add extra value to the exception, and then re-throw the error, allowing the http module to pick it up.
我假设您正在使用 SharePoint 对象模型,而不是它的 Web 服务,因为您没有说明是哪个,并且通常使用对象模型,因为它提供了更多功能。
SharePoint 使用自己的自定义 SPException 类来引发异常以及几个标准的 .NET 异常类。 不幸的是,SDK 文档没有提及哪些方法会引发异常以及何时可能发生这种情况。 您自己的经验和 Reflector 最适合了解这一点。
第一次学习时,您可能会经常引发异常,并且很难预测何时以及为何会发生这种情况。 SPException 引发的异常通常会提供有用的消息,但是 SharePoint 也引发的标准 .NET 异常(例如 ArgumentOutOfRangeException)只提供很少的详细信息。 因此,为了帮助调试,通常最好使方法简短明了,以便很容易找到可能发生异常的原因。 您只需要检查它们是否有充分的理由让它们冒泡,除非您可以重新抛出并添加更多详细信息(Peter 建议)是确保正确性并保持代码整洁的好方法。
SharePoint 通常不会自动记录异常。 有时您会在ULS(又名跟踪日志)文件中找到它们但通常在记录时,它们不会提供比您自己可以捕获的异常更多的信息。 正如此处已经提到的,有许多选项可用于设置您自己的日志记录。 您还可以在 SharePoint Dev Wiki 中找到很棒的列表。
I'm going to assume you are using the SharePoint object model and not its web services as you haven't stated which, and the object model is typically used as it provides more functionality.
SharePoint uses its own custom SPException class to throw exceptions as well as several standard .NET exception classes. Unfortunately the SDK documentation does not mention which methods throw exceptions and when this might happen. Your own experiences and Reflector are best for learning this.
When first learning you will probably cause exceptions to be thrown quite often and it can be quite unpredictable to know when and why this is happening. Those thrown by SPException generally have helpful messages however the standard .NET exceptions that SharePoint also throws (such as ArgumentOutOfRangeException) give very little detail. Therefore to help debugging it is generally a good idea to keep your methods short and to the point so it's easy to find why an exception might be occurring. You only need to check for them if there is a good reason to so letting them bubble up unless you can rethrow and add more detail (suggested by Peter) is a good approach to ensure correctness and keep your code clean.
SharePoint usually does not automatically log exceptions. Sometimes you will find them in the ULS (a.k.a. Trace Log) files but generally when logged they don't give any more information than the exceptions you can catch yourself. There are many options for setting up your own logging as already mentioned here. You can also find a great list at SharePoint Dev Wiki.