在 WebMethod 中序列化 MethodBase 和 Exception 类型

发布于 2024-10-01 09:22:12 字数 696 浏览 0 评论 0原文

我们正在尝试创建一个简单易用的 Web 服务来记录异常和消息。

理想情况下,Web 方法声明应如下所示,

public int LogException(MethodBase methodBase, Exception exception)
public int LogMessage(MethodBase methodBase, string message, string data)

但这不起作用,因为当您添加对 Web 服务的引用时,它会创建自己的类,并且 System.Reflection.MethodBase 与 LogService.MethodBase 的类型不同。 MethodBase 或 Exception 都不能很好地序列化。我也希望例外有它的数据值。

您将如何将这两个对象传递给网络方法?

我们希望只传递这两个对象的原因是,在它们之间,我们可以获得跟踪错误所需的 99% 的数据。它们包含堆栈跟踪信息、方法名称和它们所在的类等等...通过向 Exception.Data 添加一些信息,您可以在调用时获得参数值。而且,这简化了对其他开发人员的调用,并希望使他们更有可能使用通用记录器,而不是总是滚动自己的记录器。目前,这被证明是很麻烦的,因为每个记录器都是不同的,而且大多数记录器似乎没有包含追踪错误所需的所有详细信息。

通过这样做,我们可以标准化返回的值和电子邮件格式,并通过更改一项服务来更改所有应用程序日志记录。

We're trying to create a simple to use webservice for logging exceptions and messages.

Ideally the web methods declaration would look like this

public int LogException(MethodBase methodBase, Exception exception)
public int LogMessage(MethodBase methodBase, string message, string data)

But that doesn't work, since when you add a reference to a webservice it creates it's own classes and System.Reflection.MethodBase is not the same type as LogService.MethodBase. And neither MethodBase or Exception serialize nicely. And I also want exception to have it's Data values.

How would you go about passing those two objects to a webmethod?

The reason we were hoping to just pass those two objects is, between them, we can get 99% of the data needed to trace an error. They contain stack trace info, methodnames and the class they are in, etc, etc... And by adding some information to Exception.Data, you can have the parameters values at the time of the call. And, this simplifies the call for other devs, and hopefully makes them more likely to use a common logger, rather than always rolling their own. Which currently, is proving to be troublesome as each logger is different, and most don't seem to include ALL the details necessary to track down an error.

By doing this, we can standardize the values returned, and email format, and make changes to all apps logging by changing one service.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡忘如思 2024-10-08 09:22:12

请不要这样做。

您不应使用 Web 服务来传递特定于平台的数据。 MethodBase 等是特定于 .NET 的,并且 Web 服务基础设施并不旨在容纳它。

为什么需要 Web 服务来进行日志记录?请改用类库。您不希望您的日志记录因 Web 服务问题而失败。

最后,如果您必须执行此操作,请不要使用 ASMX Web 服务来执行此操作 - 使用 WCF,这将允许您在客户端和服务上使用相同的类型。

也许更重要的是,WCF将允许您使用MSMQ作为传输,这样您就可以使用可靠的队列来确保您的日志消息最终被记录下来。

我仍然怀疑您是否真的想发送 MethodBase - 相反,发送 MethodBase 中的数据。您将如何处理 .NET 版本依赖性?

Please don't do this.

You should not use web services to pass platform-specific data. MethodBase etc are specific to .NET, and the web service infrastructure is not intended to accommodate it.

Why do you want a web service to do logging? Use a class library instead. You don't want your logging to fail because of problems with a web service.

Finally, if you must do this, don't use ASMX web services to do it - use WCF, which will allow you to use the same type on both the client and the service.

Perhaps more importantly, WCF will allow you to use MSMQ as a transport, so that you can use reliable queues to ensure that your log messages are eventually logged.

I still doubt that you really want to send the MethodBase - send the data that's in the MethodBase, instead. What will you do about .NET version dependencies?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文