加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#自定义日志记录

发布时间:2020-12-15 06:17:05 所属栏目:百科 来源:网络整理
导读:废话不多说,直接上代码: 很简单:将类复制到项目中,最后在配置文件上配置一下:logUrl即可。 默认保存在:项目/temp/log /// summary /// 日志类 /// /summary /// remarksCreator: v-lxh CreateTime: 2016/7/26 11:18:09/remarks /// Description/Descri

废话不多说,直接上代码: 

很简单:将类复制到项目中,最后在配置文件上配置一下:logUrl即可。 默认保存在:项目/temp/log

/// <summary>
  /// 日志类
  /// </summary>
  /// <remarks>Creator: v-lxh CreateTime: 2016/7/26 11:18:09</remarks>
  /// <Description></Description>
  public class Log
  {
    /// <summary>
    /// 写入日志.
    /// </summary>
    /// <param name="strList">The STR list.</param>
    /// <remarks>Creator: v-lxh CreateTime: 2016/7/26 11:18:09</remarks>
    /// <Description></Description>
    public static void WriteLog(params object[] strList)
    {
      //判断是否开启日志模式
      //if (!LogModel) return;
      if (strList.Count() == 0) return;
      //日志文件路径
      string strDicPath = "";
      try
      {
        strDicPath = HttpContext.Current.Server.MapPath("~/temp/log/");
        if (strDicPath == null || strDicPath == "")
        {
          strDicPath = System.Configuration.ConfigurationManager.AppSettings["logUrl"] + "/temp/log/";
        }
      }
      catch (Exception e)
      {
        strDicPath = System.Configuration.ConfigurationManager.AppSettings["logUrl"] + "/temp/log/";
      }
      string strPath = strDicPath + string.Format("{0:yyyy年-MM月-dd日}",DateTime.Now) + "日志记录.txt";
      if (!Directory.Exists(strDicPath))
      {
        Directory.CreateDirectory(strDicPath);
      }
      if (!File.Exists(strPath))
      {
        using (FileStream fs = File.Create(strPath)) { }
      }
      string str = File.ReadAllText(strPath);
      StringBuilder sb = new StringBuilder();
      foreach (var item in strList)
      {
        sb.Append("rn" + DateTime.Now.ToString() + "-----" + item + "");
      }
      File.WriteAllText(strPath,sb.ToString() + "rn-----z-----rn" + str);
    }

  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读