Replace Serilog with NLog
This commit is contained in:
parent
ec084a124b
commit
d3fcfc8a5c
3 changed files with 62 additions and 29 deletions
|
@ -24,17 +24,15 @@
|
||||||
<PackageReference Include="Nancy" Version="2.0.0-clinteastwood" />
|
<PackageReference Include="Nancy" Version="2.0.0-clinteastwood" />
|
||||||
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0-clinteastwood" />
|
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0-clinteastwood" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
||||||
|
<PackageReference Include="NLog" Version="4.5.3" />
|
||||||
|
<PackageReference Include="NLog.Config" Version="4.5.3" />
|
||||||
<PackageReference Include="Overwatch.Net" Version="3.1.0" />
|
<PackageReference Include="Overwatch.Net" Version="3.1.0" />
|
||||||
<PackageReference Include="PokeApi.NET" Version="1.1.0" />
|
<PackageReference Include="PokeApi.NET" Version="1.1.0" />
|
||||||
<PackageReference Include="Serilog" Version="2.6.0" />
|
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
|
||||||
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.1-dev-00044" />
|
|
||||||
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />
|
|
||||||
<PackageReference Include="Serilog.Sinks.SumoLogic" Version="2.3.0" />
|
|
||||||
<PackageReference Include="SharpRaven" Version="2.3.2" />
|
<PackageReference Include="SharpRaven" Version="2.3.2" />
|
||||||
<PackageReference Include="StackExchange.Redis">
|
<PackageReference Include="StackExchange.Redis">
|
||||||
<Version>1.2.6</Version>
|
<Version>1.2.6</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="SumoLogic.Logging.NLog" Version="1.0.0.2" />
|
||||||
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
||||||
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
|
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
|
||||||
<PackageReference Include="System.Runtime.Serialization.Json">
|
<PackageReference Include="System.Runtime.Serialization.Json">
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
using System;
|
using System;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Serilog;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Lib
|
namespace Geekbot.net.Lib
|
||||||
{
|
{
|
||||||
public class GeekbotLogger : IGeekbotLogger
|
public class GeekbotLogger : IGeekbotLogger
|
||||||
{
|
{
|
||||||
private readonly bool _sumologicActive;
|
private readonly bool _sumologicActive;
|
||||||
private readonly ILogger _serilog;
|
private readonly NLog.Logger _logger;
|
||||||
private readonly JsonSerializerSettings _serializerSettings;
|
private readonly JsonSerializerSettings _serializerSettings;
|
||||||
|
|
||||||
public GeekbotLogger(bool sumologicActive)
|
public GeekbotLogger(bool sumologicActive)
|
||||||
{
|
{
|
||||||
_sumologicActive = sumologicActive;
|
_sumologicActive = sumologicActive;
|
||||||
_serilog = LoggerFactory.CreateLogger(sumologicActive);
|
_logger = LoggerFactory.CreateNLog(sumologicActive);
|
||||||
_serializerSettings = new JsonSerializerSettings
|
_serializerSettings = new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
|
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
|
||||||
|
@ -24,29 +23,22 @@ namespace Geekbot.net.Lib
|
||||||
|
|
||||||
public void Debug(string source, string message, object extra = null)
|
public void Debug(string source, string message, object extra = null)
|
||||||
{
|
{
|
||||||
_serilog.Debug(CreateLogString("Debug", source, message, null, extra));
|
_logger.Debug(CreateLogString("Debug", source, message, null, extra));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Information(string source, string message, object extra = null)
|
public void Information(string source, string message, object extra = null)
|
||||||
{
|
{
|
||||||
_serilog.Information(CreateLogString("Information", source, message, null, extra));
|
_logger.Info(CreateLogString("Information", source, message, null, extra));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Warning(string source, string message, Exception stackTrace = null, object extra = null)
|
public void Warning(string source, string message, Exception stackTrace = null, object extra = null)
|
||||||
{
|
{
|
||||||
_serilog.Warning(CreateLogString("Warning", source, message, stackTrace, extra));
|
_logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Error(string source, string message, Exception stackTrace, object extra = null)
|
public void Error(string source, string message, Exception stackTrace, object extra = null)
|
||||||
{
|
{
|
||||||
_serilog.Error(CreateLogString("Error", source, message, stackTrace, extra));
|
_logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra));
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null)
|
|
||||||
{
|
|
||||||
var logJson = CreateLogString(type, source, message, stackTrace, extra);
|
|
||||||
// fuck serilog
|
|
||||||
_serilog.Information(logJson, stackTrace + "}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null)
|
private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null)
|
||||||
|
|
|
@ -1,27 +1,70 @@
|
||||||
using System;
|
using System;
|
||||||
using Serilog;
|
using System.Text;
|
||||||
using Serilog.Sinks.SumoLogic;
|
using NLog;
|
||||||
|
using NLog.Config;
|
||||||
|
using NLog.Targets;
|
||||||
|
using SumoLogic.Logging.NLog;
|
||||||
|
|
||||||
namespace Geekbot.net.Lib
|
namespace Geekbot.net.Lib
|
||||||
{
|
{
|
||||||
public class LoggerFactory
|
public class LoggerFactory
|
||||||
{
|
{
|
||||||
public static ILogger CreateLogger(bool sumologicActive)
|
public static Logger CreateNLog(bool sumologicActive)
|
||||||
{
|
{
|
||||||
var loggerCreation = new LoggerConfiguration();
|
var config = new LoggingConfiguration();
|
||||||
var template = "{Message}{NewLine}";
|
|
||||||
if (sumologicActive)
|
if (sumologicActive)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Logging Geekbot Logs to Sumologic");
|
Console.WriteLine("Logging Geekbot Logs to Sumologic");
|
||||||
loggerCreation.WriteTo.SumoLogic(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"),
|
config.LoggingRules.Add(
|
||||||
outputTemplate: template);
|
new LoggingRule("*", LogLevel.Info, LogLevel.Fatal,
|
||||||
|
new BufferedSumoLogicTarget()
|
||||||
|
{
|
||||||
|
Url = Environment.GetEnvironmentVariable("GEEKBOT_SUMO"),
|
||||||
|
SourceName = "GeekbotLogger",
|
||||||
|
Layout = "${message}",
|
||||||
|
UseConsoleLog = false,
|
||||||
|
MaxQueueSizeBytes = 500000,
|
||||||
|
FlushingAccuracy = 250,
|
||||||
|
MaxFlushInterval = 10000,
|
||||||
|
OptimizeBufferReuse = true,
|
||||||
|
MessagesPerRequest = 10,
|
||||||
|
RetryInterval = 5000,
|
||||||
|
Name = "Geekbot"
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
loggerCreation.WriteTo.LiterateConsole(outputTemplate: template);
|
config.LoggingRules.Add(
|
||||||
loggerCreation.WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true, outputTemplate: template);
|
new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal,
|
||||||
|
new ColoredConsoleTarget
|
||||||
|
{
|
||||||
|
Name = "Console",
|
||||||
|
Encoding = Encoding.Unicode,
|
||||||
|
Layout = "[${longdate} ${level:format=FirstCharacter}] ${message} ${exception:format=toString}"
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
config.LoggingRules.Add(
|
||||||
|
new LoggingRule("*", LogLevel.Trace, LogLevel.Fatal,
|
||||||
|
new FileTarget
|
||||||
|
{
|
||||||
|
Name = "File",
|
||||||
|
Layout = "[${longdate} ${level}] ${message}",
|
||||||
|
Encoding = Encoding.Unicode,
|
||||||
|
LineEnding = LineEndingMode.Default,
|
||||||
|
MaxArchiveFiles = 30,
|
||||||
|
ArchiveNumbering = ArchiveNumberingMode.Date,
|
||||||
|
ArchiveEvery = FileArchivePeriod.Day,
|
||||||
|
ArchiveFileName = "./Logs/Archive/{#####}.log",
|
||||||
|
FileName = "./Logs/Geekbot.log"
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return loggerCreation.CreateLogger();
|
|
||||||
|
var loggerConfig = new LogFactory { Configuration = config };
|
||||||
|
return loggerConfig.GetCurrentClassLogger();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue