2017-10-26 00:55:04 +02:00
|
|
|
|
using System;
|
2018-05-01 22:55:47 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NLog.Config;
|
|
|
|
|
using NLog.Targets;
|
|
|
|
|
using SumoLogic.Logging.NLog;
|
2017-09-27 22:48:09 +02:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Lib.Logger
|
2017-09-27 22:48:09 +02:00
|
|
|
|
{
|
|
|
|
|
public class LoggerFactory
|
|
|
|
|
{
|
2018-05-03 00:56:06 +02:00
|
|
|
|
public static NLog.Logger CreateNLog(RunParameters runParameters, bool sumologicActive)
|
2017-09-27 22:48:09 +02:00
|
|
|
|
{
|
2018-05-01 22:55:47 +02:00
|
|
|
|
var config = new LoggingConfiguration();
|
|
|
|
|
|
2018-05-01 16:50:48 +02:00
|
|
|
|
if (sumologicActive)
|
2018-01-15 19:31:49 +01:00
|
|
|
|
{
|
2018-01-20 03:30:42 +01:00
|
|
|
|
Console.WriteLine("Logging Geekbot Logs to Sumologic");
|
2018-05-01 22:55:47 +02:00
|
|
|
|
config.LoggingRules.Add(
|
2018-05-03 21:20:49 +02:00
|
|
|
|
new LoggingRule("*", LogLevel.Debug, LogLevel.Fatal,
|
2018-05-06 23:28:44 +02:00
|
|
|
|
new SumoLogicTarget()
|
2018-05-01 22:55:47 +02:00
|
|
|
|
{
|
|
|
|
|
Url = Environment.GetEnvironmentVariable("GEEKBOT_SUMO"),
|
|
|
|
|
SourceName = "GeekbotLogger",
|
|
|
|
|
Layout = "${message}",
|
2018-05-06 23:28:44 +02:00
|
|
|
|
UseConsoleLog = false,
|
2018-05-01 22:55:47 +02:00
|
|
|
|
OptimizeBufferReuse = true,
|
2018-05-06 23:28:44 +02:00
|
|
|
|
Name = "Geekbot",
|
|
|
|
|
AppendException = false
|
2018-05-01 22:55:47 +02:00
|
|
|
|
})
|
|
|
|
|
);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-05-02 20:19:11 +02:00
|
|
|
|
var minLevel = runParameters.Verbose ? LogLevel.Trace : LogLevel.Info;
|
2018-05-01 22:55:47 +02:00
|
|
|
|
config.LoggingRules.Add(
|
2018-05-10 00:00:51 +02:00
|
|
|
|
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
2018-05-01 22:55:47 +02:00
|
|
|
|
new ColoredConsoleTarget
|
|
|
|
|
{
|
|
|
|
|
Name = "Console",
|
2018-05-03 21:20:49 +02:00
|
|
|
|
Encoding = Encoding.UTF8,
|
2018-05-01 22:55:47 +02:00
|
|
|
|
Layout = "[${longdate} ${level:format=FirstCharacter}] ${message} ${exception:format=toString}"
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
config.LoggingRules.Add(
|
2018-05-02 20:19:11 +02:00
|
|
|
|
new LoggingRule("*", minLevel, LogLevel.Fatal,
|
2018-05-01 22:55:47 +02:00
|
|
|
|
new FileTarget
|
|
|
|
|
{
|
|
|
|
|
Name = "File",
|
|
|
|
|
Layout = "[${longdate} ${level}] ${message}",
|
2018-05-03 21:20:49 +02:00
|
|
|
|
Encoding = Encoding.UTF8,
|
2018-05-01 22:55:47 +02:00
|
|
|
|
LineEnding = LineEndingMode.Default,
|
|
|
|
|
MaxArchiveFiles = 30,
|
|
|
|
|
ArchiveNumbering = ArchiveNumberingMode.Date,
|
|
|
|
|
ArchiveEvery = FileArchivePeriod.Day,
|
|
|
|
|
ArchiveFileName = "./Logs/Archive/{#####}.log",
|
|
|
|
|
FileName = "./Logs/Geekbot.log"
|
|
|
|
|
})
|
|
|
|
|
);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
}
|
2018-05-01 22:55:47 +02:00
|
|
|
|
|
|
|
|
|
var loggerConfig = new LogFactory { Configuration = config };
|
|
|
|
|
return loggerConfig.GetCurrentClassLogger();
|
2017-09-27 22:48:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|