mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-20 06:13:09 +00:00
Updated LogConfigurator to avoid creation of many log files during several clients in one app session
This commit is contained in:
@@ -52,24 +52,45 @@ namespace MTApiService
|
|||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum LogLevel
|
||||||
|
{
|
||||||
|
Off,
|
||||||
|
Debug,
|
||||||
|
Info
|
||||||
|
}
|
||||||
|
|
||||||
public class LogConfigurator
|
public class LogConfigurator
|
||||||
{
|
{
|
||||||
private const string LogFileNameExtension = "log";
|
private const string LogFileNameExtension = "log";
|
||||||
|
|
||||||
public static void Setup(string profileName)
|
public static void Setup(string profileName)
|
||||||
{
|
{
|
||||||
|
#if (DEBUG)
|
||||||
|
const LogLevel logLevel = LogLevel.Debug;
|
||||||
|
#else
|
||||||
|
const LogLevel logLevel = LogLevel.Info;
|
||||||
|
#endif
|
||||||
|
Setup(profileName, logLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Setup(string profileName, LogLevel logLevel)
|
||||||
|
{
|
||||||
if (string.IsNullOrEmpty(profileName))
|
if (string.IsNullOrEmpty(profileName))
|
||||||
throw new ArgumentNullException();
|
throw new ArgumentNullException();
|
||||||
|
|
||||||
var hierarchy = (Hierarchy) LogManager.GetRepository();
|
var hierarchy = (Hierarchy) LogManager.GetRepository();
|
||||||
|
|
||||||
|
//check if logger is already configurated to avoid creation many empty logs files
|
||||||
|
if (hierarchy.Configured)
|
||||||
|
return;
|
||||||
|
|
||||||
var patternLayout = new PatternLayout
|
var patternLayout = new PatternLayout
|
||||||
{
|
{
|
||||||
ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"
|
ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"
|
||||||
};
|
};
|
||||||
patternLayout.ActivateOptions();
|
patternLayout.ActivateOptions();
|
||||||
|
|
||||||
string filename = $"{DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss")}-{Process.GetCurrentProcess().Id}.{LogFileNameExtension}";
|
var filename = $"{DateTime.Now:yyyy-dd-M--HH-mm-ss}-{Process.GetCurrentProcess().Id}.{LogFileNameExtension}";
|
||||||
|
|
||||||
var roller = new RollingFileAppender
|
var roller = new RollingFileAppender
|
||||||
{
|
{
|
||||||
@@ -84,12 +105,7 @@ namespace MTApiService
|
|||||||
};
|
};
|
||||||
roller.ActivateOptions();
|
roller.ActivateOptions();
|
||||||
hierarchy.Root.AddAppender(roller);
|
hierarchy.Root.AddAppender(roller);
|
||||||
|
hierarchy.Root.Level = ConvertLogLevel(logLevel);
|
||||||
#if (DEBUG)
|
|
||||||
hierarchy.Root.Level = Level.Debug;
|
|
||||||
#else
|
|
||||||
hierarchy.Root.Level = Level.Info;
|
|
||||||
#endif
|
|
||||||
hierarchy.Configured = true;
|
hierarchy.Configured = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,5 +116,17 @@ namespace MTApiService
|
|||||||
|
|
||||||
return new MtLog(type);
|
return new MtLog(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Level ConvertLogLevel(LogLevel logLevel)
|
||||||
|
{
|
||||||
|
switch (logLevel)
|
||||||
|
{
|
||||||
|
case LogLevel.Debug: return Level.Debug;
|
||||||
|
case LogLevel.Info: return Level.Info;
|
||||||
|
case LogLevel.Off: return Level.Off;
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user