Files
ArchiSteamFarm/ArchiSteamFarm/NLog/Logging.cs

171 lines
5.6 KiB
C#
Raw Normal View History

2017-11-18 17:27:06 +01:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
//
2018-07-27 04:52:14 +02:00
// Copyright 2015-2018 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
2017-11-18 17:27:06 +01:00
//
2018-07-27 04:52:14 +02:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2017-11-18 17:27:06 +01:00
//
2018-07-27 04:52:14 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2015-10-28 19:21:27 +01:00
2016-07-04 21:58:50 +02:00
using System.Linq;
2018-09-08 01:03:55 +02:00
using ArchiSteamFarm.Collections;
using ArchiSteamFarm.IPC;
using NLog;
using NLog.Config;
using NLog.Targets;
2015-10-25 06:16:50 +01:00
2018-09-08 01:03:55 +02:00
namespace ArchiSteamFarm.NLog {
2016-06-10 00:49:44 +02:00
internal static class Logging {
private const string GeneralLayout = @"${date:format=yyyy-MM-dd HH\:mm\:ss}|${processname}-${processid}|${level:uppercase=true}|" + LayoutMessage;
private const string LayoutMessage = @"${logger}|${message}${onexception:inner= ${exception:format=toString,Data}}";
2016-06-10 00:49:44 +02:00
private static readonly ConcurrentHashSet<LoggingRule> ConsoleLoggingRules = new ConcurrentHashSet<LoggingRule>();
2016-06-10 00:49:44 +02:00
2017-06-26 01:37:14 +02:00
private static bool IsUsingCustomConfiguration;
2016-08-06 22:16:46 +02:00
private static bool IsWaitingForUserInput;
2017-06-26 01:39:22 +02:00
internal static void EnableTraceLogging() {
2017-06-26 01:37:14 +02:00
if (IsUsingCustomConfiguration || (LogManager.Configuration == null)) {
return;
}
bool reload = false;
foreach (LoggingRule rule in LogManager.Configuration.LoggingRules.Where(rule => rule.IsLoggingEnabledForLevel(LogLevel.Debug) && !rule.IsLoggingEnabledForLevel(LogLevel.Trace))) {
rule.EnableLoggingForLevel(LogLevel.Trace);
reload = true;
}
if (reload) {
LogManager.ReconfigExistingLoggers();
}
}
2018-01-31 02:42:23 +01:00
internal static void InitCoreLoggers() {
if (LogManager.Configuration != null) {
2017-06-26 01:37:14 +02:00
IsUsingCustomConfiguration = true;
2016-07-04 22:03:12 +02:00
InitConsoleLoggers();
LogManager.ConfigurationChanged += OnConfigurationChanged;
return;
}
2018-05-17 09:54:22 +02:00
ConfigurationItemFactory.Default.ParseMessageTemplates = false;
LoggingConfiguration config = new LoggingConfiguration();
2016-06-10 00:49:44 +02:00
2018-05-17 09:54:22 +02:00
ColoredConsoleTarget coloredConsoleTarget = new ColoredConsoleTarget("ColoredConsole") { Layout = GeneralLayout };
2016-09-21 19:26:20 +02:00
config.AddTarget(coloredConsoleTarget);
2016-11-06 12:19:43 +01:00
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, coloredConsoleTarget));
FileTarget fileTarget = new FileTarget("File") {
DeleteOldFileOnStartup = true,
FileName = SharedInfo.LogFile,
Layout = GeneralLayout
};
config.AddTarget(fileTarget);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, fileTarget));
2016-08-06 22:16:46 +02:00
LogManager.Configuration = config;
InitConsoleLoggers();
2016-06-10 00:49:44 +02:00
}
2018-01-31 02:42:23 +01:00
internal static void InitHistoryLogger() {
if (LogManager.Configuration == null) {
2018-01-31 02:42:23 +01:00
return;
}
2018-07-24 20:43:00 +02:00
HistoryTarget historyTarget = LogManager.Configuration.AllTargets.OfType<HistoryTarget>().FirstOrDefault();
2018-07-24 20:43:00 +02:00
if ((historyTarget == null) && !IsUsingCustomConfiguration) {
// TODO: We could use some nice HTML layout for this
historyTarget = new HistoryTarget("History") {
Layout = GeneralLayout,
MaxCount = 20
};
LogManager.Configuration.AddTarget(historyTarget);
LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, historyTarget));
2018-01-31 02:42:23 +01:00
LogManager.ReconfigExistingLoggers();
}
2018-01-31 02:42:23 +01:00
ArchiKestrel.OnNewHistoryTarget(historyTarget);
2018-01-31 02:42:23 +01:00
}
internal static void OnUserInputEnd() {
IsWaitingForUserInput = false;
2016-07-04 21:58:50 +02:00
if (ConsoleLoggingRules.Count == 0) {
return;
}
bool reconfigure = false;
2018-05-17 09:54:22 +02:00
foreach (LoggingRule consoleLoggingRule in ConsoleLoggingRules.Where(consoleLoggingRule => !LogManager.Configuration.LoggingRules.Contains(consoleLoggingRule))) {
LogManager.Configuration.LoggingRules.Add(consoleLoggingRule);
reconfigure = true;
2016-07-04 21:58:50 +02:00
}
if (reconfigure) {
2018-05-17 09:54:22 +02:00
LogManager.ReconfigExistingLoggers();
}
2016-07-04 21:58:50 +02:00
}
internal static void OnUserInputStart() {
IsWaitingForUserInput = true;
2016-07-04 21:58:50 +02:00
if (ConsoleLoggingRules.Count == 0) {
return;
}
bool reconfigure = false;
2018-05-17 09:54:22 +02:00
2018-05-17 09:55:38 +02:00
foreach (LoggingRule consoleLoggingRule in ConsoleLoggingRules) {
if (LogManager.Configuration.LoggingRules.Remove(consoleLoggingRule)) {
reconfigure = true;
2018-05-17 09:54:22 +02:00
}
2016-07-04 21:58:50 +02:00
}
if (reconfigure) {
2018-05-17 09:54:22 +02:00
LogManager.ReconfigExistingLoggers();
}
2016-07-04 21:58:50 +02:00
}
2016-07-04 22:03:12 +02:00
private static void InitConsoleLoggers() {
ConsoleLoggingRules.Clear();
2018-05-17 09:54:22 +02:00
2016-09-21 19:26:20 +02:00
foreach (LoggingRule loggingRule in LogManager.Configuration.LoggingRules.Where(loggingRule => loggingRule.Targets.Any(target => target is ColoredConsoleTarget || target is ConsoleTarget))) {
2016-07-04 22:03:12 +02:00
ConsoleLoggingRules.Add(loggingRule);
}
}
private static void OnConfigurationChanged(object sender, LoggingConfigurationChangedEventArgs e) {
if ((sender == null) || (e == null)) {
ASF.ArchiLogger.LogNullError(nameof(sender) + " || " + nameof(e));
return;
}
InitConsoleLoggers();
if (IsWaitingForUserInput) {
OnUserInputStart();
}
2018-01-31 02:42:23 +01:00
2018-07-24 20:43:00 +02:00
HistoryTarget historyTarget = LogManager.Configuration.AllTargets.OfType<HistoryTarget>().FirstOrDefault();
ArchiKestrel.OnNewHistoryTarget(historyTarget);
}
2016-06-10 00:49:44 +02:00
}
}