C#自定義日志記錄
廢話不多說(shuō),直接上代碼:
很簡(jiǎn)單:將類復(fù)制到項(xiàng)目中,最后在配置文件上配置一下:logUrl即可。 默認(rèn)保存在:項(xiàng)目/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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#引用類型轉(zhuǎn)換的常見(jiàn)方式總結(jié)
這篇文章主要介紹了C#引用類型轉(zhuǎn)換的常見(jiàn)方式,包括子類轉(zhuǎn)換成父類,父類轉(zhuǎn)換成子類,以及不是子父級(jí)關(guān)系類之間的轉(zhuǎn)換,需要的朋友可以參考下2014-09-09
C#實(shí)現(xiàn)表格數(shù)據(jù)轉(zhuǎn)實(shí)體的示例代碼
在實(shí)際開發(fā)過(guò)程中,特別是接口對(duì)接之類的,對(duì)于這種需求是屢見(jiàn)不鮮,現(xiàn)在很多在線平臺(tái)也都提供了像json轉(zhuǎn)實(shí)體、sql轉(zhuǎn)實(shí)體等。本文將用C#實(shí)現(xiàn)這一功能,需要的可以參考一下2022-09-09
.net文件上傳時(shí)實(shí)現(xiàn)通過(guò)文件頭確認(rèn)文件類型的方法
這篇文章主要介紹了.net文件上傳時(shí)實(shí)現(xiàn)通過(guò)文件頭確認(rèn)文件類型的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07
Unity中 ShaderGraph 實(shí)現(xiàn)旋渦傳送門效果入門級(jí)教程(推薦)
通過(guò)Twirl 旋轉(zhuǎn)節(jié)點(diǎn)對(duì)Gradient Noise 梯度噪聲節(jié)點(diǎn)進(jìn)行操作,就可得到一個(gè)旋轉(zhuǎn)的旋渦效果。具體實(shí)現(xiàn)代碼跟隨小編一起通過(guò)本文學(xué)習(xí)下吧2021-07-07

