本文共 6108 字,大约阅读时间需要 20 分钟。
-- ------------------------------ 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;
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; } }}
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; } } } }}
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)); } }}
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; } } }}
LogHelper.SaveMessage("re","re","re",1);
参考博客: