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
{
///
/// ロガー
///
public static class LoggerUxer
{
///
/// 初期化
///
/// ログINIファイルパス
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()?.Product ?? "不明";
// EXE(呼び出し元)のアセンブリ
var exeAssembly = Assembly.GetEntryAssembly();
var exeVersion = exeAssembly?.GetName().Version?.ToString();
var exeProduct = exeAssembly?.GetCustomAttribute()?.Product ?? "不明";
// ログ出力
Info($"[EXE] 製品名: {exeProduct}, バージョン: {exeVersion}");
Info($"[DLL] 製品名: {dllProduct}, バージョン: {dllVersion}");
}
///
/// ログINI初期化(無ければ)
///
/// ログINIファイルパス
private static void EnsureIniExists(string path)
{
if (!File.Exists(path))
{
File.WriteAllLines(path, new[]
{
"[Logging]",
"LogLevel=Information",
"LogFilePath=log.txt",
"RollingInterval=Day"
});
}
}
}
}