如何在 ASP.NET 中安全地进行 Debug.Assert?

发布于 2024-08-28 05:19:23 字数 734 浏览 6 评论 0原文

无法捕获断言。这很好,因为我不想将某些错误包含在 try/catch 中,至少不在开发服务器上。但断言似乎非常危险。如果他们投入生产,它可以使用 msgbox 挂起 ASP.NET 服务器。

//Don't want this on prod even if debug=true is in the web.config
#if DEBUG
    //A future client programmer can wrap this in a try{}catch{}
    if (!EverythingIsOkay)
        throw new InvalidOperationException("Dagnabbit, programming error");

    //This stops the but has less information that an
    // Exception and hangs the server if this accidentally 
    // runs on production
    System.Diagnostics.Debug.Assert(!EverythingIsOkay);
#endif

是否有更好的方法可以向开发人员传达违反不可侵犯条件的情况,而不会冒 IIS 挂起的风险?

更新:阅读第一个回复后,我想答案取决于一种万无一失的方法来检测代码何时在开发环境中运行以及何时在生产服务器上运行,或者弄清楚如何抛出无法捕获和忽略的异常。

Asserts can't be caught. This is good because some errors I don't want to be wrapped in try/catch, at least not on the development server. But Asserts seem awefully dangerous. If they get onto production, it can hang the ASP.NET server with a msgbox.

//Don't want this on prod even if debug=true is in the web.config
#if DEBUG
    //A future client programmer can wrap this in a try{}catch{}
    if (!EverythingIsOkay)
        throw new InvalidOperationException("Dagnabbit, programming error");

    //This stops the but has less information that an
    // Exception and hangs the server if this accidentally 
    // runs on production
    System.Diagnostics.Debug.Assert(!EverythingIsOkay);
#endif

Is there better way to communicate an violation of a inviolable condition to a developer without risking hanging IIS?

UPDATE: After reading the first replies, I guess the answer hinges on a foolproof way to detect when code is running in a development environment and when it is on a production server, or figuring out how to throw an exception that can't be caught and ignored.

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

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

发布评论

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

评论(4

薄荷梦 2024-09-04 05:19:23

我亲自创建了一个名为“Defense”的类,它有两种方法:“Assert”和“Fail”。 Assert 的工作方式与 xUnit“assert”类似,它接受一个布尔条件和一条消息,如果条件为 false,则抛出带有消息的异常。失败立即抛出异常。

它非常简单,并且多次拯救了我的tuches。它对 ASP.NET 友好并且很快就会消失。如果您担心现实世界中抛出这些错误,您可以修改 Assert 方法,以便它使用预处理指令(#if DEBUG ... #endif)进行记录,但在我的行中在工作中,我宁愿看到现实世界中的严重错误,也不愿隐藏它们而不知道发生了什么。

I personally created a class called "Defense" with two methods, "Assert" and "Fail." Assert works similarly to xUnit "assert" in that it takes a boolean condition and a message and throws an exception with the message if the condition is false. Fail throws the exception right away.

It's super-simple and has saved my tuches many, many times. It's ASP.NET friendly and dies hard and fast. If you're concerned about these errors being thrown in the real world you can modify the Assert method so it logs instead using preprocessing directives (#if DEBUG ... #endif), but in my line of work I'd rather see hard errors in the real world than hide them and not know what happened.

束缚m 2024-09-04 05:19:23

没有真正看到问题所在。像第一个一样并抛出异常。那么服务器就不会挂起。另外,您不应该在生产中使用 debug.Assert (正如您所提到的)。

抛出异常也会停止执行,您将能够捕获它。只需确保记录异常即可进行调试。

Don't really see the problem. Do like the first one and throw an exception. Then the server wont hang. Also you should never use debug.Assert in production(as you mentioned).

Throwing an exception will stop the execution aswell and you will be able to catch it. Just make sure that you log the exception and you will be fine with debugging.

蒗幽 2024-09-04 05:19:23

您是否可以使用日志记录工具(即 log4net)或实时消息泵(TCP 套接字或数据库)来记录“自定义”断言的结果,而不是使用诊断Debug.Assert?当断言失败时你想停止执行吗?

Rather than using the Diagnostics Debug.Assert, could you use a logging tool (i.e. log4net) or a realtime message pump (TCP socket or database) to log the results of a "custom" Assert? Are you wanting to stop executing when the Assert fails?

我们的影子 2024-09-04 05:19:23

好吧,你不能让一切万无一失,傻瓜太聪明了;)

检测你是否在生产中取决于你,你可以利用服务器的命名约定并检查Environment.MachineName(如果你的网络允许这样做) ),或者使用 web.config 中的 appSetting。

我不确定 Debug.Assert() 是否会挂起服务器,您是否在开发服务器上尝试过? MSDN 说 Assert 仅在用户界面模式下显示消息框 - 没有指定否则会发生什么。

要终止您的 asp.net 页面,您可以尝试 Response.Redirect 到程序员的错误页面,或者通过“

  Response.Clear()
  Response.Write("Fool! You made a dagnabbit programming error!");
  Response.End();

这应该终止您的页面”手动执行此操作,除非有人使用 try/catch(Exception ex) 反模式。

Well, you can't make everything foolproof, fools are much too ingenious ;)

Detecting if you are in production is up to you, you can exploit naming conventions for servers and check Environment.MachineName (if your web is is allowed to do so), or use an appSetting in your web.config.

I am not sure if Debug.Assert() would hang the Server, did you try that on your development server? MSDN says that Assert shows a message box only when in user interface mode - without specifying what happens otherwise.

To kill your asp.net page, you could try a Response.Redirect to a Programmer's Error Page, or do it manually by

  Response.Clear()
  Response.Write("Fool! You made a dagnabbit programming error!");
  Response.End();

This should kill your page, unless somebody uses the try/catch(Exception ex) antipattern.

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