Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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

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
 
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