Improve file log formatting

This commit is contained in:
Cyberboss
2018-09-25 11:12:29 -04:00
parent 8ba48746ab
commit e910c9875f
6 changed files with 105 additions and 34 deletions
@@ -0,0 +1,34 @@
namespace Tgstation.Server.Host.Configuration
{
/// <summary>
/// File logging configuration options
/// </summary>
sealed class FileLoggingConfiguration
{
/// <summary>
/// The key for the <see cref="Microsoft.Extensions.Configuration.IConfigurationSection"/> the <see cref="FileLoggingConfiguration"/> resides in
/// </summary>
public const string Section = "FileLogging";
/// <summary>
/// Where log files are stored
/// </summary>
public string Directory { get; set; }
/// <summary>
/// If file logging is disabled
/// </summary>
public bool Disable { get; set; }
/// <summary>
/// The <see cref="string"/>ified minimum <see cref="Microsoft.Extensions.Logging.LogLevel"/> to display in logs
/// </summary>
public string LogLevel { get; set; }
/// <summary>
/// The <see cref="string"/>ified minimum <see cref="Microsoft.Extensions.Logging.LogLevel"/> to display in logs for Microsoft library sources
/// </summary>
public string MicrosoftLogLevel { get; set; }
}
}
@@ -10,21 +10,6 @@
/// </summary>
public const string Section = "General";
/// <summary>
/// Where log files are stored
/// </summary>
public string LogFileDirectory { get; set; }
/// <summary>
/// The stringified <see cref="Microsoft.Extensions.Logging.LogLevel"/> for file logging
/// </summary>
public string LogFileLevel { get; set; }
/// <summary>
/// If file logging is disabled
/// </summary>
public bool DisableFileLogging { get; set; }
/// <summary>
/// Minimum length of database user passwords
/// </summary>
+60 -13
View File
@@ -13,6 +13,9 @@ using Microsoft.Extensions.Primitives;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Display;
using System;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
@@ -54,12 +57,6 @@ namespace Tgstation.Server.Host.Core
readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment;
readonly TaskCompletionSource<object> startupTcs;
static LogLevel GetMinimumLogLevel(string stringLevel)
{
if (String.IsNullOrWhiteSpace(stringLevel) || !Enum.TryParse<LogLevel>(stringLevel, out var minimumLevel))
minimumLevel = LogLevel.Information;
return minimumLevel;
}
/// <summary>
/// Construct an <see cref="Application"/>
@@ -89,19 +86,69 @@ namespace Tgstation.Server.Host.Core
services.Configure<UpdatesConfiguration>(configuration.GetSection(UpdatesConfiguration.Section));
var databaseConfigurationSection = configuration.GetSection(DatabaseConfiguration.Section);
services.Configure<DatabaseConfiguration>(databaseConfigurationSection);
var generalConfigurationSection = configuration.GetSection(GeneralConfiguration.Section);
services.Configure<GeneralConfiguration>(generalConfigurationSection);
services.Configure<GeneralConfiguration>(configuration.GetSection(GeneralConfiguration.Section));
//remember, anything you .Get manually can be null if the config is missing
var generalConfiguration = generalConfigurationSection.Get<GeneralConfiguration>();
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var ioManager = new DefaultIOManager();
if (generalConfiguration?.DisableFileLogging != true)
//remember, anything you .Get manually can be null if the config is missing
var fileLoggingConfigurationSection = configuration.GetSection(FileLoggingConfiguration.Section);
var fileLoggingConfiguration = fileLoggingConfigurationSection.Get<FileLoggingConfiguration>();
if (fileLoggingConfiguration?.Disable != true)
{
var logPath = !String.IsNullOrEmpty(generalConfiguration?.LogFileDirectory) ? generalConfiguration.LogFileDirectory : ioManager.ConcatPath(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), VersionPrefix, "Logs");
var logPath = !String.IsNullOrEmpty(fileLoggingConfiguration?.Directory) ? fileLoggingConfiguration.Directory : ioManager.ConcatPath(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), VersionPrefix, "Logs");
services.AddLogging(builder => builder.AddFile(ioManager.ConcatPath(logPath, "tgs-{Date}.log"), GetMinimumLogLevel(generalConfiguration?.LogFileLevel)));
logPath = ioManager.ConcatPath(logPath, "tgs-{Date}.log");
services.AddLogging(builder =>
{
LogLevel GetMinimumLogLevel(string stringLevel)
{
if (String.IsNullOrWhiteSpace(stringLevel) || !Enum.TryParse<LogLevel>(stringLevel, out var minimumLevel))
minimumLevel = LogLevel.Information;
return minimumLevel;
}
LogEventLevel? ConvertLogLevel(LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel.Critical:
return LogEventLevel.Fatal;
case LogLevel.Debug:
return LogEventLevel.Debug;
case LogLevel.Error:
return LogEventLevel.Error;
case LogLevel.Information:
return LogEventLevel.Information;
case LogLevel.Trace:
return LogEventLevel.Verbose;
case LogLevel.Warning:
return LogEventLevel.Warning;
case LogLevel.None:
return null;
default:
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid log level {0}", logLevel));
}
};
var logEventLevel = ConvertLogLevel(GetMinimumLogLevel(fileLoggingConfiguration?.LogLevel));
var microsoftEventLevel = ConvertLogLevel(GetMinimumLogLevel(fileLoggingConfiguration?.MicrosoftLogLevel));
var formatter = new MessageTemplateTextFormatter("{Timestamp:o} {RequestId,13} [{Level:u3}] {SourceContext:l}: {Message} ({EventId:x8}){NewLine}{Exception}", null);
var configuration = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Async(w => w.RollingFile(formatter, logPath, shared: true, flushToDiskInterval: TimeSpan.FromSeconds(2)));
if (logEventLevel.HasValue)
configuration.MinimumLevel.Is(logEventLevel.Value);
if (microsoftEventLevel.HasValue)
configuration.MinimumLevel.Override("Microsoft", microsoftEventLevel.Value);
builder.AddSerilog(configuration.CreateLogger(), true);
});
}
services.AddOptions();
@@ -42,7 +42,9 @@
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
<PackageReference Include="Octokit" Version="0.32.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0-dev-00024" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.3.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="4.5.0" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="4.5.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.2.4" />
@@ -1,5 +1,5 @@
{
"General": {
"DisableFileLogging": true
"FileLogging": {
"Disable": true
}
}
+6 -3
View File
@@ -1,11 +1,14 @@
{
"General": {
"LogFileDirectory": null, //use the default path
"DisableFileLogging": false,
"LogFileLevel": "Debug",
"MinimumPasswordLength": 15,
"GitHubAccessToken": null
},
"FileLogging": {
"Directory": null, //use the default path
"Disable": false,
"LogLevel": "Debug",
"MicrosoftLogLevel": "Warning"
},
"Kestrel": {
"EndPoints": {
"Http": {