case 4690 - NLog 6.2.2: archive suffix keeps log-ayanova-N.txt names and cleans up old archives; log viewer sorted by date

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-22 17:31:01 -07:00
parent 4098cd4922
commit 91f59d8554
4 changed files with 15 additions and 7 deletions

View File

@@ -48,8 +48,8 @@
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="10.0.13" />
<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.3.8" />
<PackageReference Include="MiniProfiler.EntityFrameworkCore" Version="4.3.8" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.11" />
<PackageReference Include="NLog" Version="6.2.2" />
<PackageReference Include="NLog.Web.AspNetCore" Version="6.2.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
<PackageReference Include="PuppeteerSharp" Version="17.0.0" />
<PackageReference Include="QRCoder" Version="1.5.1" />

View File

@@ -117,7 +117,8 @@ namespace AyaNova.Api.Controllers
//Iterate all log files and build return
var files = System.IO.Directory.GetFiles(ServerBootConfig.AYANOVA_LOG_PATH, "log-ayanova*.txt");
var ret = files.Where(z => !z.EndsWith("ayanova.txt")).OrderByDescending(z => z).Select(z => System.IO.Path.GetFileName(z)).ToList();
//newest archive first by file date, archive numbers only increase since NLog 6 so name order breaks past 9
var ret = files.Where(z => !z.EndsWith("ayanova.txt")).OrderByDescending(z => System.IO.File.GetLastWriteTimeUtc(z)).Select(z => System.IO.Path.GetFileName(z)).ToList();
ret.Insert(0, "log-ayanova.txt");
return Ok(ApiOkResponse.Response(ret));
}

View File

@@ -132,10 +132,15 @@ namespace AyaNova
fileTarget.FileName = Path.Combine(ServerBootConfig.AYANOVA_LOG_PATH, "log-ayanova.txt");
fileTarget.Layout = "${longdate}|${uppercase:${level}}|${logger}|${message:exceptionSeparator==>:withException=true}";
//NLog 6: ArchiveFileName must stay set, it keeps log-ayanova.txt as the current file and moves full files to archives
//(without it NLog 6 writes straight into numbered files). The archive number comes from ArchiveSuffixFormat, not {#}:
//"-{0}" keeps the documented log-ayanova-1.txt names and lets MaxArchiveFiles clean up archives from earlier versions
//(the NLog 6 default would be log-ayanova_01.txt and leave the old -0 to -8 files behind forever)
//archive numbers keep increasing (no more Rolling), the un-numbered log-ayanova.txt is always the current one
fileTarget.ArchiveFileName = Path.Combine(ServerBootConfig.AYANOVA_LOG_PATH, "log-ayanova-{#}.txt");
fileTarget.ArchiveNumbering = ArchiveNumberingMode.Rolling;//so expect to see 1 to 10 or 0 to ten as may be
fileTarget.ArchiveAboveSize = 1000000;//1mb maximum file size
fileTarget.MaxArchiveFiles = 9;//max 10 files totalling 10mb, the current log-ayanova.txt and log-ayanova-[0-9].txt
fileTarget.ArchiveSuffixFormat = "-{0}";
fileTarget.ArchiveAboveSize = 1000000;//1mb maximum file size
fileTarget.MaxArchiveFiles = 9;//max 10 files totalling 10mb, the current log-ayanova.txt and the 9 most recent numbered archives
// Step 4. Define rules