Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
namespace GTSS
{
    public class Logger
    {
        public string logFile = "";
        public string dataLogFile = "";
        public Logger()
        {
            System.Configuration.AppSettingsReader reader1 = new System.Configuration.AppSettingsReader();
            logFile = (string)reader1.GetValue("LogFile", logFile.GetType());
            dataLogFile = (string)reader1.GetValue("DataLogFile", dataLogFile.GetType());
        }
        public void AddLogEntry(string entry)
        {
            StreamWriter outFile = new StreamWriter(logFile, true);
            //StreamWriter outfile = new StreamWriter(logFile);
            //s.Append(quotes + "LVTS" + quotes + "," + quotes + dtTrades.Rows.Count.ToString() + quotes + "," + quotes + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + quotes + "," + quotes + DateTime.Now.ToString("MM/dd/yyyy") + quotes);
            outFile.WriteLine(entry);
            outFile.Close();
        }
        public void archive_log()
        {
            if (File.Exists(logFile.ToString()))
            {
                File.Copy(logFile, logFile.Substring(0, logFile.Length - 4) + "_" +
                                                                            DateTime.Now.Year.ToString() +
                                                                            DateTime.Now.Month.ToString() +
                                                                            DateTime.Now.Day.ToString() + "_" +
                                                                            DateTime.Now.Hour.ToString() +
                                                                            DateTime.Now.Minute.ToString() + ".txt");
                File.Delete(logFile.ToString());
            }
        }
        public void AddDataLogEntry(string entry)
        {
            StreamWriter outFile = new StreamWriter(dataLogFile, true);
            //StreamWriter outfile = new StreamWriter(logFile);
            //s.Append(quotes + "LVTS" + quotes + "," + quotes + dtTrades.Rows.Count.ToString() + quotes + "," + quotes + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + quotes + "," + quotes + DateTime.Now.ToString("MM/dd/yyyy") + quotes);
            outFile.WriteLine(entry);
            outFile.Close();
        }
        public void delete_data_log()
        {
            if (File.Exists(dataLogFile.ToString()))
            {
                File.Delete(dataLogFile.ToString());
            }
        }
    }
}
Posted
Updated 29-Jun-11 5:56am
v2
Comments
Thomas Krojer 29-Jun-11 11:42am    
What is the problem?
[no name] 29-Jun-11 11:56am    
Format code snippets

1 solution

Why not use one of the logging frameworks available, I preference is LoggingApplicationBlock, that will provide out of the box most of the logging features your application need. Integrating it with ExceptionHandling applicaiton block with log handler will allow you to put exception details in the same log file.

Error Management Made Exceptionally Easy[^]
 
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