澳门至尊网站-首页

您的位置:澳门至尊网站 > 黑客安全 > 100多行代码实现6秒完成50万条多线程并发日志文

100多行代码实现6秒完成50万条多线程并发日志文

2019-10-18 21:12

下载文件:

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(() => { try { int i; List<string> list; Tuple<string, string> tuple; while (true) { i = 0; list = new List<string>(); while (msgQueue.TryDequeue(out tuple) && i++ < 一千0) { list.Add(tuple.Item1.PadLeft(8) + tuple.Item2); } if (list.Count > 0) { WriteFile(list, CreateLogPath()); } Thread.Sleep(1); } } 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}", Date提姆e.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" + DateTime.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) { 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万条十六线程并发日志文件写入,扶助日志文...

代码:

后端代码:

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));
}

页面代码:

<a href=";

上传文件:

nuget下载:EPPlus.Core

基本思路:近期是经过将页面上传播的文本保留至项目地址里面,再读收取来,再判定传入ecxel文件的头部是还是不是符合须求,契合则写入数据库

 图片 5

代码:

后端代码:

public object AddMulitDeparment(IFormCollection files)
{
string[] colName = new string[] { "公司名称", "部门总监", "部门名称", "职员和工人数量", "部门代码" };
var result = new object();
string message = "";
if (files != null && files.Files.Count > 0)
{
for (int i = 0; i < files.Files.Count; i++)
{
var file = files.Files[i];
try
{
object path = _importExcelUtil.SaveExcel(file);
FileInfo fileInfo = new FileInfo((string)path);
using (FileStream fs = new FileStream(fileInfo.ToString(), FileMode.Create))
{
file.CopyTo(fs);
fs.Flush();
}
using (ExcelPackage package = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
if (_importExcelUtil.JudgeCol(worksheet,colName))
{
result = new
{
data = _importExcelUtil.SaveDepToDB(worksheet)
};
System.IO.File.Delete((string)path);
}
}
}
catch (Exception ex)
{
message= ex.Message;
}
}
}
return result;
}

//推断尾部(抽出表格内容同)

public bool JudgeCol(ExcelWorksheet worksheet,string[] colName)
{
int ColCount = worksheet.Dimension.Columns;
bool bHeaderRow = true;
//excel下标从1开始
for (int col = 1,index=0; col <= ColCount&&index<colName.Length; col++,index++)
{
string c = worksheet.Cells[1, col].Value.ToString();
if (!c.Equals(colName[index]))
{
bHeaderRow = false;
throw new Exception("格式错误,导入文本的第"+index+"列应该为"+colName[index]+"!");
}
}
return bHeaderRow;
}

前端代码,由于供给上传文件,须求将http央求底部的Content-Type修改为multipart/form-data

*仅看成个体学习记录

本文由澳门至尊网站发布于黑客安全,转载请注明出处:100多行代码实现6秒完成50万条多线程并发日志文

关键词:

  • 上一篇:没有了
  • 下一篇:没有了