Files
ArchiSteamFarm/ArchiSteamFarm/Logging.cs

143 lines
4.9 KiB
C#
Raw Normal View History

2015-10-28 19:21:27 +01:00
/*
_ _ _ ____ _ _____
/ \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
/ _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
/ ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
2016-01-16 04:21:36 +01:00
Copyright 2015-2016 Łukasz "JustArchi" Domeradzki
2015-10-28 19:21:27 +01:00
Contact: JustArchi@JustArchi.net
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
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.
*/
using System;
2016-06-19 12:20:12 +02:00
using System.Diagnostics;
2016-05-13 19:20:24 +02:00
using System.Diagnostics.CodeAnalysis;
2015-10-25 06:16:50 +01:00
using System.Runtime.CompilerServices;
using NLog;
using NLog.Config;
using NLog.Targets;
2015-10-25 06:16:50 +01:00
namespace ArchiSteamFarm {
2016-06-10 00:49:44 +02:00
internal static class Logging {
private const string Layout = @"${date:format=yyyy-MM-dd HH\:mm\:ss}|${level:uppercase=true}|${message}${onexception:inner= ${exception:format=toString,Data}}";
2016-06-10 00:49:44 +02:00
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2016-06-10 00:49:44 +02:00
internal static void Init() {
LoggingConfiguration config = new LoggingConfiguration();
2016-06-10 00:49:44 +02:00
if (Program.GlobalConfig.LogToFile) {
FileTarget fileTarget = new FileTarget("File") {
2016-07-04 19:25:01 +02:00
DeleteOldFileOnStartup = true,
FileName = Program.LogFile,
Layout = Layout
};
config.AddTarget(fileTarget);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, fileTarget));
2016-06-10 00:49:44 +02:00
}
if (Program.IsRunningAsService) {
EventLogTarget eventLogTarget = new EventLogTarget("EventLog") {
Layout = Layout
};
config.AddTarget(eventLogTarget);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, eventLogTarget));
2016-06-10 00:49:44 +02:00
}
ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget("Console") {
Layout = Layout
};
config.AddTarget(consoleTarget);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, consoleTarget));
LogManager.Configuration = config;
2016-06-10 00:49:44 +02:00
}
// Ideally, those two should disable/enable only console logging
// But for some reason removing console rule/target doesn't seem to work
internal static void OnUserInputStart() => LogManager.DisableLogging();
internal static void OnUserInputEnd() => LogManager.EnableLogging();
2016-06-10 00:49:44 +02:00
internal static void LogGenericError(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message), botName);
return;
}
Logger.Error($"{botName}|{previousMethodName}() {message}");
2016-06-10 00:49:44 +02:00
}
internal static void LogGenericException(Exception exception, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
if (exception == null) {
LogNullError(nameof(exception), botName);
return;
2016-06-10 00:49:44 +02:00
}
Logger.Error(exception, $"{botName}|{previousMethodName}()");
2016-06-10 00:49:44 +02:00
}
internal static void LogFatalException(Exception exception, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
if (exception == null) {
LogNullError(nameof(exception), botName);
return;
}
Logger.Fatal(exception, $"{botName}|{previousMethodName}()");
}
2016-06-10 00:49:44 +02:00
internal static void LogGenericWarning(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message), botName);
return;
}
Logger.Warn($"{botName}|{previousMethodName}() {message}");
2016-06-10 00:49:44 +02:00
}
internal static void LogGenericInfo(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message), botName);
return;
}
Logger.Info($"{botName}|{previousMethodName}() {message}");
2016-06-10 00:49:44 +02:00
}
2016-07-04 20:57:33 +02:00
[SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")]
2016-06-10 00:49:44 +02:00
internal static void LogNullError(string nullObjectName, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
if (string.IsNullOrEmpty(nullObjectName)) {
return;
2016-06-10 00:49:44 +02:00
}
2016-07-04 20:57:33 +02:00
LogGenericError(nullObjectName + " is null!", botName, previousMethodName);
2016-06-10 00:49:44 +02:00
}
2016-06-19 12:20:12 +02:00
[Conditional("DEBUG")]
2016-06-10 00:49:44 +02:00
[SuppressMessage("ReSharper", "UnusedMember.Global")]
internal static void LogGenericDebug(string message, string botName = "Main", [CallerMemberName] string previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message), botName);
return;
}
Logger.Debug($"{botName}|{previousMethodName}() {message}");
2016-06-10 00:49:44 +02:00
}
}
2015-10-25 06:16:50 +01:00
}