记录用户定义的异常 C#
您好,我想将用户定义的异常写入日志文件。 因此,我不想抛出异常,而是想将该消息记录到 txt 文件中。
我的异常的构造函数如下所示:
public OpenFileException(string pathToOpen, Exception innerexception)
: base("Couldn't find the path: " + pathToOpen, innerexception)
{
this.pathToOpen = pathToOpen;
}
这就是我目前记录异常的方式:
try
{
string data = Read(txtLocation.Text);
txtInfo.Text = data;
}
catch (Exception ex)
{
WriteLog("[" + DateTime.Now + "]" + " " + ex.Message);
MessageBox.Show(" ");
throw new OpenFileException(txtLocation.Text, ex);
}
所以我要问的是。如何将字符串“无法找到路径:”记录到 txt 文件?
Hello I would like to write my userdefined exception to a log file.
So Instead of throwing my exception I would like to log that message into a txt file.
The constructor for my exception looks like this:
public OpenFileException(string pathToOpen, Exception innerexception)
: base("Couldn't find the path: " + pathToOpen, innerexception)
{
this.pathToOpen = pathToOpen;
}
This is how I am logging my exception at the moment:
try
{
string data = Read(txtLocation.Text);
txtInfo.Text = data;
}
catch (Exception ex)
{
WriteLog("[" + DateTime.Now + "]" + " " + ex.Message);
MessageBox.Show(" ");
throw new OpenFileException(txtLocation.Text, ex);
}
So what I'm asking is. How can I log my string "Couldn't find the path: " to a txt file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自框架设计指南:
然后,您只需调用
log.Error(exception)
,它将按照您编写ToString()
的方式进行记录,无需额外操作。From Framework Desing Guidelines:
Then you can just call
log.Error(exception)
and it will be logged just the way you wroteToString()
without extra actions.我通常会在正常的 try/catch 之外捕获并记录用户定义的异常
您正在创建一个用户定义的异常,以便您可以以不同的方式处理它
I would normally catch and log the user defined exception outside the normal try/catch
You are creating a user defined exception so you can handle it differently
看起来有点大材小用,为什么不使用 Log4Net 并让它根据 app.config 中的配置来写入文件或向您发送电子邮件?
基本上,您可以毫不费力地获得所有您想要的东西,然后您可以专注于最重要的事情。
即使您决定保留当前的逻辑,我无论如何都会创建一个 Logger 类来完成所有操作,而不必在应用程序的每个 catch 块中指定 DateTime.Now 和其他魔法。
It looks a bit overkilling, why don't you use Log4Net and let it write files or send you emails depending on its configuration in the app.config?
basically you get all what you can want out of the box with no effort, then you can concentrate on what matters the most.
even if you decide to keep your current logic, I would anyway create a Logger class which does everything instead of having to specify DateTime.Now and other magic in every single catch block of your application.
您可以使用 log4net http://logging.apache.org/log4net/ 或 NLog http://nlog-project.org/wiki/Documentation 都值得努力即使在简单的应用程序中也能学习和使用。
You can use, instead of reinvent the wheel, log4net http://logging.apache.org/log4net/ or NLog http://nlog-project.org/wiki/Documentation both worth the effort to learn and use even in simple applications.
您需要找到一种方法在找不到文件时抛出异常。
我认为这不是一个好主意,因为 .NET 已经抛出了
FileNotFoundException
当找不到文件时。您应该抓住这一点并以这种方式记录消息。当异常类型已经存在时,不要创建新的异常类型。
(请原谅我格式上的特殊之处)
You need to find a way to get your exception thrown when a file is not found.
I don't think this is a good idea, because .NET already throws the
FileNotFoundException
when a file is not found. You should catch that and log the message that way.Don't make a new exception type when one already exists.
(Forgive the idiosyncrasies in my formatting)