Search code examples
xamarinxamarin.formsserilog

In android, I am not able to create a folder or save the log details in a specific file when using serilog.sinks.RollingFile


To get error log details in the xamarin project i installed serilog.sinks.xamarin nuget package in my project. In android, I tried serilog.sinks.rollingfile to store the log details in the documents folder. This is my code

Log.Logger=new LoggerConfiguration().WriteTo.RollingFile(Path.Combine(Environment.DirectoryDocuments,"AndroidErrorLog.txt"))
.WriteTo.AndroidLog().CreateLogger();

After executing the project, if I check the documents folder no file are created and no logs details are found. Can any one tell what mistake I have done and how to fix this issue?.


Solution

  • Log.Logger = new LoggerConfiguration()
                    .WriteTo.File(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "JCA_log-{Date}.txt")
                    , outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}")
                    .WriteTo.AndroidLog()
                    .CreateLogger();
    

    Taken from PhillipOGorman from the following sources:

    I had this exact issue. I think it was something in the constructor that seemed to stop Serilog from creating a file.

    Note #1: The Serilog code provided relies on the Sinks.File and Sinks.Xamarin.Droid Serilog libraries.

    Note #2: He used: Android.OS.Environment.ExternalStorageDirectory.AbsolutePath when referencing where the file would write to but, Environment.GetFolderPath(Environment.SpecialFolder.Personal) works fine (plus it packages it within your apps sandbox.)