简单IO操作写入本地日志
/// <summary> /// 写入本地日志 /// </summary> /// <param name="strText">日志内容</param> /// <param name="logPath">日志路径</param> /// <param name="fileName">日志文件名称</param> public static void LogsWriteTxt(string strText, string logPath, string fileName) { try { if (logPath == null || logPath.Trim() == string.Empty) { logPath = ConfigurationManager.AppSettings["LogPath"]?.ToString().Trim(); } if (Directory.Exists(logPath) == false) { Directory.CreateDirectory(logPath); } string strFilePath = ""; if (string.IsNullOrWhiteSpace(fileName)) { strFilePath = logPath + "Log" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; } else { strFilePath = logPath + fileName + ".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) { //MessageBox.Show($"写入日志异常: { ex.Message}"); } }