locked
Error logging to notepad RRS feed

  • Question

  • we have developed an application with error logging to the database.

    I, now wish to log the error that occurs to a notepad instead of the database.

    Please throw some light on how to do the error logging to notepad.

    Thursday, April 28, 2011 1:04 PM

Answers

  • It should be pretty simple.

    Use the FileStream and StreamWrite classes.

    It's even more simple than that, actually.

    I am a big fan of simplicity, you don't need to create the streams manually. The File.AppendAllText method not only handles the streams internally for you it has the added bonus of not overwriting any previous error logging. This method will APPEND to the file if it already exists. Also, it's a one line solution, which is quite nice.

          string errorMessage = "An error occured!";
    
          File.AppendAllText("c:\\log.txt", errorMessage);
    

    HTH


    -Scosby
    Microsoft Community Contributor
    Thursday, April 28, 2011 6:28 PM
  • Hi Lakshmi:

    At first you can write the error detail into a text file using FileStrim / If you want to do some more then I would suggest

    to use log4net api, For more information can be found at the link below:

    http://www.codeproject.com/KB/trace/log4net_intro.aspx


    Thanks
    Md. Marufuzzaman
    Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
    I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
    Thursday, April 28, 2011 2:15 PM
  • It should be pretty simple.

    Use the FileStream and StreamWrite classes.

    FileStream fs = new FileStream( @"C:\Myfolder\Myfile.txt", FileMode.CreateNew, FileAccess.Write);

    StreamWriter sr = new StreamWriter(fs);

    sr.Write(errorText);

    sr.close();

    fs.close();

    Note:

    1) Change the file path to anything you wish for.

    2) Change the mode to FileMode.Create - in case you want to handle an exception and show if a file exists.

    Hope it helps.

    Thursday, April 28, 2011 6:18 PM
  • Please take a look at this thread, I write a method to write the passed in error or exception info into a log file, here a txt file is used, you can change to use notepad:
    http://social.msdn.microsoft.com/Forums/en-ZA/csharpgeneral/thread/bf56581d-d8fc-4b2b-a1cf-f2dc3df306df


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 4, 2011 7:58 AM
    Moderator

All replies

  • how do you mean logging the error to notepad?  Do you mean just logging the error to a file?

    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    Thursday, April 28, 2011 1:39 PM
  • Hi Lakshmi:

    At first you can write the error detail into a text file using FileStrim / If you want to do some more then I would suggest

    to use log4net api, For more information can be found at the link below:

    http://www.codeproject.com/KB/trace/log4net_intro.aspx


    Thanks
    Md. Marufuzzaman
    Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
    I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
    Thursday, April 28, 2011 2:15 PM
  • Hi,

    The application that we have developed has error logging functionality that saves the error to database.

    But am in need of an error logging functionality that logs error to a notepad.

    Somebody plz throw some light on this issue.


    Harish
    • Moved by CoolDadTxModerator Thursday, April 28, 2011 5:40 PM Not IDE related (From:Visual C# IDE)
    • Merged by Aspen VJ Tuesday, May 3, 2011 2:24 AM duplicated
    Thursday, April 28, 2011 5:40 PM
  • It should be pretty simple.

    Use the FileStream and StreamWrite classes.

    FileStream fs = new FileStream( @"C:\Myfolder\Myfile.txt", FileMode.CreateNew, FileAccess.Write);

    StreamWriter sr = new StreamWriter(fs);

    sr.Write(errorText);

    sr.close();

    fs.close();

    Note:

    1) Change the file path to anything you wish for.

    2) Change the mode to FileMode.Create - in case you want to handle an exception and show if a file exists.

    Hope it helps.

    Thursday, April 28, 2011 6:18 PM
  • It should be pretty simple.

    Use the FileStream and StreamWrite classes.

    It's even more simple than that, actually.

    I am a big fan of simplicity, you don't need to create the streams manually. The File.AppendAllText method not only handles the streams internally for you it has the added bonus of not overwriting any previous error logging. This method will APPEND to the file if it already exists. Also, it's a one line solution, which is quite nice.

          string errorMessage = "An error occured!";
    
          File.AppendAllText("c:\\log.txt", errorMessage);
    

    HTH


    -Scosby
    Microsoft Community Contributor
    Thursday, April 28, 2011 6:28 PM
  • On 4/28/2011 1:40 PM, Lakshmi Narayanan M wrote:
    > Hi,
    >
    > The application that we have developed has error logging functionality
    > that saves the error to database.
    >
    > But am in need of an error logging functionality that logs error to a
    > notepad.
    >
    > Somebody plz throw some light on this issue.
    >
     
    There is no light. The only thing you can to is write text to a text
    file. If you want the messages to accumulate in the text file, then use
    the 'append'. You can use notepad to open the text file and look at the
    messages.
     
    Thursday, April 28, 2011 6:40 PM
  • Awesome Scosby. I never knew of File class directly having a static function :)
    Thursday, April 28, 2011 8:22 PM
  • The only drawback I can see to this is that this encompasses the following operations:

    1. Open file stream, 2. Seek to end. 3. Write data to stream, 4. Flush buffers, 5. Close stream.

    The time this process takes to complete involves the overhead of opening and closing the file and more importantly, the time that it takes to seek to the end of the file, which will likely increase the longer the file becomes.  In a logging operation (where many messages may be written to the file), it may be advantageous to open the stream and seek to the end manually, then leave the stream open and use writes until such time as the file should be closed (for example, before the application exits).  It all depends on what performance hit you are willing to take and the needs of your logging process.

    Thursday, April 28, 2011 9:04 PM
  • Additional information.

    Read http://msdn.microsoft.com/en-us/library/ms143356.aspx#Y1100.

     

    Regards,

    Eric


    Failure is not the worst thing in the world. The very worst is not to try.
    Friday, April 29, 2011 1:42 AM
  • http://www.aspfree.com/c/a/C-Sharp/C-Sharp-StreamReader-and-StreamWriter-Explained/1/
    Please mark the replies as answers if they help and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Friday, April 29, 2011 8:13 AM
  • Hi LakshmiNarayanan,

    Here is a duplicated case.
    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/dd2b2751-84ea-44d1-9e58-358777c68f4e

    I will merge them together. Thanks for understanding.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 3, 2011 2:24 AM
  • Please take a look at this thread, I write a method to write the passed in error or exception info into a log file, here a txt file is used, you can change to use notepad:
    http://social.msdn.microsoft.com/Forums/en-ZA/csharpgeneral/thread/bf56581d-d8fc-4b2b-a1cf-f2dc3df306df


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 4, 2011 7:58 AM
    Moderator