131 lines
5.3 KiB
C#
131 lines
5.3 KiB
C#
using BRS.Common.Model.Helper;
|
|
using 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 BRS.Common.Model.Helper
|
|
{
|
|
public class CSVHelper
|
|
{
|
|
public event Action<DateTime, string> OnCSVExceptionRaised;
|
|
Dictionary<string, CSVSet> csvSetDict = new Dictionary<string, CSVSet>();
|
|
|
|
//private string baseDirectory = "";
|
|
//public string BaseDirectory
|
|
//{
|
|
// get => baseDirectory;
|
|
// set
|
|
// {
|
|
// baseDirectory = value;
|
|
// if (string.IsNullOrWhiteSpace(baseDirectory) || !Path.IsPathRooted(baseDirectory))
|
|
// {
|
|
// baseDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CSV");
|
|
// }
|
|
|
|
// //if (!Directory.Exists(baseDirectory))
|
|
// //{
|
|
// // Directory.CreateDirectory(baseDirectory);
|
|
// //}
|
|
// }
|
|
//}
|
|
public bool EnableCSVRecord { get; set; } = true;
|
|
|
|
object lockObj = new object();
|
|
|
|
public async void CSVOutputAsync(string fullname, string data, string head = "")
|
|
{
|
|
if (!EnableCSVRecord)
|
|
return;
|
|
|
|
await Task.Run(() =>
|
|
{
|
|
if (!csvSetDict.ContainsKey(fullname))
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
if (!csvSetDict.ContainsKey(fullname))
|
|
{
|
|
csvSetDict[fullname] = new CSVSet()
|
|
{
|
|
FullName = fullname,
|
|
CSVHead = head,
|
|
};
|
|
|
|
csvSetDict[fullname].CSVTask = new Task(async (k) =>
|
|
{
|
|
|
|
CSVSet set = csvSetDict[k.ToString()];
|
|
try
|
|
{
|
|
lock (set.LockObj)
|
|
{
|
|
if (!StaticHelper.CheckFilesCanUse(set.FullName))
|
|
{
|
|
OnCSVExceptionRaised?.Invoke(DateTime.Now, $"CSV文件{set.FullName}被占用,无法写入");
|
|
return;
|
|
}
|
|
if (!Directory.Exists(Path.GetDirectoryName(set.FullName)))
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(set.FullName));
|
|
}
|
|
set.IsFileCreated = File.Exists(set.FullName);
|
|
}
|
|
using (StreamWriter writer = new StreamWriter(set.FullName, true, Encoding.UTF8))
|
|
{
|
|
while (true)
|
|
{
|
|
lock (set.LockObj)
|
|
{
|
|
if (!set.IsFileCreated)
|
|
{
|
|
writer.WriteLine(set.CSVHead);
|
|
set.IsFileCreated = true;
|
|
}
|
|
|
|
while (set.CSVData.Count > 0)
|
|
{
|
|
if (set.CSVData.TryDequeue(out string singleData))
|
|
{
|
|
writer.WriteLine(singleData);
|
|
}
|
|
}
|
|
|
|
writer.Flush();
|
|
}
|
|
await Task.Delay(2000);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
OnCSVExceptionRaised?.Invoke(DateTime.Now, $"CSV文件{set.FullName}写入异常:{ex.GetExceptionMessage()}");
|
|
}
|
|
}, fullname);
|
|
|
|
csvSetDict[fullname].CSVTask.Start();
|
|
}
|
|
}
|
|
}
|
|
|
|
csvSetDict[fullname].CSVData.Enqueue(data);
|
|
});
|
|
}
|
|
}
|
|
|
|
public class CSVSet
|
|
{
|
|
public string FullName { get; set; } = "";
|
|
public object LockObj { get; set; } = new object();
|
|
public Task CSVTask { get; set; } = null;
|
|
public string CSVHead { get; set; } = "";
|
|
public ConcurrentQueue<string> CSVData { get; set; } = new ConcurrentQueue<string>();
|
|
public bool IsFileCreated { get; set; } = false;
|
|
|
|
}
|
|
}
|