dotnet-core_mail-server/MailServer/Logging/Logging.cs

75 lines
1.7 KiB
C#
Raw Normal View History

2016-11-23 21:32:40 +00:00
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
2016-11-23 21:32:40 +00:00
2016-11-26 11:11:27 +00:00
//namespace Logging
//{
public class Logging
{
private static LoggingType[] ActiveLoggingTypes { get; set; }
public string LogFolder { get; private set; }
public enum LoggingType
2016-11-23 21:32:40 +00:00
{
TRACE,
ERROR,
DEBUG,
WARNING,
INFO,
ALL
}
2016-11-23 21:32:40 +00:00
public Logging()
{
System.IO.Directory.GetCurrentDirectory();
2016-11-23 21:32:40 +00:00
ActiveLoggingTypes = new LoggingType[] { LoggingType.ALL };
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
LogFolder = "/var/log/mail/mail.log";
2016-11-23 21:32:40 +00:00
}
}
public static void AddLogMessage(LoggingType messagetype, string message)
{
new TaskFactory(TaskScheduler.Current);
new Task(() =>
2016-11-26 11:11:27 +00:00
{
if (CheckLoggingType(messagetype)) PrintToConsole(messagetype, message);
}).Start();
}
public static void AddException(Exception e)
{
2016-11-26 11:11:27 +00:00
AddLogMessage(LoggingType.ERROR, e.Message);
}
2016-11-26 11:11:27 +00:00
private static bool CheckLoggingType(LoggingType lt)
{
if (ActiveLoggingTypes == null || ActiveLoggingTypes.Length < 1)
{
ActiveLoggingTypes = new LoggingType[1];
ActiveLoggingTypes[0] = LoggingType.ALL;
}
bool found = false;
foreach (LoggingType l in ActiveLoggingTypes)
{
if (l == lt || l == LoggingType.ALL)
2016-11-23 21:32:40 +00:00
{
found = true;
break;
2016-11-23 21:32:40 +00:00
}
}
2016-11-23 21:32:40 +00:00
return found;
}
2016-11-23 21:32:40 +00:00
private static void PrintToConsole(LoggingType lt, string message)
{
Console.WriteLine("[" + lt.ToString() + "]" + message);
2016-11-23 21:32:40 +00:00
}
}