Click here to Skip to main content
14,303,339 members
Rate this:
Please Sign up or sign in to vote.
See more:
static void Main(string[] args)
            {
                  int strText;
                  int a, b ,c;
                  StreamWriter logFile;
                        Console.Write("Name: ");
                        try
                        {
                        strText = Convert.ToInt32(Console.ReadLine());
                        a = Convert.ToInt32(Console.ReadLine());
                        b = Convert.ToInt32(Console.ReadLine());
                        c = a / b;
                                if (!File.Exists("Log.txt"))
                                {
                                        logFile = new StreamWriter("Log.txt");
                                }
                                else
                                {
                                        logFile = File.AppendText("Log.txt");
                                }
                                logFile.WriteLine(DateTime.Now);

                                logFile.WriteLine(strText.ToString());

                                logFile.WriteLine();
                                Console.WriteLine("Log file saved successfully!");
                        }
                        catch (Exception ex)
                        {
                        if (!File.Exists("Log.txt"))
                            {
                            logFile = new StreamWriter("Log.txt");
                            }
                        else
                            {
                            logFile = File.AppendText("Log.txt");
                            }
                                Console.WriteLine("Error: " + ex.Message);
                                logFile.WriteLine("Error: " + ex.Message);
                        }
                        logFile.Close();
                        Console.Read();
                }
            }
        }
Posted
Updated 10-Oct-12 21:32pm
v3
Comments
Legor 11-Oct-12 3:33am
   
Unclear question. Please elaborate.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,
If you are trying to ask "which pattern to use", you should be using singleton for logging purposes.
However, if the purpose is really logging, rather than re-inventing wheel, you can try using proven framework like Log4Net.
-Milind
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100