我正在试图弄清楚如何在MVC中记录平坦的txt文件。
我不是要求最好的解决方案。我正在向正确的方向寻求一点。
我已经知道有一个单例类处理我的所有日志记录。
我基本上只想调用一个名为
的方法alogger = Logger.GetLogger();
aLogger.Log("some message",enum.serverity)
每当我需要将某些内容记录到文件中时。然后让singelton处理如何写入文件等等。
但后来我开始查看microsoft.extensions.logging库,希望我可以将大部分日志记录外包给NuGet中的某些标准。
然而,我觉得这个库的复杂性让我感到不知所措,并且正在考虑做我自己的基于小线程的日志记录。因为它似乎是我简单需求的简单解决方案。 有没有简单的指南如何存档我想要的但仍然使用看起来不像我们正在构建记录器火箭的标准库? 有没有更好的更简单的方法,我错过了?答案 0 :(得分:1)
您的所有问题都有一个简单的答案 - 使用Log4Net库。
这是我在其中一个项目中完成的最简单的实现:
<强> ILogger.cs 强>
public interface ILogger
{
void Info(string message);
void Warn(string message);
void Debug(string message);
void Error(string message);
void Error(Exception exception);
void Fatal(string message);
}
<强> Logger.cs 强>
public class Logger : ILogger
{
#region Properties
private readonly ILog log; //ILog refers to interface from log4net library
//Singleton Instance of Logger Class
private static readonly object threadLock = new Object();
private static Lazy<Logger> instance;
public static Logger Instance
{
get
{
if (instance == null)
{
lock (threadLock)
{
instance = new Lazy<Logger>(() => new Logger());
return instance.Value;
}
}
else
{
return instance.Value;
}
}
}
#endregion
#region Constructor
//Private constructor to restrict from any instance creation
private Logger()
{
XmlConfigurator.Configure();
log = LogManager.GetLogger(GetType());
//log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
#endregion
#region Methods
public void Info(string message)
{
log.Info(message);
}
public void Warn(string message)
{
log.Warn(message);
}
public void Debug(string message)
{
log.Debug(message);
}
public void Error(string message)
{
log.Error(message);
}
public void Error(Exception exception)
{
log.Error(exception);
}
public void Fatal(string message)
{
log.Fatal(message);
}
#endregion
}
<强> App.Config中强>
<configSections>
<!--- Specifying the configuration information about the XML tag "log4net" -->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<!---log4Net configuration for enabling logs to be stored in different modes -->
<log4net debug="true">
<appender name="consoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger -%message%newline" />
</layout>
</appender>
<appender name="filelogAppender" type="log4net.Appender.RollingFileAppender">
</appender>
<appender name="rollingFilelogAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\SpecifyLocation\SpecifyFileName.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="'Test_APILogs-'ddMMyyyy'.log'" />
<!--<staticLogFileName value="false" />
<maxSizeRollBackups value="50" />
<maximumFileSize value="1MB" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger -%message%newline" />
</layout>
</appender>
<appender name="dblogAppender" type="log4net.Appender.AdoNetAppender">
</appender>
<!---Root element where we specify the appenders (providers) that stores the logs.
The various logging levels provided are:
OFF - nothing gets logged (cannot be called)
FATAL
ERROR
WARN
INFO
DEBUG
ALL - everything gets logged (cannot be called)
-->
<root>
<level value="ALL" />
<!--<appender-ref ref="consoleAppender" />-->
<!--<appender-ref ref="filelogAppender" />-->
<appender-ref ref="rollingFilelogAppender" />
<!--<appender-ref ref="dblogAppender" />-->
</root>
</log4net>
<强>控制器强>
[HttpPost, Route("add")]
public IHttpActionResult AddEmployee(AddEmployeeRequestDTO request)
{
try
{
if (request == null)
return Content(HttpStatusCode.BadRequest, ErrorCodes.E002);
}
catch (Exception ex)
{
#if (DEBUG)
Logger.Instance.Error(ex);
throw ex;
#endif
//Logger.Instance.Error(ex);
//return Content(HttpStatusCode.InternalServerError, ErrorCodes.E001);
}
}