Messenger/MSGer.tk/Logging.cs

50 lines
1.5 KiB
C#
Raw Normal View History

2015-07-06 14:29:34 +00:00
using System; //Copyright (c) NorbiPeti 2015 - See LICENSE file
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MSGer.tk
{
public class Logging
{
public enum LogType
{
Network
}
public static void Log(string message, LogType logtype) //2014.12.31.
{
if (!Directory.Exists("logs"))
Directory.CreateDirectory("logs");
string path;
switch (logtype)
{
case LogType.Network:
path = "network";
break;
default:
throw new NotImplementedException("Log type not implemented.");
}
string finaltext = "[" + Process.GetCurrentProcess().Id + ": " + Thread.CurrentThread.Name + " | " + DateTime.Now.ToString("yyyy.MM.dd. HH:mm:ss") + "] " + message + Environment.NewLine;
Console.WriteLine(logtype.ToString() + " - " + finaltext);
2015-07-06 11:04:54 +00:00
while (true)
{
bool retry = false;
try
{
File.AppendAllText("logs\\" + path + ".txt", finaltext);
}
2015-07-06 11:04:54 +00:00
catch (IOException)
{
retry = true;
}
if (!retry)
break;
}
}
}
}