v2rayN/v2rayN/ServiceLib/Common/Logging.cs

51 lines
1.4 KiB
C#
Raw Normal View History

2025-01-30 09:10:05 +00:00
using NLog;
2023-01-01 11:42:01 +00:00
using NLog.Config;
using NLog.Targets;
2021-12-19 12:47:58 +00:00
2024-08-19 10:15:54 +00:00
namespace ServiceLib.Common
2021-12-19 12:47:58 +00:00
{
public class Logging
{
public static void Setup()
{
2023-02-19 05:34:22 +00:00
LoggingConfiguration config = new();
FileTarget fileTarget = new();
2023-01-01 11:42:01 +00:00
config.AddTarget("file", fileTarget);
fileTarget.Layout = "${longdate}-${level:uppercase=true} ${message}";
2024-03-26 06:26:03 +00:00
fileTarget.FileName = Utils.GetLogPath("${shortdate}.txt");
2023-01-01 11:42:01 +00:00
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, fileTarget));
LogManager.Configuration = config;
2021-12-19 12:47:58 +00:00
}
2023-04-14 12:49:36 +00:00
2023-03-20 07:25:58 +00:00
public static void LoggingEnabled(bool enable)
{
if (!enable)
{
LogManager.SuspendLogging();
}
}
2022-04-03 09:01:45 +00:00
2024-01-10 02:43:48 +00:00
public static void SaveLog(string strContent)
{
2025-01-30 09:10:05 +00:00
if (!LogManager.IsLoggingEnabled())
return;
LogManager.GetLogger("Log1").Info(strContent);
2024-01-10 02:43:48 +00:00
}
public static void SaveLog(string strTitle, Exception ex)
{
2025-01-30 09:10:05 +00:00
if (!LogManager.IsLoggingEnabled())
return;
var logger = LogManager.GetLogger("Log2");
logger.Debug($"{strTitle},{ex.Message}");
logger.Debug(ex.StackTrace);
if (ex?.InnerException != null)
2024-01-10 02:43:48 +00:00
{
logger.Error(ex.InnerException);
2024-01-10 02:43:48 +00:00
}
}
2021-12-19 12:47:58 +00:00
}
2025-01-30 09:10:05 +00:00
}