必威-必威-欢迎您

必威,必威官网企业自成立以来,以策略先行,经营致胜,管理为本的商,业推广理念,一步一个脚印发展成为同类企业中经营范围最广,在行业内颇具影响力的企业。

/// 写日志类 /// &lt,后端代码

2019-11-28 22:58 来源:未知

下载文件:

100多行代码达成6秒完成50万条八线程并发日志文件写入,50万日志文件

100多行代码完结6秒完毕50万条四线程并发日志文件写入,辅助日志文件分隔

日记工具类代码:

图片 1using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Utils { /// <summary> /// 写日志类 /// </summary> public class LogUtil { #region 字段 public static object _lock = new object(); public static string path = "D:\log"; public static int fileSize = 10 * 1024 * 1024; //日志分隔文件大小 private static ConcurrentQueue<Tuple<string, string>> msgQueue = new ConcurrentQueue<Tuple<string, string>>(卡塔尔国; #endregion #region 静态构造函数 static LogUtil(卡塔尔 { Thread thread = new Thread(new ThreadStart((卡塔尔(英语:State of Qatar) => { try { int i; List<string> list; Tuple<string, string> tuple; while (true卡塔尔国 { i = 0; list = new List<string>(卡塔尔(英语:State of Qatar); while (msgQueue.TryDequeue(out tuple卡塔尔(英语:State of Qatar) && i++ < 10000卡塔尔国 { list.Add(tuple.Item1.PadLeft(8卡塔尔(英语:State of Qatar) + tuple.Item2卡塔尔国; } if (list.Count > 0卡塔尔 { WriteFile(list, CreateLogPath(卡塔尔卡塔尔; } Thread.Sleep(1卡塔尔(英语:State of Qatar); } } catch { } }卡塔尔国卡塔尔; thread.IsBackground = true; thread.Start(卡塔尔; } #endregion #region 写文件 /// <summary> /// 写文件 /// </summary> public static void WriteFile(List<string> list, string path) { try { if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } if (!File.Exists(path)) { using (FileStream fs = new FileStream(path, FileMode.Create)) { fs.Close(); } } using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { list.ForEach(item => { #region 日志内容 string value = string.Format(@"{0} {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"卡塔尔国, item卡塔尔国; #endregion sw.WriteLine(value); }); sw.Flush(); } fs.Close(); } } catch { } } #endregion #region 生成日志文件路线 /// <summary> /// 生成日志文件路线 /// </summary> public static string CreateLogPath(卡塔尔(قطر‎ { int index = 0; string logPath; bool bl = true; do { index++; logPath = Path.Combine(path, "Log" + Date提姆e.Now.ToString("yyyyMMdd"卡塔尔(قطر‎ + (index == 1 ? "" : "_" + index.ToString()) + ".txt"); if (File.Exists(logPath)) { FileInfo fileInfo = new FileInfo(logPath); if (fileInfo.Length < fileSize) { bl = false; } } else { bl = false; } } while (bl); return logPath; } #endregion #region 写错误日志 /// <summary> /// 写错误日志 /// </summary> public static void LogError(string log卡塔尔(英语:State of Qatar) { msgQueue.Enqueue(new Tuple<string, string>("[Error] ", log)); } #endregion #region 写操作日志 /// <summary> /// 写操作日志 /// </summary> public static void Log(string log卡塔尔(قطر‎ { msgQueue.Enqueue(new Tuple<string, string>("[Info] ", log)); } #endregion } } View Code

测量试验代码:

图片 2using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Utils; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { LogUtil.path = Application.StartupPath + "\log"; //初阶化日志路线 } private void button1_Click(object sender, EventArgs e) { for (int n = 0; n < 10; n++) { Thread thread = new Thread(new ThreadStart(() => { int i = 0; for (int k = 0; k < 50000; k++) { LogUtil.Log((i++).ToString() + " abcdab[email protected]#$%^&dabcdabcdabcdabcdabcdabcdabcdabcd"); } })); thread.IsBackground = true; thread.Start(); } } } } View Code

 测验截图:

图片 3

图片 4

 

100多行代码达成6秒实现50万条多线程并发日志文件写入,援助日志文...

C#写文本日志帮忙类(扶助八十多线程卡塔尔国改过版(不适用于ASP.NET程序卡塔尔(英语:State of Qatar),

出于iis的电动回笼机制,不适用于ASP.NET程序

代码:

图片 5using System; using System.Collections.Concurrent; using System.Configuration; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; namespace CommonDll { /// <summary> /// 写日志类 /// </summary> public class LogUtil { #region 字段 public static string path = ConfigurationManager.AppSettings["LogPath"]; public static int fileSize = 10 * 1024 * 1024; //日志分隔文件大小 private static ConcurrentQueue<Tuple<string, DateTime>> queue = new ConcurrentQueue<Tuple<string, DateTime>>(卡塔尔(英语:State of Qatar); #endregion #region 结构函数 static LogUtil(卡塔尔(قطر‎ { Task.Factory.StartNew(new Action(delegate(卡塔尔 { StringBuilder log; string path; Tuple<string, DateTime> tuple; string item; while (true卡塔尔 { log = new StringBuilder(卡塔尔国; path = CreateLogPath(卡塔尔国; while (queue.TryDequeue(out tuple卡塔尔卡塔尔(قطر‎ { item = string.Format(@"{0} {1}", tuple.Item2.ToString("yyyy-MM-dd HH:mm:ss.fff"卡塔尔(英语:State of Qatar), tuple.Item1卡塔尔(قطر‎; log.AppendFormat("rn{0}", item); } if (log.Length > 0) WriteFile(log.ToString(2, log.Length - 2), path); Thread.Sleep(100); } })); } #endregion #region 写文件 /// <summary> /// 写文件 /// </summary> public static void WriteFile(string log, string path) { try { if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } if (!File.Exists(path)) { using (FileStream fs = new FileStream(path, FileMode.Create)) { fs.Close(); } } using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(log); sw.Flush(); } fs.Close(); } } catch { } } #endregion #region 生成日志文件路线 /// <summary> /// 生成日志文件路线 /// </summary> public static string CreateLogPath(卡塔尔国 { int index = 0; string logPath; bool bl = true; do { index++; logPath = Path.Combine(path, "Log" + Date提姆e.Now.ToString("yyyyMMdd"卡塔尔国 + (index == 1 ? "" : "_" + index.ToString()) + ".txt"); if (File.Exists(logPath)) { FileInfo fileInfo = new FileInfo(logPath); if (fileInfo.Length < fileSize) { bl = false; } } else { bl = false; } } while (bl); return logPath; } #endregion #region 写错误日志 /// <summary> /// 写错误日志 /// </summary> public static void LogError(string log卡塔尔国{ queue.Enqueue(new Tuple<string, DateTime>("[Error] " + log, DateTime.Now)); } #endregion #region 写操作日志 /// <summary> /// 写操作日志 /// </summary> public static void Log(string log卡塔尔(قطر‎ { queue.Enqueue(new Tuple<string, DateTime>("[Info] " + log, DateTime.Now)); } #endregion } } View Code

测验代码:

图片 6private void button1_Click(object sender, 伊芙ntArgs e卡塔尔(قطر‎ { int n = 10000; DateTime dtStart = DateTime.Now; for (int i = 1; i <= n; i++卡塔尔(英语:State of Qatar) { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object obj卡塔尔国 { int j = (int卡塔尔国obj; LogUtil.Log("测验" + j.ToString("00000"卡塔尔(英语:State of Qatar)卡塔尔国; if (j == n卡塔尔 { double sec = DateTime.Now.Subtract(dtStart卡塔尔.TotalSeconds; MessageBox.Show(n + "条日志完毕,耗费时间" + sec.ToString("0.000"卡塔尔国 + "秒"卡塔尔(قطر‎; } }卡塔尔, i卡塔尔; } } View Code

效果图:

图片 7

 

由于iis的机动回笼机制,不适用于ASP.NET程序 代码: using System; using System.C...

protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add;
dt.Columns.Add;
dt.Columns.Add;
dt.Columns.Add;
dt.Columns.Add;
for (int i = 0; i < 6; i++)
{
dt.Rows.Add();
dt.Rows[i][0] = "CN"+i.ToString();
dt.Rows[i][1] = "EN"+i.ToString();
dt.Rows[i][2] = "JN"+i.ToString();
dt.Rows[i][3] = "HK"+i.ToString();
dt.Rows[i][4] = "TW"+i.ToString();
}

代码:

ExportDataGridToCSV;

后端代码:

}

public IActionResult DownloadFile()
{
var FilePath = @"./files/deparment.xlsx";
var stream = System.IO.File.OpenRead(FilePath);
return File(stream, "application/vnd.android.package-archive", Path.GetFileName(FilePath));
}

/// <summary>
/// Export the data from datatable to CSV file
/// </summary>
/// <param name="grid"></param>
public void ExportDataGridToCSV(DataTable dt)
{
string strFile = "";
string path = "";

页面代码:

//File info initialization
strFile = "test";
strFile = strFile + DateTime.Now.ToString("yyyyMMddhhmmss");
strFile = strFile + ".csv";
path = Server.MapPath;

TAG标签:
版权声明:本文由必威发布于必威-编程,转载请注明出处:/// 写日志类 /// &lt,后端代码