MetroLog SQLite target for IOS and Android

PhilipOGormanPhilipOGorman USMember ✭✭✭

Metrolog is great for debugging when connected to the debugger. For windows it has a file target, but for IOS and Android there is no way to persist the debug messages. This would be really useful for beta testers

For windows there is SQLite target: https://github.com/onovotny/MetroLog/tree/dev/MetroLog.Targets.SQLite

I'd like to create and share one that works on IOS and Android, although I have never used SQLite before. I was thinking that if the user could send the db file to the developers we could them view the logs. Would the db file be accessible by a user on IOS and Android?

Thanks,
Philip

Best Answer

Answers

  • Curup1raB77Curup1raB77 USMember
    edited July 2017

    @PhilipOGorman said:
    Nevermind serilog can do files and console (at least on android)

    Log.Logger = new LoggerConfiguration() .WriteTo.RollingFile(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "JCA_log-{Date}.txt") ,outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}") .WriteTo.AndroidLog() .CreateLogger();

    Hi Philip, I am trying to figure out how to use Serilog with Xamarin and I am having some trouble so clarification would be greatly appreciated.

    First, I added Serilog through Nuget package and then Serilog.sinks.rollingfile and Seriolog.Sinks.Xamarin (to Android / IOS as these nuget packages would not install under PCL). I then added the line of code here to the MainActivity.cs in my .Droid. Afterwards I added Log.Information into the PCL to places I want to add log message.

    I find that the log file is not being created, and even the Sinks.Xamarin is not showing the messages in the console log.

    Would you mind please explaining what I have done wrong?

    Thanks in advance

  • PhilipOGormanPhilipOGorman USMember ✭✭✭
    edited July 2017

    @Curup1raB77 try this:
    Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.Async(a => a.RollingFile(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "AutoGrader_log-{Date}.txt") , outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}" , restrictedToMinimumLevel: LogEventLevel.Verbose)) .WriteTo.AndroidLog(restrictedToMinimumLevel: LogEventLevel.Verbose) // .WriteTo.Trace() .CreateLogger();

    See if you see your messages in the debug output.

    Here are the package to use:

    <package id="Serilog" version="2.3.0" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.Async" version="1.0.1" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.File" version="3.1.0" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.RollingFile" version="3.2.0-dev-00753" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.Trace" version="2.1.0" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.Xamarin" version="0.1.18" targetFramework="monoandroid60" />

    You'll have to initialize the logger outside of the PCL code

  • Curup1raB77Curup1raB77 USMember

    @PhilipOGorman said:
    @Curup1raB77 try this:
    Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.Async(a => a.RollingFile(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "AutoGrader_log-{Date}.txt") , outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}" , restrictedToMinimumLevel: LogEventLevel.Verbose)) .WriteTo.AndroidLog(restrictedToMinimumLevel: LogEventLevel.Verbose) // .WriteTo.Trace() .CreateLogger();

    See if you see your messages in the debug output.

    Here are the package to use:

    <package id="Serilog" version="2.3.0" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.Async" version="1.0.1" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.File" version="3.1.0" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.RollingFile" version="3.2.0-dev-00753" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.Trace" version="2.1.0" targetFramework="monoandroid60" /> <package id="Serilog.Sinks.Xamarin" version="0.1.18" targetFramework="monoandroid60" />

    You'll have to initialize the logger outside of the PCL code

    Thanks a bunch Philip. It worked out great and the issue was that I was testing on an emulator. On the actual device it created the file with no problems.

    Thanks again.

  • mirzashahbazmirzashahbaz INMember
    edited August 2017

    @Curup1raB77 can you please tell me how u achieved the above result. A detailed step by step explanation is highly appreciated , i m newbie in xamarin. Thanks . If u have a sample code example pls share.

Sign In or Register to comment.