diff --git a/PearAdmin.AbpTemplate.sln b/PearAdmin.AbpTemplate.sln index 3b190dfccdc32efde1d1e02502e90e4768ded84d..d12370d625e22be0c7051270b612c5288ac5758d 100644 --- a/PearAdmin.AbpTemplate.sln +++ b/PearAdmin.AbpTemplate.sln @@ -37,6 +37,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "module", "module", "{A9CEE7 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PearAdmin.AbpTemplate.Storage.Local", "module\PearAdmin.AbpTemplate.Storage.Local\PearAdmin.AbpTemplate.Storage.Local.csproj", "{91E47E05-561D-42C2-AFB8-2F567185E177}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PearAdmin.AbpTemplate.NLog", "module\PearAdmin.AbpTemplate.NLog\PearAdmin.AbpTemplate.NLog.csproj", "{9AFB01E8-783F-472E-90D6-F2A8AD6120FF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -95,6 +97,10 @@ Global {91E47E05-561D-42C2-AFB8-2F567185E177}.Debug|Any CPU.Build.0 = Debug|Any CPU {91E47E05-561D-42C2-AFB8-2F567185E177}.Release|Any CPU.ActiveCfg = Release|Any CPU {91E47E05-561D-42C2-AFB8-2F567185E177}.Release|Any CPU.Build.0 = Release|Any CPU + {9AFB01E8-783F-472E-90D6-F2A8AD6120FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AFB01E8-783F-472E-90D6-F2A8AD6120FF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AFB01E8-783F-472E-90D6-F2A8AD6120FF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AFB01E8-783F-472E-90D6-F2A8AD6120FF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -113,6 +119,7 @@ Global {E5B76444-3961-42D9-996C-A10921B2EA04} = {A9CEE774-07BE-415D-935C-D42562C045F9} {F17AF2E0-E290-43A6-8695-B653FA6446EE} = {AFAA0841-BD93-466F-B8F4-FB4EEC86F1FC} {91E47E05-561D-42C2-AFB8-2F567185E177} = {A9CEE774-07BE-415D-935C-D42562C045F9} + {9AFB01E8-783F-472E-90D6-F2A8AD6120FF} = {A9CEE774-07BE-415D-935C-D42562C045F9} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C6B33810-FA59-48A3-9DC4-F1F0312C57F6} diff --git a/module/PearAdmin.AbpTemplate.NLog/AbpTemplateCastleNLogModule.cs b/module/PearAdmin.AbpTemplate.NLog/AbpTemplateCastleNLogModule.cs new file mode 100644 index 0000000000000000000000000000000000000000..2a94122dedd269b644b547d1f38f03fff9ef66d3 --- /dev/null +++ b/module/PearAdmin.AbpTemplate.NLog/AbpTemplateCastleNLogModule.cs @@ -0,0 +1,11 @@ +using Abp; +using Abp.Modules; + +namespace PearAdmin.AbpTemplate.NLog +{ + [DependsOn(typeof(AbpKernelModule))] + public class AbpTemplateCastleNLogModule : AbpModule + { + + } +} diff --git a/module/PearAdmin.AbpTemplate.NLog/LoggingFacilityExtensions.cs b/module/PearAdmin.AbpTemplate.NLog/LoggingFacilityExtensions.cs new file mode 100644 index 0000000000000000000000000000000000000000..9a5ee4f54566b2bfc449da05ae958889347d5a7a --- /dev/null +++ b/module/PearAdmin.AbpTemplate.NLog/LoggingFacilityExtensions.cs @@ -0,0 +1,13 @@ +using Castle.Facilities.Logging; +using Microsoft.Extensions.Configuration; + +namespace PearAdmin.AbpTemplate.NLog +{ + public static class LoggingFacilityExtensions + { + public static LoggingFacility UseAbpNLog(this LoggingFacility loggingFacility, IConfigurationSection section) + { + return loggingFacility.LogUsing(new NLogLoggerFactory(section)); + } + } +} \ No newline at end of file diff --git a/module/PearAdmin.AbpTemplate.NLog/NLogLogger.cs b/module/PearAdmin.AbpTemplate.NLog/NLogLogger.cs new file mode 100644 index 0000000000000000000000000000000000000000..34983099b02d5f478cd65c981d640feb73f0fdf1 --- /dev/null +++ b/module/PearAdmin.AbpTemplate.NLog/NLogLogger.cs @@ -0,0 +1,249 @@ +using System; +using System.Globalization; +using ILogger = Castle.Core.Logging.ILogger; +using NLogCore = NLog; + +namespace PearAdmin.AbpTemplate.NLog +{ + [Serializable] + public class NLogLogger : MarshalByRefObject, ILogger + { + protected internal NLogCore.ILogger Logger { get; set; } + + public NLogLogger(NLogCore.ILogger logger) + { + Logger = logger; + } + + internal NLogLogger() + { + } + + public bool IsDebugEnabled => Logger.IsEnabled(NLogCore.LogLevel.Debug); + + public bool IsErrorEnabled => Logger.IsEnabled(NLogCore.LogLevel.Error); + + public bool IsFatalEnabled => Logger.IsEnabled(NLogCore.LogLevel.Fatal); + + public bool IsInfoEnabled => Logger.IsEnabled(NLogCore.LogLevel.Info); + + public bool IsWarnEnabled => Logger.IsEnabled(NLogCore.LogLevel.Warn); + + public bool IsTraceEnabled => throw new NotImplementedException(); + + public ILogger CreateChildLogger(string loggerName) + { + return new NLogLogger(NLogCore.LogManager.GetLogger(Logger.Name + "." + loggerName)); + } + + public void Debug(string message) + { + Logger.Debug(message); + } + + public void Debug(Func messageFactory) + { + Logger.Debug(messageFactory); + } + + public void Debug(string message, Exception exception) + { + Logger.Debug(exception, message); + } + + public void DebugFormat(string format, params object[] args) + { + Logger.Debug(CultureInfo.InvariantCulture, format, args); + } + + public void DebugFormat(Exception exception, string format, params object[] args) + { + Logger.Debug(exception, CultureInfo.InvariantCulture, format, args); + } + + public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Debug(formatProvider, format, args); + } + + public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Debug(exception, formatProvider, format, args); + } + + public void Error(string message) + { + Logger.Error(message); + } + + public void Error(Func messageFactory) + { + Logger.Error(messageFactory); + } + + public void Error(string message, Exception exception) + { + Logger.Error(exception, message); + } + + public void ErrorFormat(string format, params object[] args) + { + Logger.Error(CultureInfo.InvariantCulture, format, args); + } + + public void ErrorFormat(Exception exception, string format, params object[] args) + { + Logger.Error(exception, CultureInfo.InvariantCulture, format, args); + } + + public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Error(formatProvider, format, args); + } + + public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Error(exception, formatProvider, format, args); + } + + public void Fatal(string message) + { + Logger.Fatal(message); + } + + public void Fatal(Func messageFactory) + { + Logger.Fatal(messageFactory); + } + + public void Fatal(string message, Exception exception) + { + Logger.Fatal(exception, message); + } + + public void FatalFormat(string format, params object[] args) + { + Logger.Fatal(CultureInfo.InvariantCulture, format, args); + } + + public void FatalFormat(Exception exception, string format, params object[] args) + { + Logger.Fatal(exception, CultureInfo.InvariantCulture, format, args); + } + + public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Fatal(formatProvider, format, args); + } + + public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Fatal(exception, formatProvider, format, args); + } + + public void Info(string message) + { + Logger.Info(message); + } + + public void Info(Func messageFactory) + { + Logger.Info(messageFactory); + } + + public void Info(string message, Exception exception) + { + Logger.Info(exception, message); + } + + public void InfoFormat(string format, params object[] args) + { + Logger.Info(CultureInfo.InvariantCulture, format, args); + } + + public void InfoFormat(Exception exception, string format, params object[] args) + { + Logger.Info(exception, CultureInfo.InvariantCulture, format, args); + } + + public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Info(formatProvider, format, args); + } + + public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Info(exception, formatProvider, format, args); + } + + public void Warn(string message) + { + Logger.Warn(message); + } + + public void Warn(Func messageFactory) + { + Logger.Warn(messageFactory); + } + + public void Warn(string message, Exception exception) + { + Logger.Warn(exception, message); + } + + public void WarnFormat(string format, params object[] args) + { + Logger.Warn(CultureInfo.InvariantCulture, format, args); + } + + public void WarnFormat(Exception exception, string format, params object[] args) + { + Logger.Warn(exception, CultureInfo.InvariantCulture, format, args); + } + + public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Warn(formatProvider, format, args); + } + + public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Warn(exception, formatProvider, format, args); + } + + public void Trace(string message) + { + Logger.Trace(message); + } + + public void Trace(Func messageFactory) + { + Logger.Trace(messageFactory); + } + + public void Trace(string message, Exception exception) + { + Logger.Trace(exception, message); + } + + public void TraceFormat(string format, params object[] args) + { + Logger.Trace(format, args); + } + + public void TraceFormat(Exception exception, string format, params object[] args) + { + Logger.Trace(exception, format, args); + } + + public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Trace(formatProvider, format, args); + } + + public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + Logger.Trace(exception, formatProvider, format, args); + } + } +} \ No newline at end of file diff --git a/module/PearAdmin.AbpTemplate.NLog/NLogLoggerFactory.cs b/module/PearAdmin.AbpTemplate.NLog/NLogLoggerFactory.cs new file mode 100644 index 0000000000000000000000000000000000000000..53a8048db8c7f3bb0bb22099355c64628ccf5862 --- /dev/null +++ b/module/PearAdmin.AbpTemplate.NLog/NLogLoggerFactory.cs @@ -0,0 +1,30 @@ +using System; +using Castle.Core.Logging; +using Microsoft.Extensions.Configuration; +using NLog.Extensions.Logging; +using NLogCore = NLog; + +namespace PearAdmin.AbpTemplate.NLog +{ + public class NLogLoggerFactory : AbstractLoggerFactory + { + public NLogLoggerFactory(IConfigurationSection section) + { + NLogCore.LogManager.Configuration = new NLogLoggingConfiguration(section); + } + + public override ILogger Create(string name) + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + return new NLogLogger(NLogCore.LogManager.GetLogger(name)); + } + + public override ILogger Create(string name, LoggerLevel level) + { + throw new NotSupportedException("Logger levels cannot be set at runtime. Please review your configuration file."); + } + } +} \ No newline at end of file diff --git a/module/PearAdmin.AbpTemplate.NLog/PearAdmin.AbpTemplate.NLog.csproj b/module/PearAdmin.AbpTemplate.NLog/PearAdmin.AbpTemplate.NLog.csproj new file mode 100644 index 0000000000000000000000000000000000000000..3b5e386eb2576a9caf7ecd8c12f0e741506b19d9 --- /dev/null +++ b/module/PearAdmin.AbpTemplate.NLog/PearAdmin.AbpTemplate.NLog.csproj @@ -0,0 +1,14 @@ + + + + 1.0.0.0 + net5.0 + + + + + + + + + diff --git a/src/PearAdmin.AbpTemplate.Admin/AbpTemplateAdminModule.cs b/src/PearAdmin.AbpTemplate.Admin/AbpTemplateAdminModule.cs index eb73c9f3242352285bcc37dc34c58bfbc8486d8c..d56b49e522792e4ceb04593d8efc5f5ec3f51160 100644 --- a/src/PearAdmin.AbpTemplate.Admin/AbpTemplateAdminModule.cs +++ b/src/PearAdmin.AbpTemplate.Admin/AbpTemplateAdminModule.cs @@ -40,7 +40,7 @@ namespace PearAdmin.AbpTemplate.Admin { Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(AbpTemplateCoreConsts.ConnectionStringName); - // Use database for language management + // 本地化内容存储到数据库 Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization(); // 显示所有错误信息到客户端 diff --git a/src/PearAdmin.AbpTemplate.Admin/Extensions/AbpBootstrapperOptionsExtension.cs b/src/PearAdmin.AbpTemplate.Admin/Extensions/AbpBootstrapperOptionsExtension.cs new file mode 100644 index 0000000000000000000000000000000000000000..2d3f198fa6653d9498549a6ef3100e5dedb6fee9 --- /dev/null +++ b/src/PearAdmin.AbpTemplate.Admin/Extensions/AbpBootstrapperOptionsExtension.cs @@ -0,0 +1,17 @@ +using System; +using Abp; +using Castle.Facilities.Logging; +using Microsoft.Extensions.Configuration; +using PearAdmin.AbpTemplate.NLog; + +namespace PearAdmin.AbpTemplate.Admin.Extensions +{ + public class AbpBootstrapperOptionsExtension + { + public static Action GetOptions(IConfiguration configuration) + { + return options => options.IocManager.IocContainer + .AddFacility(f => f.UseAbpNLog(configuration.GetSection("NLog"))); + } + } +} diff --git a/src/PearAdmin.AbpTemplate.Admin/PearAdmin.AbpTemplate.Admin.csproj b/src/PearAdmin.AbpTemplate.Admin/PearAdmin.AbpTemplate.Admin.csproj index 1bf6b337db4b5d0d8a3cf85931646b14c51cf8ca..dad5e608d939d3c8e04512b68fb37275211d9cc8 100644 --- a/src/PearAdmin.AbpTemplate.Admin/PearAdmin.AbpTemplate.Admin.csproj +++ b/src/PearAdmin.AbpTemplate.Admin/PearAdmin.AbpTemplate.Admin.csproj @@ -15,6 +15,7 @@ true + @@ -33,6 +34,5 @@ - \ No newline at end of file diff --git a/src/PearAdmin.AbpTemplate.Admin/Startup.cs b/src/PearAdmin.AbpTemplate.Admin/Startup.cs index ada6c819404894d66089cb2fb6367173f87894f6..e7d36bf5bd53f813c5467101ba96080727d60cff 100644 --- a/src/PearAdmin.AbpTemplate.Admin/Startup.cs +++ b/src/PearAdmin.AbpTemplate.Admin/Startup.cs @@ -2,11 +2,9 @@ using Abp.AspNetCore; using Abp.AspNetCore.Mvc.Antiforgery; using Abp.AspNetCore.SignalR.Hubs; -using Abp.Castle.Logging.Log4Net; using Abp.Dependency; using Abp.Hangfire; using Abp.Json; -using Castle.Facilities.Logging; using Hangfire; using Hangfire.MemoryStorage; using Microsoft.AspNetCore.Builder; @@ -19,6 +17,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Serialization; using PearAdmin.AbpTemplate.Admin.Configuration; +using PearAdmin.AbpTemplate.Admin.Extensions; using PearAdmin.AbpTemplate.Admin.SignalR; using PearAdmin.AbpTemplate.Authorization; using PearAdmin.AbpTemplate.Identity; @@ -27,22 +26,20 @@ namespace PearAdmin.AbpTemplate.Admin { public class Startup { - private readonly IConfigurationRoot _appConfiguration; + private readonly IConfigurationRoot Configuration; public Startup(IWebHostEnvironment env) { - _appConfiguration = env.GetAppConfiguration(); + Configuration = env.GetAppConfiguration(); } public IServiceProvider ConfigureServices(IServiceCollection services) { - services.AddControllersWithViews( - options => - { - options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); - options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute()); - } - ) + services.AddControllersWithViews(options => + { + options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); + options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute()); + }) .AddRazorRuntimeCompilation() .AddNewtonsoftJson(options => { @@ -71,13 +68,7 @@ namespace PearAdmin.AbpTemplate.Admin #endif }); - // Configure Abp and Dependency Injection - return services.AddAbp( - // Configure Log4Net logging - options => options.IocManager.IocContainer.AddFacility( - f => f.UseAbpLog4Net().WithConfig("log4net.config") - ) - ); + return services.AddAbp(AbpBootstrapperOptionsExtension.GetOptions(Configuration)); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) diff --git a/src/PearAdmin.AbpTemplate.Admin/appsettings.json b/src/PearAdmin.AbpTemplate.Admin/appsettings.json index 5dcd4c6d7839b93e0e8e0facb07e4048c6efafd5..2f415e52c6e82027169b286f8d216621d517a19b 100644 --- a/src/PearAdmin.AbpTemplate.Admin/appsettings.json +++ b/src/PearAdmin.AbpTemplate.Admin/appsettings.json @@ -20,5 +20,47 @@ "Endpoint": "", "AccessKey": "", "SecretKey": "" + }, + "NLog": { + "autoReload": true, + "throwConfigExceptions": true, + "internalLogLevel": "Info", + "internalLogFile": "App_Data\\Logs\\nlogs.txt", + "extensions": [ + { + "NLog.Web.AspNetCore": { + "assembly": "NLog.Web.AspNetCore" + } + } + ], + "targets": { + "allfile": { + "type": "File", + "fileName": "${basedir}\\App-Data\\Logs\\nlog-all-${shortdate}.log", + "layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" + }, + "ownFile-web": { + "type": "File", + "fileName": "${basedir}\\App-Data\\Logs\\nlog-my-${shortdate}.log", + "layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" + } + }, + "rules": [ + { + "logger": "*", + "minLevel": "Trace", + "writeTo": "allfile" + }, + { + "logger": "*", + "minLevel": "Trace", + "writeTo": "ownFile-web" + }, + { + "logger": "Microsoft.*", + "maxLevel": "Info", + "final": true + } + ] } } diff --git a/src/PearAdmin.AbpTemplate.Admin/log4net.config b/src/PearAdmin.AbpTemplate.Admin/log4net.config deleted file mode 100644 index b5b55e838def3bd9e719e5e22002cc35fcd08414..0000000000000000000000000000000000000000 --- a/src/PearAdmin.AbpTemplate.Admin/log4net.config +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - - -