日志文件中的错误信息 [英] error information in log file

查看:95
本文介绍了日志文件中的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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());
            }
        }
    }
}

推荐答案

为什么不使用一种可用的日志记录框架,我优先选择LoggingApplicationBlock,它将提供现成的大多数日志记录功能您的应用程序需求.将其与带有日志处理程序的ExceptionHandling应用程序块集成在一起,将使您可以将异常详细信息放在同一日志文件中.

错误管理异常简单 [
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[^]


这篇关于日志文件中的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆