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

75 lines
1.7 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
//namespace Logging
//{
public class Logging
{
private static LoggingType[] ActiveLoggingTypes { get; set; }
public string LogFolder { get; private set; }
public enum LoggingType
{
TRACE,
ERROR,
DEBUG,
WARNING,
INFO,
ALL
}
public Logging()
{
System.IO.Directory.GetCurrentDirectory();
ActiveLoggingTypes = new LoggingType[] { LoggingType.ALL };
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
LogFolder = "/var/log/mail/mail.log";
}
}
public static void AddLogMessage(LoggingType messagetype, string message)
{
new TaskFactory(TaskScheduler.Current);
new Task(() =>
{
if (CheckLoggingType(messagetype)) PrintToConsole(messagetype, message);
}).Start();
}
public static void AddException(Exception e)
{
AddLogMessage(LoggingType.ERROR, e.Message);
}
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)
{
found = true;
break;
}
}
return found;
}
private static void PrintToConsole(LoggingType lt, string message)
{
Console.WriteLine("[" + lt.ToString() + "]" + message);
}
}