控制台应用程序中的错误捕获 [英] Error trapping in Console Application

查看:73
本文介绍了控制台应用程序中的错误捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有以下结构,

MyApp-C#控制台应用程序
ClsMessage-C#类库
ClsLog-C#类库

我正在从MyApp调用ClsMessage类以显示消息. ClsMessage检查
如果应用程序类型是Windows,则显示消息框.
如果应用程序类型为控制台,则ClsMessage类将调用ClsLog类将消息写入日志文件.我正在使用log4Net生成日志.如果log4Net配置不正确,我想捕获错误.
当ClsMessage类调用ClsLog类时,在ClsLog构造函数中,我正在使用以下命令来配置log4Net.

log4net.Config.XmlConfigurator.Configure();

如果我在App.config文件中没有正确配置log4Net,则上面的行将生成错误并在控制台中显示该错误.但是我想捕获该错误并将该错误记录在事件日志中.
我在ClsLog类构造函数中使用以下代码.

My application has following structure,

MyApp - C# Console Application
ClsMessage - C# Class Library
ClsLog - C# Class Library

I am calling ClsMessage class from MyApp to display message. ClsMessage checks
If application type is Windows then it display message box.
If application type is console then ClsMessage class calls ClsLog class to write message into log file. I am using log4Net for generating log. I want to trap error if log4Net is not configured properly.
When ClsMessage class calls ClsLog class, in ClsLog constructor I am using the following command to confiture the log4Net.

log4net.Config.XmlConfigurator.Configure();

If I did''t properly confitured log4Net in App.config file, above line generate an error and display that error in console. But I want to trap that error and log that error in event log.
I am using the following code in ClsLog class constructor.

static Log()
{
    try
    {
       log4net.Config.XmlConfigurator.Configure();
    }
    catch (Exception ex)
    {
       throw ex;
    }
}


上面的构造函数无法捕获该异常.


The above constructor can''t catch the exception.

推荐答案

您的构造函数"具有try catch块,如果捕获到该异常,则将其重新引发.那么您认为错误在哪里呢?您会期望什么呢?

请解释!

修改:
如果log4net的配置失败,它将把有关该失败的输出写入控制台.因此,如果您仍然希望我们提供帮助,那么如果您修改(使用改进问题"链接)您的问题以包括转储到控制台的完整错误消息,那将是非常不错的.这很可能与您的配置文件有关.您仍然必须说明为什么要重新引发捕获的异常.
修改结束

问候,
Manfred
Your "constructor" has a try catch block and re-throws the exception if one was caught. So where do you think the mistake is? What would you have expected instead?

Please explain!

Modification:
If the configuration of log4net fails, it will write output about that failure to the console. So if you still want us to help us it would be really great if you modified (use "Improve Question" link) your question to include the full error message that is dumped to the console. This is most likely an issue with your configuration file. You still have to explain though why you are re-throwing the exception you caught.
End of modification

Regards,
Manfred


[正在添加Manfred的回复.]

如果您的问题是log4net在您有机会处理任何异常之前将错误记录到控制台,请检查它是否具有禁用控制台错误记录的任何方法.如果它不能让您做到这一点,将是非常令人惊讶的.而且它不是开源的吗?如果是这样,您还有修改源的最终选择,以防止发生此错误日志,尽管我认为您不太可能需要这样做.
[Adding to Manfred''s response.]

If your problem is that log4net logs errors to the console before you get a chance to handle any exceptions, then check to see if it has any means to disable console error logging. It would be quite surprising if it won''t let you do that. And isn''t it open source? If so you also have the final option of modifying the source to prevent this error logging from happening, although I''d think it would be very unlikely that you''d need to do that.


这篇关于控制台应用程序中的错误捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆