欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.Net Core 实践 - 使用log4net记录日志(2)

程序员文章站 2022-03-30 10:12:40
实现目标:将log4net的相关操作封装成一个 .Net Standard类库 demo地址:https://github.com/PuzzledAlien/log4net_demo/tree/master/DotNetCoreConsole_V2 Step 1 建立解决方案和处理项目相关配置 在当 ......

实现目标:将log4net的相关操作封装成一个 .net standard类库
demo地址:https://github.com/puzzledalien/log4net_demo/tree/master/dotnetcoreconsole_v2

step 1 建立解决方案和处理项目相关配置

在当前解决方案下新建项目,选择 .net standard类库,命名“mylogmanager”
.Net Core 实践 - 使用log4net记录日志(2)

nuget引用log4net,添加log4net.config配置,右键点击log4net.config,选择“属性”,修改log4net.config文件属性
刚添加的log4net.config文件属性可能如下图所示(生成操作为无,不复制到输出目录)
.Net Core 实践 - 使用log4net记录日志(2)

需要修改成如下所示(生成操作 无 改为 内容复制到输出目录 - 选择 如果较新则复制
.Net Core 实践 - 使用log4net记录日志(2)

.Net Core 实践 - 使用log4net记录日志(2)

重新生成解决方案后,在生成目录中就能看到log4net.config文件

step 2 上代码

思路:两个startlogger方法,在程序启动时启动该方法,一个方法自己配置处理,一个走默认
代码如下

    public class mylogmanager
    {
        private static iloggerrepository _loggerrepository;

        /// <summary>
        /// 启动logger
        /// </summary>
        /// <param name="repository">repository名称</param>
        /// <param name="filename">配置文件名称</param>
        public static void startlogger(string repository, string filename)
        {
            _loggerrepository = logmanager.createrepository(repository);
            xmlconfigurator.configureandwatch(_loggerrepository, new fileinfo(filename));
        }

        /// <summary>
        /// 启动logger
        /// </summary>
        public static void startlogger()
        {
            _loggerrepository = logmanager.createrepository(nameof(mylogmanager));
            xmlconfigurator.configureandwatch(_loggerrepository, new fileinfo("log4net.config"));
        }

        public static ilog getmylog<t>(t t)
        {
            return logmanager.getlogger(_loggerrepository.name, t.gettype());
        }

        public static ilog getmylog(object obj)
        {
            return logmanager.getlogger(_loggerrepository.name, obj.gettype());
        }

        public static ilog getmylog(type type)
        {
            return logmanager.getlogger(_loggerrepository.name, type);
        }
    }

封装之后,调用代码会比第一版本的稍微精简

        static void testv1()
        {
            mylogmanager.mylogmanager.startlogger("dotnetcoreconsole_v2", "log4net.config");

            var log = mylogmanager.mylogmanager.getmylog(typeof(program));

            const int numberofcycles = 20000;

            var sw = stopwatch.startnew();
            for (var i = 0; i < numberofcycles; i++)
            {
                log.infoformat("testnum: {0} ", i);
            }
            sw.stop();

            console.writeline();
            console.writeline("ellapsed: {0}, numpersec: {1}", sw.elapsedmilliseconds, numberofcycles / (sw.elapsedmilliseconds / (double)1000));
            console.readkey();
        }

代码重构和优化

思考:可不可以去掉startlogger这行代码呢?可以。
实现如下

    public class nulllogmanager
    {
        private static iloggerrepository _loggerrepository;

        private static iloggerrepository loggerrepository
        {
            get
            {
                if (_loggerrepository != null)
                {
                    return _loggerrepository;
                }
                _loggerrepository = logmanager.createrepository(nameof(nulllogmanager));
                xmlconfigurator.configureandwatch(_loggerrepository, new fileinfo("log4net.config"));
                return _loggerrepository;
            }
        }

        public static ilog getmylog<t>(t t)
        {
            return logmanager.getlogger(loggerrepository.name, t.gettype());
        }

        public static ilog getmylog(object obj)
        {
            return logmanager.getlogger(loggerrepository.name, obj.gettype());
        }

        public static ilog getmylog(type type)
        {
            return logmanager.getlogger(loggerrepository.name, type);
        }

        public static ilog getmylog()
        {
            return logmanager.getlogger(loggerrepository.name, nameof(getmylog));
        }
    }

调用代码可再次精简

        static void testnulllogmanager()
        {
            var log = mylogmanager.nulllogmanager.getmylog(typeof(program));

            const int numberofcycles = 20000;

            var sw = stopwatch.startnew();
            for (var i = 0; i < numberofcycles; i++)
            {
                log.infoformat("testnum: {0} ", i);
            }
            sw.stop();

            console.writeline();
            console.writeline("ellapsed: {0}, numpersec: {1}", sw.elapsedmilliseconds, numberofcycles / (sw.elapsedmilliseconds / (double)1000));
            console.readkey();
        }