You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.4 KiB
89 lines
3.4 KiB
|
4 weeks ago
|
using System;
|
||
|
|
using System.IO;
|
||
|
|
using System.Text;
|
||
|
|
using leak_test_project.Models;
|
||
|
|
|
||
|
|
namespace leak_test_project.Utils
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 통신 로그 및 시스템 이력을 파일로 저장하는 유틸리티
|
||
|
|
/// </summary>
|
||
|
|
public static class FileLogger
|
||
|
|
{
|
||
|
|
private static readonly string LogDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
|
||
|
|
private static readonly object _lock = new object();
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 검사 데이터를 Logs/yyyy-MM-dd.csv 파일에 저장함
|
||
|
|
/// </summary>
|
||
|
|
public static void LogInspectData(InspectData data)
|
||
|
|
{
|
||
|
|
lock (_lock)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (!Directory.Exists(LogDirectory))
|
||
|
|
Directory.CreateDirectory(LogDirectory);
|
||
|
|
|
||
|
|
string dateStr = DateTime.Now.ToString("yyyy-MM-dd");
|
||
|
|
string filePath = Path.Combine(LogDirectory, $"{dateStr}.csv");
|
||
|
|
|
||
|
|
bool isNewFile = !File.Exists(filePath);
|
||
|
|
|
||
|
|
// CSV 헤더: Date,Time,Channel,ID,Value,Judgment,Mode,LineNo,ProductType,SpecUL,SpecLL,Retest
|
||
|
|
if (isNewFile)
|
||
|
|
{
|
||
|
|
string header = "Date,Time,Channel,ID,Value,Judgment,Mode,LineNo,ProductType,SpecUL,SpecLL,Retest" + Environment.NewLine;
|
||
|
|
File.WriteAllText(filePath, header, Encoding.UTF8);
|
||
|
|
}
|
||
|
|
|
||
|
|
string csvLine = $"{Esc(data.InspectDate)},{Esc(data.InspectTime)},{Esc(data.Channel)},{Esc(data.ProductId)}," +
|
||
|
|
$"{Esc(data.MeasuredValue)},{Esc(data.Judgment)},{Esc(data.Mode)},{Esc(data.LineNo)}," +
|
||
|
|
$"{Esc(data.ProductType)},{Esc(data.SpecUL)},{Esc(data.SpecLL)},{Esc(data.Retest)}{Environment.NewLine}";
|
||
|
|
|
||
|
|
File.AppendAllText(filePath, csvLine, Encoding.UTF8);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Console.WriteLine($"[FileLogger Error] {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 단순 텍스트 로그 (기존 호환성 유지용)
|
||
|
|
/// </summary>
|
||
|
|
public static void Log(string tag, string message)
|
||
|
|
{
|
||
|
|
lock (_lock)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (!Directory.Exists(LogDirectory))
|
||
|
|
Directory.CreateDirectory(LogDirectory);
|
||
|
|
|
||
|
|
string dateStr = DateTime.Now.ToString("yyyy-MM-dd");
|
||
|
|
string filePath = Path.Combine(LogDirectory, $"{dateStr}_system.log");
|
||
|
|
|
||
|
|
string logEntry = $"[{DateTime.Now:HH:mm:ss.fff}] [{tag}] {message}{Environment.NewLine}";
|
||
|
|
File.AppendAllText(filePath, logEntry, Encoding.UTF8);
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Console.WriteLine($"[FileLogger Error] {ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// CSV 값 이스케이프: 쉼표, 따옴표, 줄바꿈이 포함된 값을 안전하게 감싸줌
|
||
|
|
/// </summary>
|
||
|
|
private static string Esc(string value)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrEmpty(value)) return "";
|
||
|
|
if (value.Contains(",") || value.Contains("\"") || value.Contains("\n"))
|
||
|
|
return $"\"{value.Replace("\"", "\"\"")}\"";
|
||
|
|
return value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|