I am using NLog for logging in xamarin forms application, config settings are archive everyday, archive numbering set to rolling, concurrent writes set to true, max archiving 15.
The problem is nlog is writing the archived content to the current log file with actual log appended to it.
Check these links too
Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!
Can you show your NLog.config ?
The easy solution is just doing this:
<target type="file" filename="logfile_${shortdate}.txt" maxArchiveFiles="7" />
Then NLog will not perform File.Move which apparently fails when building application with latest Xamarin-tools. But will only do File.Delete.
const string fileName = "log.txt";
const string archiveFileName = "log.{#}.txt";
var config = new LoggingConfiguration();
var consoleTarget = new ConsoleTarget();
config.AddTarget("console", consoleTarget);
var consoleRule = new LoggingRule("*", LogLevel.Trace, consoleTarget);
config.LoggingRules.Add(consoleRule);
var fileTarget = new FileTarget
{
ArchiveEvery = FileArchivePeriod.Day,
ArchiveNumbering = ArchiveNumberingMode.Rolling,
ConcurrentWrites = true,
MaxArchiveFiles = 15,
Layout = "${longdate} ${message} ${exception:format=tostring}"
};
if (Android.OS.Environment.IsExternalStorageEmulated)
{
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
fileTarget.FileName = Path.Combine(sdCardPath, fileName);
fileTarget.ArchiveFileName = Path.Combine(sdCardPath, archiveFileName);
}
else
{
var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
fileTarget.FileName = Path.Combine(Directory.Exists(folder) ? folder : Directory.CreateDirectory(folder).FullName, fileName);
fileTarget.ArchiveFileName = Path.Combine(Directory.Exists(folder) ? folder : Directory.CreateDirectory(folder).FullName, archiveFileName);
}
config.AddTarget("file", fileTarget);
var warningRule = new LoggingRule("*", LogLevel.Warn, fileTarget);
var infoRule = new LoggingRule("*", LogLevel.Info, fileTarget);
config.LoggingRules.Add(warningRule);
config.LoggingRules.Add(infoRule);
LogManager.Configuration = config;
Either fix Xamarin tools so you can do File.Move or do this:
const string fileName = "log-${shortdate}.txt";
And remove all archive parameters except maxArchiveFiles = 15
@snakefoot
What you mean by Fix Xamarin Tool? How I can fix it.
Could you please let us know, how we need to update Xamarin Config?
It is very difficult to read the information from the answer you provided.
Like File.Move()
Remove All Archive Parameters etc etc.
How we can do all these things.?? Could you please provide some code samples to fix.
What you mean by Fix Xamarin Tool? How I can fix it.
Unfortunately we cannot fix this as this issue is part of the Xamarin tooling. The issue is reported here (The bug-link also explains about File.Move stops working)
How we can do all these things?
@talk2stephan Are you a co-worker to @hashmurali and using the exact configuration? If not then I really suggest that you create your own new issue.
But for the code above:
c#
const string fileName = "log-${shortdate}.txt";
var fileTarget = new FileTarget
{
Layout = "${longdate} ${message} ${exception:format=tostring}"
ConcurrentWrites = false,
MaxArchiveFiles = 15,
};
Remember also to remove the assignment of ArchiveFileName. Then NLog will not call File.Move but only File.Delete.
Bug is fixed with Visual Studio 2019 version 16.4. To get the new version that includes the fix, check for the latest updates or install the latest version from https://visualstudio.microsoft.com/downloads/.
(Fix also included on macOS in Visual Studio 2019 for Mac version 8.4 Preview 2.1 and higher. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.)