栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#自定义日志记录

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#自定义日志记录

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

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

/// 
  /// 日志类
  /// 
  /// Creator: v-lxh CreateTime: 2016/7/26 11:18:09
  /// 
  public class Log
  {
    /// 
    /// 写入日志.
    /// 
    /// The STR list.
    /// Creator: v-lxh CreateTime: 2016/7/26 11:18:09
    /// 
    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);
    }

  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/124006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号