mirror of
https://github.com/LorenzCK/OnTopReplica.git
synced 2024-05-20 20:33:06 +12:00
78 lines
2.3 KiB
C#
78 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace OnTopReplica {
|
|
static class Log {
|
|
|
|
const string LogFileName = "lastrun.log.txt";
|
|
|
|
private readonly static StreamWriter Writer;
|
|
|
|
static Log() {
|
|
try {
|
|
var filepath = Path.Combine(AppPaths.PrivateRoamingFolderPath, LogFileName);
|
|
Writer = new StreamWriter(new FileStream(filepath, FileMode.Create));
|
|
Writer.AutoFlush = true;
|
|
}
|
|
catch (Exception) {
|
|
//TODO: provide fallback logging mechanism?
|
|
Writer = null;
|
|
}
|
|
}
|
|
|
|
public static void Write(string message) {
|
|
WriteLine(message);
|
|
}
|
|
|
|
public static void Write(string format, object arg0) {
|
|
WriteLine(string.Format(format, arg0));
|
|
}
|
|
|
|
public static void Write(string format, object arg0, object arg1) {
|
|
WriteLine(string.Format(format, arg0, arg1));
|
|
}
|
|
|
|
public static void Write(string format, params object[] args) {
|
|
WriteLine(string.Format(format, args));
|
|
}
|
|
|
|
public static void WriteDetails(string caption, string format, params object[] args) {
|
|
WriteLines(caption, string.Format(format, args));
|
|
}
|
|
|
|
public static void WriteException(string message, Exception exception) {
|
|
if (exception != null) {
|
|
WriteLines(message, exception.ToString());
|
|
}
|
|
else {
|
|
WriteLines(message, "(no last exception)");
|
|
}
|
|
}
|
|
|
|
private static void WriteLine(string message) {
|
|
if (Writer == null)
|
|
return;
|
|
|
|
var s = string.Format("{0,-8:HH:mm:ss} {1}", DateTime.Now, message);
|
|
Writer.WriteLine(s);
|
|
}
|
|
|
|
private static void WriteLines(params string[] messages) {
|
|
if (Writer == null)
|
|
return;
|
|
|
|
if (messages.Length > 0)
|
|
WriteLine(messages[0]);
|
|
if (messages.Length > 1) {
|
|
for (int i = 1; i < messages.Length; ++i) {
|
|
Writer.WriteLine(" {0}", messages[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|