Hello,
Serilog
, Serilog.Extensions.Logging
and Serilog.Sinks.File
are third-party tools and not supported on Q&A. Besides the log feature, your main problem is that you cannot find the file in the path.
Please test like the following:
var dicPath = Path.Combine(FileSystem.AppDataDirectory, "logs");
var logFilePath = Path.Combine(dicPath, "log.txt");
if (!Directory.Exists(dicPath))
{
Directory.CreateDirectory(dicPath);
}
if (File.Exists(logFilePath))
{// if the file exists, read the content and set new log
var logContent = File.ReadAllText(logFilePath);
Debug.WriteLine("logContent:>>" + logContent);// just debug the value, it won't write the file, please use File.WriteAllText method to write
File.WriteAllText(""xxx new log content"", logFilePath);
}
else
{//if the file doesn't exist, create the file and set the log txt
Debug.WriteLine("Log file not found.");
File.Create(logFilePath);// if the file is not in the path, create this file
File.WriteAllText(logFilePath, "1345test");// save text in log.txt
}
After testing, the text can be written to the file path and can be read. The problem is on the following code:
Log.Logger = new LoggerConfiguration()
.WriteTo.File(
path: Path.Combine(logDirectory, "log.txt"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 7, // Keep logs for the last 7 days
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information
)
Since this is a third-party tool issue, please contact them for further help.
Update
This issue was solved, refer to - https://stackoverflow.com/questions/79345097/maui-serilog-log-file-is-not-creating
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.