csv 页面等
This commit is contained in:
1 parent
57299c0b55
commit
2b56f8e7d3
17 files changed
+923
-794
No files matched your search
@@ -3,6 +3,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace WindowsFormsApp1
|
||||
{
|
||||
@@ -20,16 +21,33 @@ namespace WindowsFormsApp1
|
||||
{
|
||||
private static string _baseFilePath;
|
||||
private static List<string> _customHeaders = new List<string>();
|
||||
private static readonly object _lockObj = new object(); // 添加锁对象用于线程安全
|
||||
private static bool _initialized = false;
|
||||
|
||||
public static void Initialize(string baseFilePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(baseFilePath))
|
||||
throw new ArgumentNullException(nameof(baseFilePath), "日志文件路径不能为空");
|
||||
|
||||
_baseFilePath = baseFilePath;
|
||||
_initialized = true;
|
||||
|
||||
// 确保目录存在
|
||||
string directory = Path.GetDirectoryName(baseFilePath);
|
||||
if (!Directory.Exists(directory))
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
// 设置自定义表头
|
||||
public static void SetHeaders(List<string> headers)
|
||||
{
|
||||
_customHeaders = headers;
|
||||
if (headers == null)
|
||||
throw new ArgumentNullException(nameof(headers));
|
||||
|
||||
lock (_lockObj)
|
||||
{
|
||||
_customHeaders = new List<string>(headers); // 创建副本
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetCurrentLogFilePath()
|
||||
@@ -37,48 +55,60 @@ namespace WindowsFormsApp1
|
||||
var date = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
var fileExtension = Path.GetExtension(_baseFilePath);
|
||||
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(_baseFilePath);
|
||||
if (!Directory.Exists(Path.GetDirectoryName(_baseFilePath)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(_baseFilePath));
|
||||
}
|
||||
return $"{Path.GetDirectoryName(_baseFilePath)}/{fileNameWithoutExtension}_{date}{fileExtension}";
|
||||
}
|
||||
|
||||
public static void Log(string csvFormattedString)
|
||||
{
|
||||
var filePath = GetCurrentLogFilePath();
|
||||
// 检查文件是否存在,如果不存在则创建并写入表头
|
||||
if (!File.Exists(filePath))
|
||||
if (!_initialized)
|
||||
throw new InvalidOperationException("CsvLogger 未初始化,请先调用 Initialize 方法");
|
||||
|
||||
lock (_lockObj)
|
||||
{
|
||||
using (var writer = new StreamWriter(filePath))
|
||||
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
|
||||
try
|
||||
{
|
||||
if (_customHeaders.Count > 0)
|
||||
var filePath = GetCurrentLogFilePath();
|
||||
bool fileExists = File.Exists(filePath);
|
||||
|
||||
// 如果文件不存在,先写入表头
|
||||
if (!fileExists)
|
||||
{
|
||||
foreach (var header in _customHeaders)
|
||||
using (var writer = new StreamWriter(filePath, false, Encoding.UTF8))
|
||||
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
|
||||
{
|
||||
csv.WriteField(header);
|
||||
if (_customHeaders.Count > 0)
|
||||
{
|
||||
foreach (var header in _customHeaders)
|
||||
{
|
||||
csv.WriteField(header);
|
||||
}
|
||||
csv.NextRecord();
|
||||
}
|
||||
else
|
||||
{
|
||||
csv.WriteHeader<LogEntry>();
|
||||
csv.NextRecord();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 直接写入日志,不使用队列
|
||||
using (var writer = new StreamWriter(filePath, true, Encoding.UTF8))
|
||||
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
|
||||
{
|
||||
var fields = csvFormattedString.Split(',');
|
||||
foreach (var field in fields)
|
||||
{
|
||||
csv.WriteField(field);
|
||||
}
|
||||
csv.NextRecord();
|
||||
}
|
||||
else
|
||||
{
|
||||
csv.WriteHeader<LogEntry>();
|
||||
csv.NextRecord();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 追加日志条目到文件
|
||||
using (var writer = new StreamWriter(filePath, true))
|
||||
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
|
||||
{
|
||||
var fields = csvFormattedString.Split(',');
|
||||
foreach (var field in fields)
|
||||
catch (Exception ex)
|
||||
{
|
||||
csv.WriteField(field);
|
||||
// 记录错误,但不抛出异常
|
||||
Console.WriteLine($"写入日志文件时出错: {ex.Message}");
|
||||
}
|
||||
csv.NextRecord();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user