MAUI: Serilog log file is not creating

Sreejith Sreenivasan 1,001 Reputation points
2025-01-14T13:05:45.6333333+00:00

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

I did a test log like below in another page like below.

Log.Information("Test log!");

  1. Finally I tried reading the log details like below to send it to my server.
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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,023 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,246 Reputation points Microsoft External Staff
    2025-01-15T06:31:34.7366667+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.