C#中封装写日志函数

public static void WriteLogJson(string msg)
        {

            try
            {
                string logPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\Json\\";

                //string logPath = Environment.CurrentDirectory + "\\Logs\\";

                string logPathFile = logPath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

                if (!System.IO.Directory.Exists(logPath))
                    System.IO.Directory.CreateDirectory(logPath);

                if (!System.IO.File.Exists(logPathFile))
                {
                    System.IO.File.Create(logPathFile).Close();
                }
                //对日志加密写入
               // msg = ParamUtils.encrypted(Global.Publickey, msg);
                using (System.IO.StreamWriter sw = System.IO.File.AppendText(logPathFile))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }

 

posted @ 2023-07-04 16:41  NangFah  阅读(60)  评论(0)    收藏  举报