记录用户定义的异常 C#

发布于 2024-10-16 00:45:34 字数 641 浏览 4 评论 0原文

您好,我想将用户定义的异常写入日志文件。 因此,我不想抛出异常,而是想将该消息记录到 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 技术交流群。

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

发布评论

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

评论(5

难得心□动 2024-10-23 00:45:35

来自框架设计指南:

当您的异常提供额外属性时,请重写 ToString。

然后,您只需调用 log.Error(exception) ,它将按照您编写 ToString() 的方式进行记录,无需额外操作。

From Framework Desing Guidelines:

Do override ToString when your exception provides extra properties.

Then you can just call log.Error(exception) and it will be logged just the way you wrote ToString() without extra actions.

有深☉意 2024-10-23 00:45:34

我通常会在正常的 try/catch 之外捕获并记录用户定义的异常

try {
    try {
        string data = Read(txtLocation.Text);
        txtInfo.Text = data;
    } catch (Exception ex) {
        throw new OpenFileException(txtLocation.Text, ex);
    }

        ....

} catch(OpenFileException ex) { 
    WriteLog("[" + DateTime.Now + "]" + " " + ex.Message);
    MessageBox.Show(" ");   
} catch(Exception ex) { 
    WriteLog("[" + DateTime.Now + "]" + " " + ex.Message);
    MessageBox.Show(" ");   
}

您正在创建一个用户定义的异常,以便您可以以不同的方式处理它

I would normally catch and log the user defined exception outside the normal try/catch

try {
    try {
        string data = Read(txtLocation.Text);
        txtInfo.Text = data;
    } catch (Exception ex) {
        throw new OpenFileException(txtLocation.Text, ex);
    }

        ....

} catch(OpenFileException ex) { 
    WriteLog("[" + DateTime.Now + "]" + " " + ex.Message);
    MessageBox.Show(" ");   
} catch(Exception ex) { 
    WriteLog("[" + DateTime.Now + "]" + " " + ex.Message);
    MessageBox.Show(" ");   
}

You are creating a user defined exception so you can handle it differently

泪眸﹌ 2024-10-23 00:45:34

看起来有点大材小用,为什么不使用 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.

花期渐远 2024-10-23 00:45:34

您可以使用 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.

蝶…霜飞 2024-10-23 00:45:34

您需要找到一种方法在找不到文件时抛出异常。

我认为这不是一个好主意,因为 .NET 已经抛出了 FileNotFoundException 当找不到文件时。您应该抓住这一点并以这种方式记录消息。

try
{
    string data = Read(txtLocation.Text);
    txtInfo.Text = data;

}
catch (FileNotFoundException ex)
{
    string log = String.Format("[{0}] Couldn't find the path: {1}"
                              , DateTime.Now
                              , ex.FileName);
    WriteLog(log);
}

当异常类型已经存在时,不要创建新的异常类型。

(请原谅我格式上的特殊之处)

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.

try
{
    string data = Read(txtLocation.Text);
    txtInfo.Text = data;

}
catch (FileNotFoundException ex)
{
    string log = String.Format("[{0}] Couldn't find the path: {1}"
                              , DateTime.Now
                              , ex.FileName);
    WriteLog(log);
}

Don't make a new exception type when one already exists.

(Forgive the idiosyncrasies in my formatting)

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