Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/server/Logging/ServerLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public class ServerLoggerConfiguration
{
[LogLevel.Information] = ConsoleColor.Green
};

/// <summary>
/// The set of logging levels that are written to the log file on disk. This is
/// independent of LogLevels above (which gates both the dashboard's live entry list and
/// the file write) -- if this dictionary is non-empty, only levels present as keys here
/// are written to disk, while the dashboard continues to show everything permitted by
/// LogLevels. If this dictionary is empty (the default), file writing falls back to the
/// same set as LogLevels, preserving the original behaviour.
/// </summary>
public Dictionary<LogLevel, ConsoleColor> FileLogLevels { get; set; } = new();
}

/// <summary>
Expand Down Expand Up @@ -214,15 +224,18 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
category = category,
label = label,
exception = exception?.ToString() ?? string.Empty
});
}, logLevel);
}

/// <summary>
/// Stores a log entry. Currently this means "adds to a limited, quick access list" and
/// "writes to file".
/// </summary>
/// <param name="entry"></param>
private void StoreLogEntry(LogEntry entry)
/// <param name="logLevel">The log level of this entry, used to independently gate whether
/// it's written to the file (see FileLogLevels on ServerLoggerConfiguration) separately
/// from whether it's added to the in-memory list the dashboard reads from.</param>
private void StoreLogEntry(LogEntry entry, LogLevel logLevel)
{
// This used to be locked, but Lists are *so* fast that we're not going to be able to
// cause lock contention. Even if we do, it's non critical. Just move on.
Expand All @@ -240,7 +253,17 @@ private void StoreLogEntry(LogEntry entry)
}
catch { }

StoreInFile(entry);
_config ??= _getCurrentConfig();

// Empty FileLogLevels means "not configured" -- fall back to the same set as
// LogLevels, preserving the original (pre-patch) behaviour of writing everything
// that's shown live to the file as well.
bool writeToFile = _config.FileLogLevels.Count == 0
? _config.LogLevels.ContainsKey(logLevel)
: _config.FileLogLevels.ContainsKey(logLevel);

if (writeToFile)
StoreInFile(entry);
}

private void StoreInFile(LogEntry logEntry)
Expand Down
12 changes: 9 additions & 3 deletions src/server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// will be logged. The order of log levels is:
// Trace = 0, Debug = 1, Information = 2, Warning = 3, Error = 4, Critical = 5, and None = 6.
"LogLevel": {
"Default": "Trace",
"Default": "Information",
"Microsoft": "Error",
"System": "Error"
},
Expand All @@ -16,12 +16,18 @@
"FileTemplate": "log-%Y-%m-%d.txt", // Template for daily logging file
"MaxLogsToStoreMB": 20, // Maximum amount of logs to store in MB. Once this is hit old log files get dumped (not trimmed, dumped)
"LogLevels": {
"Debug": "Yellow",
"Trace": "Gray",
"Information": "White",
"Warning": "Cyan",
"Error": "Red",
"Critical": "Magenta"
},
// Independent gate for what gets written to disk (see FileLogLevels on
// ServerLoggerConfiguration). If empty, falls back to LogLevels above. Colors here are
// unused for file output but kept as the dictionary value type for consistency.
"FileLogLevels": {
"Warning": "Cyan",
"Error": "Red",
"Critical": "Magenta"
}
}
},
Expand Down