当前位置: 移动技术网 > IT编程>开发语言>.net > 使用log4net记录ABP日志

使用log4net记录ABP日志

2019年07月29日  | 移动技术网IT编程  | 我要评论

我的祖国诗朗诵,曲阜人才网,金三角坤沙玩的女人

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。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网