|
@@ -0,0 +1,93 @@
|
|
|
+using System;
|
|
|
+using System.IO;
|
|
|
+using Serilog;
|
|
|
+using Serilog.Core;
|
|
|
+using Xamarin.Forms;
|
|
|
+
|
|
|
+namespace InABox.Mobile
|
|
|
+{
|
|
|
+ public enum LogType
|
|
|
+ {
|
|
|
+ Query,
|
|
|
+ Save,
|
|
|
+ Delete,
|
|
|
+ Validate,
|
|
|
+ UIUpdate,
|
|
|
+ BackgroundProcess
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class MobileLogging
|
|
|
+ {
|
|
|
+
|
|
|
+ private static Logger _logger = null;
|
|
|
+
|
|
|
+ private static string _logfilenameincludingpath;
|
|
|
+
|
|
|
+ private static void CheckLogger()
|
|
|
+ {
|
|
|
+ if (_logger == null)
|
|
|
+ {
|
|
|
+ var libraryPath = Device.RuntimePlatform.Equals(Device.iOS)
|
|
|
+ ? Environment.GetFolderPath(Environment.SpecialFolder.Resources)
|
|
|
+ : Environment.GetFolderPath(Environment.SpecialFolder.Personal);
|
|
|
+
|
|
|
+
|
|
|
+ var filename = $"{DateTime.Today:yyyy-MMM-dd}.prsmobile";
|
|
|
+ _logfilenameincludingpath = Path.Combine(libraryPath, filename);
|
|
|
+
|
|
|
+ var files = Directory.GetFiles(libraryPath, "*.prsmobile");
|
|
|
+ foreach (var file in files)
|
|
|
+ {
|
|
|
+ if (!String.Equals(filename.ToUpper(), Path.GetFileName(file).ToUpper()))
|
|
|
+ File.Delete(file);
|
|
|
+ }
|
|
|
+
|
|
|
+ _logger = new LoggerConfiguration()
|
|
|
+ .WriteTo.File(_logfilenameincludingpath)
|
|
|
+ .CreateLogger();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Log(LogType type, string entitytype, string message, string page)
|
|
|
+ {
|
|
|
+ CheckLogger();
|
|
|
+ System.Diagnostics.Debug.WriteLine($"[Log] {message}");
|
|
|
+ Console.WriteLine($"[Log] {message}");
|
|
|
+ _logger.Information("{Type} {Entity} {Message} {Page}", type, entitytype, message, page);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Log(string message)
|
|
|
+ {
|
|
|
+ CheckLogger();
|
|
|
+ System.Diagnostics.Debug.WriteLine($"[Log] {message}");
|
|
|
+ Console.WriteLine($"[Log] {message}");
|
|
|
+ _logger.Information("{Log}", message);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Log(Exception exception, String tag = "")
|
|
|
+ {
|
|
|
+ CheckLogger();
|
|
|
+ if (String.IsNullOrWhiteSpace(tag))
|
|
|
+ _logger.Error("{Message} {StackTrace}",exception.Message, exception.StackTrace);
|
|
|
+ else
|
|
|
+ _logger.Error("{Tag} {Message} {StackTrace}", tag, exception.Message, exception.StackTrace);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Clear()
|
|
|
+ {
|
|
|
+ CheckLogger();
|
|
|
+ File.WriteAllText(_logfilenameincludingpath,"");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String ReadLog()
|
|
|
+ {
|
|
|
+ CheckLogger();
|
|
|
+ return File.Exists(_logfilenameincludingpath)
|
|
|
+ ? File.ReadAllText(_logfilenameincludingpath)
|
|
|
+ : "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|