欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#自定義日志記錄

 更新時間:2016年09月24日 16:56:11   作者:深入學習ing  
這篇文章主要為大家詳細介紹了C#自定義日志記錄的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

廢話不多說,直接上代碼: 

很簡單:將類復制到項目中,最后在配置文件上配置一下: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("\r\n" + DateTime.Now.ToString() + "-----" + item + "");
      }
      File.WriteAllText(strPath, sb.ToString() + "\r\n-----z-----\r\n" + str);
    }

  }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論