随笔 - 317, 文章 - 0, 评论 - 453, 阅读 - 114万
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

C# 简易日志记录类

Posted on   PHP-张工  阅读(2685)  评论(0编辑  收藏  举报
public class MyLog
{
    public static void WriteLog(string error)
    {
        WriteLog(error, null);
    }
 
    public static void WriteLog(string error, Exception ex)
    {
        string dir = Application.StartupPath + "\\LOG";
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
 
        error = DateTime.Now.ToString() + " " + error + System.Environment.NewLine;
 
        if (ex != null)
        {
            error += ex.ToString() + System.Environment.NewLine;
        }
 
        string logFilePath = dir + "\\log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
        File.AppendAllText(logFilePath, error);
    }
}

  

努力加载评论中...
点击右上角即可分享
微信分享提示