Files
mtapi/MTApiService/LogConfigurator.cs
T
Joe d27addc694 Update LogConfigurator.cs
The part of filename "{DateTime.Now:yyyy-dd-M--HH-mm-ss}-{Process.GetCurrentProcess().Id}" generates tons of log files with same content but different per-second/thread filenames until the harddisk have 0KB space left. This kept happening in my AWS EC2 machine until I found the big GBs-size log file in temp folder caused by this line.

Changing the filename to $"{DateTime.Now:yyyy-MM-dd}.{LogFileNameExtension}" solved the issue. The timestamp and processId can still be found in the log content.
2021-07-11 17:07:23 +08:00

134 lines
3.7 KiB
C#
Executable File

using System;
using System.Diagnostics;
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
namespace MTApiService
{
public class MtLog
{
#region ctor
internal MtLog(Type type)
{
_log = LogManager.GetLogger(type);
}
#endregion
#region Public
public void Debug(object message)
{
_log.Debug(message);
}
public void Error(object message)
{
_log.Error(message);
}
public void Fatal(object message)
{
_log.Fatal(message);
}
public void Info(object message)
{
_log.Info(message);
}
public void Warn(object message)
{
_log.Warn(message);
}
#endregion
#region Private
private readonly ILog _log;
#endregion
}
public enum LogLevel
{
Off,
Debug,
Info
}
public class LogConfigurator
{
private const string LogFileNameExtension = "log";
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))
throw new ArgumentNullException();
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
{
ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"
};
patternLayout.ActivateOptions();
//var filename = $"{DateTime.Now:yyyy-dd-M--HH-mm-ss}-{Process.GetCurrentProcess().Id}.{LogFileNameExtension}"; // This filename format keeps generating tons of log files with same content but different per-second/thread filenames until the harddisk have 0KB space left
var filename = $"{DateTime.Now:yyyy-MM-dd}.{LogFileNameExtension}";
var roller = new RollingFileAppender
{
AppendToFile = true,
File = $@"{System.IO.Path.GetTempPath()}{profileName}\Logs\{filename}",
Layout = patternLayout,
PreserveLogFileNameExtension = true,
MaxSizeRollBackups = 5,
MaximumFileSize = "1GB",
RollingStyle = RollingFileAppender.RollingMode.Size,
StaticLogFileName = false
};
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);
hierarchy.Root.Level = ConvertLogLevel(logLevel);
hierarchy.Configured = true;
}
public static MtLog GetLogger(Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(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);
}
}
}
}