locked
exception handling RRS feed

  • Question

  • I need to take the exceptions from the try catch block and append them to a text file. Can't believe this is so hard??? Any ideas?
    www.helixpoint.com
    Wednesday, July 14, 2010 2:43 PM

Answers

  • Try
      ' Do something
    
    Catch ex As Exception
      Using sw As New System.IO.StreamWriter("C:\log.txt", True)
        sw.WriteLine(ex.Message)
        sw.Flush
      End Using
    End Try
    
    • Proposed as answer by Helen Zhou Wednesday, July 21, 2010 1:19 AM
    • Marked as answer by Helen Zhou Wednesday, July 21, 2010 9:30 AM
    Wednesday, July 14, 2010 2:53 PM
  • Hi Helixpoint,

                try
                {
                    int i = 0;
                    int k = 0;
                    int ooo = 0;
                    ooo = i / k;
                }
                catch (Exception ex)
                {
                    StreamWriter sw = new StreamWriter("E:\\exception.txt",true);
                    sw.WriteLine(ex.Message);
                    sw.Close();
                } 

    and in E:\excetion.txt shows "Attempted to divide by zero."

    Regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Helen Zhou Wednesday, July 21, 2010 1:19 AM
    • Marked as answer by Helen Zhou Wednesday, July 21, 2010 9:30 AM
    Tuesday, July 20, 2010 3:45 AM

All replies

  • Try
      ' Do something
    
    Catch ex As Exception
      Using sw As New System.IO.StreamWriter("C:\log.txt", True)
        sw.WriteLine(ex.Message)
        sw.Flush
      End Using
    End Try
    
    • Proposed as answer by Helen Zhou Wednesday, July 21, 2010 1:19 AM
    • Marked as answer by Helen Zhou Wednesday, July 21, 2010 9:30 AM
    Wednesday, July 14, 2010 2:53 PM
  • Hi Helixpoint,

                try
                {
                    int i = 0;
                    int k = 0;
                    int ooo = 0;
                    ooo = i / k;
                }
                catch (Exception ex)
                {
                    StreamWriter sw = new StreamWriter("E:\\exception.txt",true);
                    sw.WriteLine(ex.Message);
                    sw.Close();
                } 

    and in E:\excetion.txt shows "Attempted to divide by zero."

    Regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Helen Zhou Wednesday, July 21, 2010 1:19 AM
    • Marked as answer by Helen Zhou Wednesday, July 21, 2010 9:30 AM
    Tuesday, July 20, 2010 3:45 AM
  • PMFJI, Helen, but this is a pet peeve of mine:

    When showing an error to the User, by all means show them the Message.

    When logging it to a file for your own use later on, though, always, always , always record the entire Exception (obtained via ex.ToString()). 

    The message tells you what went wrong.
    The stack trace that comes with the whole Exception tells you where it went wrong, which is far more useful.  
    It would be even better if we could get hold of the arguments passed to each method to see what caused the problem, according to "my" principle that

    "Bad Code Breaks; Good Code gets broken - by Bad Data".

     


    Regards, Phill W.
    Tuesday, July 20, 2010 11:30 AM
  • Hello Helixpoint,

                   You can use the Microsoft Enterprise Library for inbuilt and reusable Logging functionality. Kindly, find the below lines for your understanding:

    Logging

    There are four basic concepts in the Logging and Instrumentation Application Block: log entries, tracers, sinks, and formatters.

    • Log entries allow you to record or write some information to a log sink.
    • A tracer event may be used to group log entries together. That is, a log entry may be part of a process.
    • A sink defines where and how log entries are recorded. A sink has many options: event log, MSMQ (Microsoft Message Queue), text file, WMI log, database entry (will need the database application block), and e-mail sent via SMTP.
    • A formatter is used to convert a log entry's information into a string.

    There are numerous classes contained in the Logging and Instrumentation Application Block. The following list contains a sampling of the classes:

    • LogEntry: Represents a log message. It contains the common properties that are required for all log messages.
    • LoggingException: Used to handle logging exceptions.
    • Logger: A facade for writing log entries to log sinks. It writes entries to the default sink unless otherwise specified.

    These classes are contained within the Microsoft.Practices.EnterpriseLibrary.Logging namespace. The application block includes additional namespaces for working with formatters, sinks, configuration, and so forth. The documentation included with the application block includes full details of each namespace.

    Logging sample

    With the basic concepts laid out, you can easily incorporate the application block in an application. The sample will only use the Logging namespace, so it must be referenced in your code. The following line makes it available in a C# application:

    usingMicrosoft.Practices.EnterpriseLibrary.Logging;

    And in VB.NET:

    Imports Microsoft.Practices.EnterpriseLibrary.Logging

    If you're using Visual Studio .NET, you should add a reference to the corresponding dll file (Microsoft.Practices.EnterpriseLibrary.Logging.dll). If you're not using Visual Studio, you can copy the necessary dll to the application's bin directory.

    The following C# snippet adds logging to an application. It results in entries in the default log sink: Windows Event Log.

    LogEntrylogEntry = new LogEntry();
    logEntry.Message = "Starting pplication";
    Logger.Write(logEntry);
    Logger.Write("Shutting down application");

    Here's the VB.NET equivalent:

    Dim logEntry As LogEntry
    logEntry = New LogEntry
    logEntry.Message = "Starting up the application"
    Logger.Write(logEntry)
    Logger.Write("Shutting down the application")

    Basically, the code writes to the log sink using two approaches. First, an instance of the LogEntry class is created. Its message property is populated—this appears with the entry in the log sink. The Write method of the Logger class is used to send the LogEntry object to the log sink. The final line uses a basic text message to make an entry in the log sink (event log in this case). You need to complete one final step before you can use this code in your application: You need to configure logging.

     

    Moreover, the below link will help you:

    http://www.codeproject.com/KB/architecture/GetLoggingWithEntLib.aspx

    Thanks,

    Paras Sanghani

    Mark As Answer if it helped you

    Tuesday, July 20, 2010 11:45 AM