Files
HCoreLib/Commons/LoggerUxer.cs
T
2026-06-01 12:28:44 +09:00

86 lines
3.0 KiB
C#

using KssSmaPlaLib.Ini;
using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace KssSmaPlaLib.Commons
{
/// <summary>
/// ロガー
/// </summary>
public static class LoggerUxer
{
/// <summary>
/// 初期化
/// </summary>
/// <param name="iniPath">ログINIファイルパス</param>
public static void Initialize(string iniPath)
{
EnsureIniExists(iniPath);
// var config = IniReferenceResolveUxer.ReadSectionWithReference(iniPath, "Logging");
var level = Enum.TryParse(
//config["LogLevel"],
IniHelper.ReadValue(iniPath, "Logging", "LogLevel"),
out LogEventLevel parsedLevel)
? parsedLevel
: LogEventLevel.Information;
// var path = config["LogFilePath"];
var path = IniHelper.ReadValue(iniPath, "Logging", "LogFilePath");
var rolling = IniHelper.ReadValue(iniPath, "Logging", "RollingInterval");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(level)
.WriteTo.Console()
.WriteTo.File(path, rollingInterval: (RollingInterval)Enum.Parse(typeof(RollingInterval), rolling))
.CreateLogger();
}
public static void Info(string message) => Log.Information(message);
public static void Warn(string message) => Log.Warning(message);
public static void Error(string message) => Log.Error(message);
public static void Close() => Log.CloseAndFlush();
public static void StartInfo()
{
// DLL(自身)のアセンブリ
var dllAssembly = Assembly.GetExecutingAssembly();
var dllVersion = dllAssembly.GetName().Version?.ToString();
var dllProduct = dllAssembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product ?? "不明";
// EXE(呼び出し元)のアセンブリ
var exeAssembly = Assembly.GetEntryAssembly();
var exeVersion = exeAssembly?.GetName().Version?.ToString();
var exeProduct = exeAssembly?.GetCustomAttribute<AssemblyProductAttribute>()?.Product ?? "不明";
// ログ出力
Info($"[EXE] 製品名: {exeProduct}, バージョン: {exeVersion}");
Info($"[DLL] 製品名: {dllProduct}, バージョン: {dllVersion}");
}
/// <summary>
/// ログINI初期化(無ければ)
/// </summary>
/// <param name="path">ログINIファイルパス</param>
private static void EnsureIniExists(string path)
{
if (!File.Exists(path))
{
File.WriteAllLines(path, new[]
{
"[Logging]",
"LogLevel=Information",
"LogFilePath=log.txt",
"RollingInterval=Day"
});
}
}
}
}