日志记录

简单记录日志 

internal static class LogWriter
    {
        private static LogHelper Getlog()
        {
            string LogerSource = "LogText";
            LogHelper log = new LogHelper(LogerSource);
            return log;
        }
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="message"></param>
        public static void LogInfo(string message)
        {
            try
            {
                if (string.IsNullOrEmpty(message))
                {
                    return;
                }
                LogHelper log = Getlog();
                log.LogInfo(message);
            }
            catch
            {
            }
        }
        /// <summary>
        /// 记录错误
        /// </summary>
        /// <param name="message"></param>
        public static void LogError(string message)
        {
            try
            {
                if (string.IsNullOrEmpty(message))
                {
                    return;
                }
                LogHelper log = Getlog();
                log.LogError(message);
            }
            catch
            {
            }
        }
    }
LogWriter
public class LogHelper
{
    // Fields
    private string _logerSource;
    private string ERROR = "错误内容:{0}";
    private string INFO = "事件内容:{0}";
    private string LOG = "{0} {1}
";

    // Methods
    public LogHelper(string logerSource)
    {
        this._logerSource = logerSource;
    }

    public void LogError(string error)
    {
        try
        {
            string log = string.Format(this.ERROR, error);
            this.WriteString("ErrorLog", log);
        }
        catch
        {
        }
    }

    public void LogInfo(string info)
    {
        try
        {
            string log = string.Format(this.INFO, info);
            this.WriteString("InfoLog", log);
        }
        catch
        {
        }
    }

    private bool WriteString(string logType, string log)
    {
        bool flag = false;
        string path = this._logPath + logType;
        DirectoryInfo info = new DirectoryInfo(path);
        if (!info.Exists)
        {
            info.Create();
        }
        using (StreamWriter writer = new StreamWriter(path + @"" + DateTime.Now.Date.ToString("yyyyMMdd") + ".log", true, Encoding.UTF8))
        {
            string str2 = DateTime.Now.ToString("HH:mm:ss");
            writer.Write(string.Format(this.LOG, str2, log));
            writer.Flush();
            writer.Close();
            return flag;
        }
    }

    // Properties
    private string _logPath
    {
        get
        {
            return (AppDomain.CurrentDomain.BaseDirectory + @"Logs" + this._logerSource + @"");
        }
    }
}
LogHelper
原文地址:https://www.cnblogs.com/weixing/p/5674357.html