Skip to content

Commit ec1d1ce

Browse files
Retention policy for log files
1 parent 2134091 commit ec1d1ce

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

nexus_config/Internal/LoggingConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected virtual void ConfigureNLogDynamically(Microsoft.Extensions.Logging.Log
4747
var nlogConfig = LogManager.Configuration ?? new NLog.Config.LoggingConfiguration();
4848

4949
// Ensure main file target exists
50+
var retentionDays = m_Settings.Get().Logging.RetentionDays;
5051
if (nlogConfig.FindTargetByName("mainFile") is not NLog.Targets.FileTarget)
5152
{
5253
var fileTarget = new NLog.Targets.FileTarget("mainFile")
@@ -55,7 +56,7 @@ protected virtual void ConfigureNLogDynamically(Microsoft.Extensions.Logging.Log
5556
ArchiveFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "archive", "mcp-nexus-${shortdate}-{##}.log"),
5657
ArchiveEvery = NLog.Targets.FileArchivePeriod.Day,
5758
ArchiveSuffixFormat = "{#}",
58-
MaxArchiveFiles = 356,
59+
MaxArchiveFiles = Math.Max(1, retentionDays),
5960
KeepFileOpen = true,
6061
AutoFlush = true,
6162
CreateDirs = true,

nexus_config/Models/LoggingSettings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ public class LoggingSettings
99
/// Gets or sets the log level.
1010
/// </summary>
1111
public string LogLevel { get; set; } = "Information";
12+
13+
/// <summary>
14+
/// Gets or sets the maximum number of archived log files to retain.
15+
/// Each archived file typically represents one day of logs.
16+
/// </summary>
17+
public int RetentionDays { get; set; } = 7;
1218
}

nexus_config/appsettings.Defaults.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"Logging": {
3-
"LogLevel": "Debug"
3+
"LogLevel": "Debug",
4+
"RetentionDays": 7
45
},
56
"McpNexus": {
67
"Server": {

unittests/nexus_config_unittests/Models/SharedConfigurationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,12 @@ public void LoggingSettings_CanBeModified()
316316
var settings = new LoggingSettings
317317
{
318318
LogLevel = "Debug",
319+
RetentionDays = 30,
319320
};
320321

321322
// Assert
322323
_ = settings.LogLevel.Should().Be("Debug");
324+
_ = settings.RetentionDays.Should().Be(30);
323325
}
324326

325327
/// <summary>

0 commit comments

Comments
 (0)