新建一个 DataLog类 用来存放生成Log日志方法
#region 写日志 public void WriteLogEx(string sendJson, string code = "", string msg = "", string fileName = "") { string path = AppDomain.CurrentDomain.BaseDirectory; string logDir = "log"; logDir += "\" + DateTime.Now.ToString("yyyy") + "\" + DateTime.Now.ToString("MM"); path = Path.Combine(path, logDir); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } try { StringBuilder log = new StringBuilder(); log.Append(Environment.NewLine); log.Append("================BEGIN"); log.Append(DateTime.Now); log.Append("================"); log.Append(Environment.NewLine); log.Append(sendJson); log.Append(Environment.NewLine); log.Append("调用HTTP请求,返回的数据结果:{"code":" + code + ","msg":" + msg + "}"); log.Append(Environment.NewLine); log.Append("================END "); log.Append(DateTime.Now); log.Append("================"); log.Append(Environment.NewLine); fileName = string.IsNullOrWhiteSpace(fileName) ? DateTime.Now.Day.ToString() + "日" : fileName; fileName = fileName + ".txt"; string file = Path.Combine(path, fileName); System.IO.File.AppendAllText(file, log.ToString(), System.Text.Encoding.Default); } catch { } } #endregion
使用的话直接实例化,调方法。