-1

My code stops automatically and I am not getting the cause behind it. So I need to add the log file in order to capture the exception message and save it in text document format in my machine.

public void objInsurityTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        try
        {
            if (IsTimeActive())
            {
                clsTextComparison clsCallTextComaprison = new clsTextComparison();
                clsCallTextComaprison.GetActivityLog();

            }
            clsInsurityWorkFlow.GetInstance().ProcessXML();
            clsInsurityWorkFlow.DestroyInstance();
        }
        catch (Exception )
        {

        }

Please let me know what to write in the Catch part in order to get the log file in text format and it to save it in my machine folder.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141

1 Answers1

0

You should use a logging framework like NLog, Log4Net, etc.

However, at the simplest level (in a single threaded app) you could just do this:

catch (Exception ex)
{
   using (var sw = File.AppendText(path))
      sw.WriteLine(ex.ToString());
}

File.AppendText(String) Method

Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • This is already built into .Net - no need to reinvent the wheel with `File.AppendText` - please read about https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace?redirectedfrom=MSDN&view=netframework-4.7.2 – Alexei Levenkov Dec 27 '18 at 08:57
  • @AlexeiLevenkov ect ect ect ect :) yes i knew about it, definitely worthy note though – TheGeneral Dec 27 '18 at 08:59