博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NET - NET Core中使用Log4net输出日志到数据库中去
阅读量:4050 次
发布时间:2019-05-25

本文共 6108 字,大约阅读时间需要 20 分钟。

转载自: 

1.使用Nuget安装log4net 和 mysql.data

2.设置log4net 的配置文件 log4net.config

image-20210112224320408

image-20210112223527630

image-20210112223957164

  • 可以设置多个仓库进而插入到数据库中不同的表

3.在mysql 中创建对应的数据表

-- ------------------------------ Table structure for loggerbackstage-- ----------------------------DROP TABLE IF EXISTS `loggerbackstage`;CREATE TABLE `loggerbackstage`  (  `id` bigint(0) NOT NULL AUTO_INCREMENT,  `log_datetime` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),  `log_thread` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,  `log_level` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,  `log_logger` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,  `log_message` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,  PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 20706 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;SET FOREIGN_KEY_CHECKS = 1;

4.创建自定义字段的实体类

using System;using System.Collections.Generic;using System.Text;namespace Utility.Log4net{    public class ActionLoggerInfo    {        public string UserName { get; set; }        public string UserIP { get; set; }        public string Message { get; set; }        public ActionLoggerInfo(string username, string userip,string message)        {            this.UserName = username;            this.UserIP = userip;            this.Message = message;        }    }}

5.创建转换器类ActionConverter

using System;using System.Collections.Generic;using System.Text;using log4net;using log4net.Layout;using log4net.Layout.Pattern;using log4net.Core;namespace Utility.Log4net{    public class ActionConverter: PatternLayoutConverter    {        protected override void Convert(System.IO.TextWriter writer, LoggingEvent loggingEvent)        {            var actionInfo = loggingEvent.MessageObject as ActionLoggerInfo;            if (actionInfo == null)            {                writer.Write("");            }            else            {                switch (this.Option.ToLower())                {                    case "username":                        writer.Write(actionInfo.UserName);                        break;                    case "userip":                        writer.Write(actionInfo.UserIP);                        break;                    case "message":                        writer.Write(actionInfo.Message);                        break;                    default:                        writer.Write("");                        break;                }            }        }    }}

6.继承log4net 的PatternLayout ,创建自定义的属性类

image-20210112225205967

using System;using System.Collections.Generic;using System.Text;using log4net;using log4net.Layout;using log4net.Layout.Pattern;using log4net.Core;namespace Utility.Log4net{    public class ActionLayoutPattern : PatternLayout    {        public ActionLayoutPattern()        {            this.AddConverter("actionInfo", typeof(ActionConverter));        }    }}

7.创建日志帮助类loghelper

using log4net;using log4net.Config;using System;using System.Collections.Generic;using System.IO;using System.Text;namespace Utility.Log4net{    public class LogHelper    {        public static readonly LogHelper Instance = new LogHelper();        private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");        //public static log4net.ILog loginfo = LogManager.GetLogger("loginfo");        public LogHelper() {            string text = "";            text = System.AppDomain.CurrentDomain.BaseDirectory + "config\\Log4Net.config";            XmlConfigurator.ConfigureAndWatch(new FileInfo(text));        }        private static ActionLoggerInfo _message = null;        private static log4net.ILog _log;        public static log4net.ILog Log        {            get            {                if (_log == null)                {                    _log = LogManager.GetLogger("OperateLogger");                }                return _log;            }        }        public static void Debug()        {            if (Log.IsDebugEnabled)            {                Log.Debug(_message);            }        }        public static void Error()        {            if (Log.IsErrorEnabled)            {                Log.Error(_message);            }        }        public static void Fatal()        {            if (Log.IsFatalEnabled)            {                Log.Fatal(_message);            }        }        public static void Info()        {            if (loginfo.IsInfoEnabled)            {                loginfo.Info(_message);            }        }        public static void Warn()        {            try            {                if (Log.IsWarnEnabled)                {                    Log.Warn(_message);                }            } catch (Exception e)            {                var t = e;            }        }        public static void SaveMessage(string username, string userip, string message, int level)        {            _message = new ActionLoggerInfo(username, userip, message);            switch (level)            {                case 1: Info(); break;                case 2: Warn(); break;                case 3: Error(); break;                case 4: Fatal(); break;                default: break;            }        }    }}

8.使用

image-20210112230153338

LogHelper.SaveMessage("re","re","re",1);

 

参考博客:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的文章
qt 创建异形窗体
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
wpa_supplicant控制脚本
查看>>
gstreamer相关工具集合
查看>>
RS232 四入四出模块控制代码
查看>>
linux 驱动开发 头文件
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>
带WiringPi库的交叉编译如何处理一
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Spring事务的七种传播行为
查看>>