我终于在引导程序中找到了我的答案:
using Abp;
using Abp.Castle.Logging.Log4Net;
using Abp.Dependency;
using Castle.Facilities.Logging;
using System;
namespace MYNAMESPACE
{
class Program
{
static void Main(string[] args)
{
//Bootstrapping ABP system
using (var bootstrapper = AbpBootstrapper.Create<MYMODULE>())
{
IocManager.Instance.IocContainer.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net().WithConfig("log.config"));
bootstrapper.Initialize();
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
Console.ReadLine();
}
}
}
在 MyModule 里面我有:
public class MYMODULE: AbpModule
{
Timer timer;
private static readonly ILog log = LogManager.GetLogger(typeof(MYMODULE));
public override void PreInitialize()
{
log.Error("MyMessage");
}
}
在我的 log.config 中,我可以配置我的日志记录设置(例如,我可以为 log4net、graylog 或两者设置配置):
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="C:\Temp\" />
<datePattern value="yyyy-MM-dd.'txt'"/>
<staticLogFileName value="false"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="15MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level App %newline %message %newline %newline"/>
</layout>
</appender>
<appender name="GelfHttpAppender" type="Gelf4Net.Appender.GelfHttpAppender, Gelf4Net.HttpAppender">
<url value="http://MYURL/gelf" />
<layout type="Gelf4Net.Layout.GelfLayout, Gelf4Net.Core">
<param name="AdditionalFields" value="app:MYAPP,version:1.0,Environment:Dev,Level:%level" />
<param name="Facility" value="RandomPhrases" />
<param name="IncludeLocationInformation" value="true" />
</layout>
</appender>
<root>
<level value="All"/>
<appender-ref ref="GelfHttpAppender"/>
</root>
</log4net>