2022-07-23 19:23:32 +00:00
|
|
|
|
using log4net;
|
|
|
|
|
using log4net.Appender;
|
|
|
|
|
using log4net.Config;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace MSFSPopoutPanelManager.Shared
|
|
|
|
|
{
|
|
|
|
|
public class FileLogger
|
|
|
|
|
{
|
2024-02-28 02:44:21 +00:00
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.DeclaringType);
|
2024-07-28 00:12:07 +00:00
|
|
|
|
|
2022-07-23 19:23:32 +00:00
|
|
|
|
static FileLogger()
|
|
|
|
|
{
|
|
|
|
|
// Setup log4Net
|
|
|
|
|
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
|
|
|
|
|
|
|
|
|
|
// Need to use AppContext.BaseDirectory for Single File Publishing to work
|
|
|
|
|
// https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md#api-semantics
|
|
|
|
|
XmlConfigurator.Configure(logRepository, new FileInfo(Path.Combine(AppContext.BaseDirectory, "log4net.config")));
|
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
if (LogManager.GetRepository(Assembly.GetEntryAssembly()).GetAppenders().First() is not RollingFileAppender errorLogAppender)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-07-28 00:12:07 +00:00
|
|
|
|
errorLogAppender.File = FileIo.GetErrorLogFilePath(UseApplicationDataPath);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
errorLogAppender.ActivateOptions();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 00:12:07 +00:00
|
|
|
|
public static bool UseApplicationDataPath { get; set; } = false;
|
|
|
|
|
|
2022-07-23 19:23:32 +00:00
|
|
|
|
public static void WriteLog(string message, StatusMessageType messageType)
|
|
|
|
|
{
|
|
|
|
|
switch (messageType)
|
|
|
|
|
{
|
|
|
|
|
case StatusMessageType.Error:
|
|
|
|
|
Log.Error(message);
|
|
|
|
|
break;
|
2024-07-28 00:12:07 +00:00
|
|
|
|
//case StatusMessageType.Info:
|
|
|
|
|
// Log.Info(message);
|
|
|
|
|
// break;
|
|
|
|
|
//case StatusMessageType.Debug:
|
|
|
|
|
// Log.Debug(message);
|
|
|
|
|
// break;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void WriteException(string message, Exception exception)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(message, exception);
|
|
|
|
|
}
|
2024-07-28 00:12:07 +00:00
|
|
|
|
|
|
|
|
|
public static void CloseFileLogger()
|
|
|
|
|
{
|
|
|
|
|
LogManager.ShutdownRepository();
|
|
|
|
|
}
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|