C#自定义日志记录

C#自定义日志记录,第1张

概述废话不多说,直接上代码: 很简单:将类复制到项目中,最后在配置文件上配置一下:logUrl即可。默认保存在:项目/temp/log

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

很简单:将类复制到项目中,最后在配置文件上配置一下: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);    }  }

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

总结

以上是内存溢出为你收集整理的C#自定义日志记录全部内容,希望文章能够帮你解决C#自定义日志记录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1257121.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存