Last active
December 15, 2015 04:50
-
-
Save nicwise/5204694 to your computer and use it in GitHub Desktop.
My logger. Requires ServiceStack.Text for the objectdump. DebugMode can be set on in the app startup, I usually do it on a Settings.app setting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using ServiceStack.Text; | |
namespace BigTed.Utils | |
{ | |
public class BTLogger | |
{ | |
#if DEBUG | |
public static bool DebugMode = true; | |
#else | |
public static bool DebugMode = false; | |
#endif | |
public static void Dump<T> (T o) | |
{ | |
if (o == null) { | |
Log ("LogObject was NULL"); | |
return; | |
} | |
try { | |
string json = JsonSerializer.SerializeToString<T> (o); | |
//string.Format gets funny with all the {} | |
Log (json.Replace ("{", "{{").Replace ("}", "}}")); | |
} catch (Exception ex) { | |
Log ("error logging object"); | |
LogException (ex); | |
} | |
} | |
static object loggingGate = new object (); | |
public static void Log (string msg, params object[] param) | |
{ | |
string res = msg; | |
if (param.Length > 0) { | |
res = string.Format (msg, param); | |
} | |
if (DebugMode) { | |
Console.WriteLine (res); | |
lock (loggingGate) { | |
using (StreamWriter sw = File.AppendText(LogFilename)) { | |
sw.WriteLine (DateTime.Now.ToString ("yyyyMMdd/HHmmss") + ": " + res); | |
sw.Flush (); | |
sw.Close (); | |
} | |
} | |
} | |
} | |
public static string LogContent { | |
get { | |
lock (loggingGate) { | |
if (!File.Exists (LogFilename)) | |
return "No log file present"; | |
return File.ReadAllText (LogFilename); | |
} | |
} | |
} | |
public static void LogException (Exception ex) | |
{ | |
Log ("Exception: " + ex.ToString ()); | |
Log ("StackTrace: " + ex.StackTrace.ToString ()); | |
if (ex.InnerException != null) { | |
LogException (ex.InnerException); | |
} | |
} | |
public static void LogCurrentStack () | |
{ | |
StackTrace st = new StackTrace (); | |
Log ("Stack trace: " + st.ToString ()); | |
} | |
public static void CheckLogsForCleanup () | |
{ | |
if (File.Exists (LogFilename)) { | |
FileInfo fi = new FileInfo (LogFilename); | |
//BTLogger.Log ("Log file is {0:0} kb", fi.Length / 1024); | |
if (fi.Length > (70 * 1024)) | |
{ | |
DeleteLogFile (); | |
Log ("Log file purged - more than 70k"); | |
} | |
} | |
} | |
public static void DeleteLogFile () | |
{ | |
lock (loggingGate) { | |
if (File.Exists (LogFilename)) { | |
File.Delete (LogFilename); | |
} | |
} | |
} | |
public static string LogFilename { | |
get { | |
return Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments), "bigted.log"); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment