demo地址: "ABP.WindowsService" 該文章是系列文章 "基於.NetCore和ABP框架如何讓Windows服務執行Quartz定時作業" 的其中一篇。 參考:https://aspnetboilerplate.com/Pages/Documents/Logging ABP框架 ...
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>