NetCore 记录文本日志

1、首先写日志帮助类

 public class TextLogHelper
    {
        /// <summary>
        /// 打印文本日志
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ex"></param>
        /// <param name="param"></param>
        public static void PrintLog(string url, Exception ex, string param = "")
        {
            string folderNew = Directory.GetCurrentDirectory() + "/OperateLog/" + DateTime.Now.ToString("yyyy-MM-dd");
            string pathFileNew = folderNew + "/" + DateTime.Now.Hour + ".log";//记事本文件名
            if (!FileHelper.FolderExists(folderNew))
                FileHelper.CreateFolder(folderNew);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"--------------------------------------{DateTime.Now}--------------------------------------");
            sb.Append(string.Format("请求参数--{0}\r\n", param));
            sb.Append(string.Format("请求时间--{0}\r\n", DateTime.Now));
            sb.Append(string.Format("请求地址--{0}\r\n", url));
            sb.Append(string.Format("异常信息--{0}\r\n", ex == null ? "" : ex.Message));
            sb.Append(string.Format("堆栈信息--{0}\r\n", ex == null ? "" : ex.StackTrace));
            sb.AppendLine($"--------------------------------------{DateTime.Now}--------------------------------------");
            FileHelper.WriteFile(pathFileNew, sb.ToString());
        }
    }
    public class FileHelper
    {
        public static bool WriteFile(string filePath, string fileContent)
        {
            return WriteFile(filePath, fileContent, true);
        }

        public static bool WriteFile(string filePath, string fileContent, bool append)
        {
            try
            {
                StreamWriter sw = new StreamWriter(filePath, append);
                sw.WriteLine(fileContent);
                sw.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string ReadFile(string filePath)
        {
            try
            {
                StreamReader sr = new StreamReader(filePath);
                string fileContent = sr.ReadToEnd();
                sr.Close();
                return fileContent;
            }
            catch
            {
                return string.Empty;
            }
        }

        public static bool FolderExists(string folder)
        {
            return Directory.Exists(folder);
        }

        public static DirectoryInfo CreateFolder(string folder)
        {
            return Directory.CreateDirectory(folder);
        }

        public static bool FileExists(string filePath)
        {
            return File.Exists(filePath);
        }
    }

2、测试

[HttpPost]
        public IActionResult TestTextLog(TextLogModel model)
        {            
            try
            {
                Student? stu = null;
                var name = stu.Name;
                return ToSuccess(name);
            }
            catch (Exception ex)
            {
                //调用日志方法
                TextLogHelper.PrintLog($"api/{GetType().Name}/TestTextLog", ex, JsonConvert.SerializeObject(model));
                throw;
            }
        }

3、效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值