demo地址:ABP.WindowsService

该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇。

参考:https://aspnetboilerplate.com/Pages/Documents/Logging

ABP框架使用的是Castle Windsor的日志工具,Castle Windsor定义了日志接口

ILogger,并提供了Log4Net, NLog, Serilog的实现。

首先,nuget添加Abp.Castle.Log4Net,github地址:Abp.Castle.Log4Net

然后,添加AppConfigurations,根据环境变量获取log4net.config的绝对路径,模仿appsettings.json的使用方式。代码如下:

using Abp.Extensions;
using System.Collections.Concurrent;
using System.IO; namespace Demo.MyJob.Configuration
{
public static class AppLog4NetConfigs
{
private static readonly ConcurrentDictionary<string, string> Log4NetConfigCache; static AppLog4NetConfigs()
{
Log4NetConfigCache = new ConcurrentDictionary<string, string>();
} public static void AddProperty(string key, string value)
{
log4net.GlobalContext.Properties[key] = value;
} public static string Get(string path, string environmentName = null)
{
var cacheKey = path + "#" + environmentName; return Log4NetConfigCache.GetOrAdd(
cacheKey,
_ => BuildLog4NetConfig(path, environmentName)
);
} private static string BuildLog4NetConfig(string path, string environmentName = null)
{
var configFile = Path.Combine(path, "log4net.config"); if (environmentName.IsNullOrWhiteSpace())
{
return configFile;
} var tempPath = Path.Combine(path, $"log4net.{environmentName}.config");
if (File.Exists(tempPath))
{
configFile = tempPath;
} return configFile;
}
}
}

最后,先在ConfigureAppConfiguration获取绝对路径,

AppLog4NetConfigs.AddProperty("LogsDirectory", hostingEnvironment.ContentRootPath);
LogConfigFile = AppLog4NetConfigs.Get(hostingEnvironment.ContentRootPath, hostingEnvironment.EnvironmentName);

接着添加日志配置

IocManager.IocContainer.AddFacility<LoggingFacility>(
f => f.UseAbpLog4Net().WithConfig(LogConfigFile)
);

测试一下

在Execute添加日记记录

LogHelper.Logger.Info(nameof(SayHelloJob));

测试ok。

log4net.config参考

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="ManagedColoredConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red" />
</mapping>
<mapping>
<level value="Info" />
<foreColor value="Green" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Blue" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{ABSOLUTE} [%thread] %-5p %c{1}:%L - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="Fatal" />
</filter>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="%property{LogsDirectory}\logs\" />
<datePattern value="'my-windows-service-'dd.MM.yyyy'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ManagedColoredConsoleAppender" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
</configuration>
05-13 15:31