1

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433

5 Answers5

0

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.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • I'm not using log4net because I want to learn how to do it. :) that's the reason I am trying to write this. I will try to make the logger class, but how do I log the string from my user-defined exception? –  Feb 04 '11 at 10:51
0

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.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
0

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

djeeg
  • 6,685
  • 3
  • 25
  • 28
0

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)

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • Yea, you are right, it seemse waste of time to create an exception that is in .NET. Just wanted to try, to make an exception by my self, and that was what, I could come up with :) –  Feb 04 '11 at 11:31
  • @Kenneth Andersen: Fair enough. I didn't realise this was an experiment :) – Matt Ellen Feb 07 '11 at 20:18
  • It may be a good idea to have custom exceptions for failing to open various files, if the intended course of action will vary depending upon which file didn't open. For example, suppose opening a media file requires a CODEC to be loaded as well. Would it be helpful or unhelpful to have both file-open attempts throw FileNotFoundException on failure? Would it be more or less helpful to have one report MediaFileOpeningException and the other CodecLoadingException? – supercat Mar 22 '11 at 20:27
0

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.

Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44