简单写入本地日志,日志文件位置与主程序exe位置相同
/// <summary> /// 写入本地日志 /// </summary> /// <param name="strText">日志内容</param> /// <param name="logPath">日志路径</param> /// <param name="fileName">日志文件名称</param> public static void SaveLogInfo(string strText, string logPath, string fileName) { try { string dtStr = DateTime.Now.ToString("yyyyMMddHHmmssffff"); if (logPath == null || logPath.Trim() == string.Empty) { logPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Startup); } if (Directory.Exists(logPath) == false) { Directory.CreateDirectory(logPath); } string strFilePath = ""; if (string.IsNullOrWhiteSpace(fileName)) { strFilePath = $"{logPath}\\Log{dtStr}.txt"; } else { strFilePath = $"{logPath}\\{fileName}_{dtStr}.txt"; } System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, true, System.Text.Encoding.Default); sw.WriteLine($"【{DateTime.Now}】{strText}"); sw.Close(); } catch (Exception ex) { } }