77 lines
1.9 KiB
C#
77 lines
1.9 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
|
|
namespace Tallier.MapDriveScriptUpdate
|
|
{
|
|
public enum LogLevel
|
|
{
|
|
Info = 0,
|
|
Warn = 1,
|
|
Error = 2
|
|
}
|
|
|
|
public class Logger
|
|
{
|
|
public struct LogArg
|
|
{
|
|
public string Msg { get; set; }
|
|
}
|
|
|
|
private const string logFile = "errors_log.txt";
|
|
private BackgroundWorker worker;
|
|
|
|
public Logger(BackgroundWorker worker)
|
|
{
|
|
this.worker = worker;
|
|
}
|
|
|
|
public void Error(Exception x)
|
|
{
|
|
string msg = string.Format("{0}\n{1}", x.Message, x.StackTrace);
|
|
this.Error(msg);
|
|
}
|
|
|
|
public void Message(LogLevel level, string msg)
|
|
{
|
|
msg = string.Format("{0}: {1} {2}{3}", DateTime.Now, level, msg, Environment.NewLine);
|
|
this.worker.ReportProgress(0, new LogArg { Msg = msg });
|
|
if (level == LogLevel.Warn || level == LogLevel.Error)
|
|
{
|
|
this.logToFile(level, msg);
|
|
}
|
|
}
|
|
|
|
private void logToFile(LogLevel level, string msg)
|
|
{
|
|
try
|
|
{
|
|
File.AppendAllText(logFile, string.Format("{0}: {1} {2}{3}", DateTime.Now, level, msg, Environment.NewLine));
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
// public void Error(string msg)
|
|
// {
|
|
// this.Message(LogLevel.Error, msg);
|
|
// }
|
|
|
|
public void Error(string msg, params object[] args)
|
|
{
|
|
this.Message(LogLevel.Error, string.Format(msg, args));
|
|
}
|
|
|
|
public void Info(string msg, params object[] args)
|
|
{
|
|
this.Message(LogLevel.Info, string.Format(msg, args));
|
|
}
|
|
|
|
public void Warn(string msg, params object[] args)
|
|
{
|
|
this.Message(LogLevel.Warn, string.Format(msg, args));
|
|
}
|
|
}
|
|
}
|