Click here to Skip to main content
15,848,391 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C# console application how to save exception in txt file?

I generated an error. It's not writing an error to text file.

Is there any other way to do it?

Currently, I created in text file in C:\ location in local. But in Test and prod server, "log file" should be in D:\.

What I have tried:

C#
class Program
   {
       static void Main(string[] args)
       {
           // SMTP server details
           int timeout = 1000; // Ping timeout in milliseconds

           try
           {
               var serverDetails = MailStatus.sqlUtility.GetSystemDetails();

               // Ping the SMTP server
               bool isServerUp =
                    PingServer(serverDetails.system_Address, timeout);

               // Save the result in SQL Server
               if (isServerUp)
               {
                   MailStatus.sqlUtility.SaveResultToDatabase
                       (serverDetails, isServerUp);
               }
           }
           catch (Exception ex)
           {
               //Console.WriteLine(ex.Message);
               LogExceptionToFile(ex);
           }

           Console.WriteLine("Ping completed.");
       }
       public static void LogExceptionToFile(Exception ex)
       {
           string logFilePath = "C:\\Projects\\\Logs\\SMTP\\SMTPlog.txt";

           //string logFilePath = "D:\\Projects\\Logs\\SMTP\\SMTPlog.txt";

           try
           {
               using (StreamWriter writer =
                      new StreamWriter(logFilePath, true))
               {
                   writer.WriteLine($"[{DateTime.Now}] Exception:
                                   {ex.GetType().FullName}");
                   writer.WriteLine($"Message: {ex.Message}");
                   writer.WriteLine($"Stack Trace: {ex.StackTrace}");
                   writer.WriteLine(new string('-', 40));
               }
           }
           catch (IOException)
           {
               // Handle any exceptions related to file access here
           }
       }
Posted
Updated 23-Aug-23 2:20am
v2

Change this line:
catch (IOException)

to:
catch (IOException ex)

Now you can view the exception:
catch (IOException ex)
{
	Console.WriteLine(ex);
}

This should explain the issue that you are having. If still unsure, click on "Improve question" and post the exception details, do not abbreviate.
 
Share this answer
 
To add to what Graeme has said, you should never hard code a file path into your app - it requires you to change the app between development and release versions, which means what you release is untested.
Use the Settings file instead - open the Solution Explorer pane, the project branch, the Properties twig, and double click the "Settings.settings" leaf. Add your path to that with the name "LogPath" and you can access it in your code via "
Properties.Settings.Default.LogPath
Now you can change the path outside the app and you don't need to recompile, or provide a mechanism to set the path inside your app options form if you have one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900