【C#】写日志方法

来源:互联网 发布:大数据处理过程知乎 编辑:程序博客网 时间:2024/04/29 02:31
c# 写日志的方法,几个参数分别标识日志头显示什么,可赋值为空
public class LogTool    {        public static void WriteLog(string ClassName, string TagType, string ParameterName, string ParameterValue)        {            StreamWriter sw = null;            try            {                //string logPath = string.Format("{0}Log\\", AppDomain.CurrentDomain.BaseDirectory);                string logPath = string.Format("D:\\SelfLog\\SYS");                if (!Directory.Exists(logPath))                {                    Directory.CreateDirectory(logPath);                }                //以日期作为日志文件名                string LogDate = DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString();                string logFile = string.Format("{0}\\_log_{1}.log", logPath, LogDate);                sw = new StreamWriter(logFile, true);                string LogMessage = DateTime.Now.ToString() + "//" + ClassName + "// :  " + TagType + "->" + ParameterName + ":[" + ParameterValue + "]";                sw.WriteLine(LogMessage);                sw.Close();            }            catch            {                if (sw != null)                {                    sw.Close();//写日志失败,关闭文件                }            }        }    }


 

0 0
原创粉丝点击