在控制台应用程序中使用 ELMAH
我刚刚开始使用 ELMAH,并且是它的粉丝。 我的团队支持大量 Web 应用程序,令我特别兴奋的是 ELMAH 允许我们将每个应用程序的异常保存到同一个 MS SQL 数据库表中。
我们还支持一些控制台、DLL 和桌面应用程序。 是否可以使用 ELMAH DLL 将这些应用程序中的异常记录到同一位置?
I just started using ELMAH and am a fan. My team supports a large number of web applications and I'm particularly excited that ELMAH lets us save exceptions from each application to the same MS SQL database table.
We also support a few console, DLL and desktop applications. Is it possible to use the ELMAH DLL to log exceptions in these apps to that same location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
除了 ASP.NET 站点之外,我们还需要能够从控制台应用程序和 Windows 服务进行登录。 我使用了答案(
ErrorLog.GetDefault(null);
),该答案运行良好,直到我也需要发送电子邮件为止。所以,这是我的解决方案。 它处理日志、电子邮件、推文和过滤(在配置文件和代码中)。 我还将主调用包装为 Exception 的扩展,因此可以像这样调用它:
catch(Exception ex) { ex.LogToElmah(); 代码
要在代码中进行过滤,请挂钩相应的 .Filtering 事件:
ElmahExtension.ErrorLog.Filtering += new ExceptionFilterEventHandler(ErrorLog_Filtering);
:
此外,您还需要添加对项目中的
System.Web.dll
才能正常工作。编辑:根据评论,仅当您的配置文件具有<,请参阅此代码片段 /code> 在您的配置文件中(当 HttpContext.Current 可用时使用)。
时,此代码才会发送电子邮件。 如果您想保留We needed the ability to log from a console app and a windows service in addition to our ASP.NET site. I used the answer (
ErrorLog.GetDefault(null);
) which worked well until I needed to email too.So, here is my solution. It handles the log, email, tweet and filtering (both in the config file and in code). I have also wrapped the main call as an extension to Exception so it can be called like:
catch(Exception ex) { ex.LogToElmah(); }
To filter in code, hook the corresponding .Filtering event:
ElmahExtension.ErrorLog.Filtering += new ExceptionFilterEventHandler(ErrorLog_Filtering);
Code:
In addition, you will need to add a reference to the
System.Web.dll
in your project for this to work.EDIT: As per the comments, this code will send emails only if your config file has
<errorMail async="false"/>
. Refer to this code snippet should you want to keep<errorMail async="true"/>
in your config file (to be used when HttpContext.Current is available).我们这里的情况完全相同。 为我们所有的 Web 应用程序运行 ELMAH。 其中一些有基于控制台的调度程序。
在深入研究了源代码之后,以下代码似乎可以工作:
上述代码唯一真正的问题是您需要将应用程序名称保留在配置中的某个位置,以便能够在 ELMAH.axd 查看器上查看条目。
因此,在我们的通用错误处理代码中,我们这样做:
We have exactly the same situation here. Running ELMAH for all our web applications. A few of them have console based schedulers.
After doing some digging through the source code, the following code seems to work:
The only real problem with the above is that you need to keep the application name somewhere in your config to be able to see the entries on the ELMAH.axd viewer.
So in our generic error handling code we do:
如果您只想通过电子邮件发送日志而不使用 http,您可以这样做:
并根据
app.config和
您选择的
smtp 设置。 全做完了。 :-)
If you just want to email the log without http you can do like this:
And in terms of app.config
And
And smtp settings of your choice.
All done. :-)
编辑:这可以可以完成 - 请参阅此答案。
我很确定你不能这样做。 我会尝试挖掘相关材料。
http://groups. google.com/group/elmah/browse_thread/thread/f214c4f782dc2bf4/d96fe43b60765f0c?lnk=gst&q=app#d96fe43b60765f0c
因此,从我在 Google 群组中搜索到的内容来看,这是不可能的...因为 ELMAH 有效脱离 HttpHandlers(一种 ASP.NET 构造),它仅适用于 ASP.NET。
话虽如此,您可以通过多种方式在控制台应用程序上使用它。 ELMAH 提供了一种引发错误的方法,因此您可以将 ELMAH 包装在异常处理中,然后通过以下方式发出错误信号:
这意味着将整个应用程序包装在异常处理程序和信号中。 你可能需要一些调整才能把它记下来,但我认为这是完全可能的。
如果您需要,这是 ELMAH 的链接代码存储库。
Edit: This CAN be done - See this answer.
I'm pretty sure you can't do this. I'll try and dig up the relevant material.
http://groups.google.com/group/elmah/browse_thread/thread/f214c4f782dc2bf4/d96fe43b60765f0c?lnk=gst&q=app#d96fe43b60765f0c
So from what I can find searching the Google group is that it's not possible... Since ELMAH works off of HttpHandlers (an asp.net construct) it is ASP.NET only.
With that said, there are ways that you could utilize it on a console application. ELMAH provides a method to raise errors, so you could wrap ELMAH in your exception handling and then signal an error via:
This would mean wrapping your entire application in an exception handler and signaling. It might take you some tweaking to get it down, but I think it's totally possible.
In case you require it, this is the link to the ELMAH code repository.
对于那些需要将 Brian Chance 的答案移植到 VB.NET 的人:
但是,对于仅将错误记录到数据库,这就足够了:
作为完整的解决方案:
则它将起作用
如果在 Initialize() 之后调用它,
For those that need Brian Chance's answer ported to VB.NET:
However, for just logging errors to the database, this will be sufficient:
As a complete solution:
And
will work if it is called after Initialize()
好吧,因为我无法发表评论,所以我会将其发布在这里,也许有人会看到它。
在遵循 Brian 的方法和评论者之后,我能够使电子邮件正常工作,但我仍然没有看到 SQL 消息被记录,即使我已经设置了 applicationName。 我没有意识到它们实际上正在被记录,我只是没有看到它们,因为 applicationName 必须与您的 web.config 相同才能查看它。
我的 web.config 没有指定 applicationName,因此它默认为“/LM/W3SVC/2/ROOT”,这基本上是“asgeo1”评论的内容,尽管我没有意识到它必须相同。
因为我并没有真正遇到任何我担心的错误,所以我在 web.config 和 app.config 中将 applicationName 配置为相同,现在一切都显示得像冠军一样。
Well, since I can't comment I'll post this down here and maybe someone will see it.
After following Brian's method and the commentors I was able to get email working but I still wasn't seeing the SQL messages being logged, even though I had set the applicationName. What I didn't realize is that they were actually being logged I just wasn't seeing them because the applicationName must be the same as your web.config in order to be able to view it.
My web.config didn't have applicationName specified, so it was defaulting to "/LM/W3SVC/2/ROOT", which is basically what "asgeo1" commented, though I didn't realize it had to be the same.
Since I didn't really have any errors I was concerned with, I configured applicationName in my web.config and my app.config to be the same and now everything shows up like a champ.
ELMAH 代表错误日志模块和处理程序 - 当然指的是
IHttpModule
和IHttpHandler
。控制台应用程序不使用 HTTP,因此通常无法从为 HTTP 构建的模块和处理程序中获得太多好处。
ELMAH stands for Error Logging Modules and Handlers - referring, of course, to
IHttpModule
andIHttpHandler
.Console applications do not use HTTP, so would typically not be able to benefit much from Modules and Handlers built for HTTP.