如何使用 ELMAH 手动记录错误
是否可以使用 ELMAH 执行以下操作?
logger.Log(" something");
我正在做这样的事情:
try
{
// Code that might throw an exception
}
catch(Exception ex)
{
// I need to log error here...
}
ELMAH 不会自动记录此异常,因为它已被处理。
Is it possible to do the following using ELMAH?
logger.Log(" something");
I'm doing something like this:
try
{
// Code that might throw an exception
}
catch(Exception ex)
{
// I need to log error here...
}
This exception will not be automatically logged by ELMAH, because it was handled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
直接日志写入方法,从ELMAH 1.0开始工作:
ELMAH 1.2引入了更灵活的API:
两种解决方案之间存在差异:
Raise
方法将ELMAH过滤规则应用于异常。Log
方法没有。Direct log writing method, working since ELMAH 1.0:
ELMAH 1.2 introduces a more flexible API:
There is a difference between the two solutions:
Raise
method applies ELMAH filtering rules to the exception.Log
method does not.Raise
is subscription based and is able to log one exception into the several loggers.我建议将对 Elmah 的调用包装在您自己的简单包装类中。
然后只要需要记录错误就调用它。
这样做有以下好处:
注意:我添加了一个“contextualMessage”属性来获取上下文信息。如果您愿意,可以忽略它,但我发现它非常有用。 Elmah 会自动解开异常,因此底层异常仍会在日志中报告,但当您单击它时 contextualMessage 将可见。
I'd recommend wrapping the call to Elmah in a simple wrapper class of your own.
Then just call it whenever you need to log an error.
This has the following benefits:
Note: I've added a 'contextualMessage' property for contextual information. You can omit this if you prefer but I find it very useful. Elmah automatically unwraps exceptions so the underlying exception will still be reported in the log but the contextualMessage will be visible when you click on it.
您可以使用 Elmah.ErrorSignal() 方法来记录问题而不引发异常。
You can use the Elmah.ErrorSignal() method to log an issue without raising an exception.
我使用 ASP.NET core,并使用 ElmahCore。
要使用 HttpContext(在控制器中)手动记录错误,只需编写:
在应用程序的另一部分不使用 HttpContext:
I am on ASP.NET core and I use ElmahCore.
To manually log errors with HttpContext (in controller) simply write:
In another part of your application without HttpContext:
是的,这是可能的。 ELMAH 旨在拦截未处理的异常。但是,您可以通过 ErrorSignal 类向 ELMAH 发出异常信号。这些异常不会抛出(不会冒泡),而只会发送到 ELMAH(以及 ErrorSignal 类的 Raise 事件的订阅者)。
一个小例子:
Yes, it is possible. ELMAH was designed to intercept unhandled exceptions. However you can signal an exception to ELMAH via the ErrorSignal class. Those exceptions are not thrown (don't bubble up), but are only sent out to ELMAH (and to subscribers of the Raise event of the ErrorSignal class).
A small example:
我希望在一个线程中做同样的事情,我已经开始从 MVC4 应用程序中对邮件进行排队,因此当引发异常时我没有可用的 HttpContext。为此,我最终根据此问题和此处找到的另一个答案得到了以下结果: elmah:没有 HttpContext 的异常?
在配置文件中我指定了一个应用程序名称:
然后在代码中(如上面提供的答案,但没有 HttpContext)你可以通过null 而不是 HttpContext:
I was looking to do this same thing in a thread I had started to queue mail from within my MVC4 application, as such I did not have the HttpContext available when an exception was raised. To do this I ended up with the following based on this question and another answer found on here: elmah: exceptions without HttpContext?
In the config file I specified an application name:
Then in code (like the answer provided above, but without the HttpContext) you can pass null instead of an HttpContext:
有时
CurrentHttpContext
可能不可用。定义
使用
Sometimes
CurrentHttpContext
may not be available.Define
Use
我试图使用 Signal.FromCurrentContext().Raise(ex); 将自定义消息写入 elmah 日志中并发现这些异常被冒出来,例如:
此外,我不明白 elmah 如何支持不同级别的日志记录 - 是否可以通过 web.config 设置关闭详细日志记录?
I was trying to write custom messages into elmah logs using Signal.FromCurrentContext().Raise(ex); and found that these exceptions are bubbled up, eg:
Besides I don't see how elmah supports different levels of logging - is it possible to switch off verbose logging by a web.config setting?
使用了这条线,效果非常好。
Used this line and it works perfectly fine.