I am trying to write all log details to a file and send that to our server using an API. I am using Serilog
for this. I did below steps:
Installed Serilog, Serilog.Extensions.Logging and Serilog.Sinks.File
.
MAUIProgram.cs
added below code:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
var logDirectory = Path.Combine(FileSystem.Current.AppDataDirectory, "logs");
var logFilePath = Path.Combine(logDirectory, "log.txt");
// Ensure the directory exists
if (!Directory.Exists(logDirectory))
{
Directory.CreateDirectory(logDirectory);
}
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
)
.CreateLogger();
// Check if the file is created right after logging configuration
if (File.Exists(logFilePath))
{
Debug.WriteLine($"Log file created: {logFilePath}");
}
else
{
Debug.WriteLine($"Log file not found at expected location: {logFilePath}");
}
// Add Serilog to the logging system
builder.Logging.AddSerilog();
Here the Directory is creating, but the log.txt
file is not creating. I am getting below response in output box:
Log file not found at expected location: /data/user/0/com.companyname.appname/files/logs/log.txt
Log.Information("Test log!");
var logFilePath = Path.Combine(FileSystem.AppDataDirectory, "logs", "log.txt");
Debug.WriteLine("Log file location:>" + logFilePath);
if (File.Exists(logFilePath))
{
var logContent = File.ReadAllText(logFilePath);
Debug.WriteLine("logContent:>>" + logContent);
}
else
{
Debug.WriteLine("Log file not found.");
}
Here also I am getting "Log file not found" message in output box. Am I missing anything in this? I want to write all the logs to that file and I need to send it to the server for debugging the real world issues.
Log file not found at expected location: /data/user/0/com.companyname.appname/files/logs/log.txt
This is because you used rollingInterval: RollingInterval.Day
parameter in the new LoggerConfiguration().WriteTo.File
method and this will format the log file's name as: logyyyyMMdd.txt. And it will create a new log file when you run the appliction on different day.
So the real location should be /data/user/0/com.companyname.appname/files/logs/log20250120.txt. You can check it by the Android Studio's Device File Explorer.
You can delete rollingInterval: RollingInterval.Day
and just use:
Log.Logger = new LoggerConfiguration()
.WriteTo.File(
path: Path.Combine(logDirectory, "log.txt"),
retainedFileCountLimit: 7, // Keep logs for the last 7 days
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information
)
.CreateLogger();
This keeps the log file's name as log.txt and you can read /data/user/0/com.companyname.appname/files/logs/log.txt.
Or you can also format the path when you try to read the log file:
var logFilePath =
Path.Combine(FileSystem.Current.AppDataDirectory, "logs",
"log"+DateTime.Now.ToString("yyyyMMdd")+".txt");
Debug.WriteLine("Log file location:>" + logFilePath);
if (File.Exists(logFilePath))
{
var logContent = File.ReadAllText(logFilePath);
Debug.WriteLine("logContent:>>" + logContent);
}
This will read the /data/user/0/com.companyname.appname/files/logs/log20250120.txt.