基于Log4Net记录日志到SQLServer

本文记录通过log4net将日志信息记录到SQLServer数据库中。

1、新建控制台应用程序 Log4NetDemo;

2、通过NuGet安装Log4Net (项目版本2.0.8);

3、项目根目录下添加 log4net.config 配置文件;

<?xml version="1.0"?>
<configuration>
  <!--声明一个名为“log4net”的自定义配置节-->
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <!--log4net配置信息-->
  <log4net>
    <logger name="WebLogger">
      <level value="INFO"/><!--定义在这个级别之上的日志才会被记录-->
      <appender-ref ref="ADONetAppender" /><!--定义日志对象使用的Appender对象-->
    </logger>

    <!--Appenders用来定义日志的输出方式 -->
    <!--name = “AdoNetAppender” sql数据库-->
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
      <!--缓冲区大小为10,缓存10条记录同时写入数据库,避免每次都去请求数据库连接写数据-->
      <bufferSize value="10"/>
      <!-- SQL数据源 ,本地安装SQL客户端-->
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      <!-- SQL连接字符串,修改为自己的-->
      <connectionString value="Data Source=ali.xsd.com;Initial Catalog=test;Integrated Security=false;User ID=admin;Password=123456;" />
      <!-- 数据库插入-->
      <commandText value="INSERT INTO AppOpLog ([ThreadId],[Level],[Message],[Exception],[LogTime],[UserPhone],[IP],[ControllerName],[ActionName],[ActionParam],[Url],[HttpHeader],[HttpMethod],[UserAgent],[StartTime],[EndTime],[RunTime])
                   VALUES ( @thread,@log_level, @message, @exception,@log_date, @UserPhone,@IP,@ControllerName,@ActionName,@ActionParam,@Url,@HttpHeader,@HttpMethod,@UserAgent,@StartTime,@EndTime,@RunTime)"/>
      <!--线程号-->
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="100" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%t" />
        </layout>
      </parameter>
      <!--日志记录时间,RawTimeStampLayout 为默认的时间输出格式-->
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawTimeStampLayout"/>
      </parameter>
      <!--日志等级-->
      <parameter>
        <parameterName value="@log_level"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level"/>
        </layout>
      </parameter>
      <!--异常 ExceptionLayout 默认的异常输出格式-->
      <parameter>
        <parameterName value="@exception"/>
        <dbType value="String"/>
        <size value="2000"/>
        <layout type="log4net.Layout.ExceptionLayout"/>
      </parameter>

      <parameter>
        <parameterName value="@message"/>
        <dbType value="String"/>
        <size value="4000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message"/>
        </layout>
      </parameter>

      <!--自定义成员-->
      <parameter>
        <parameterName value="@UserPhone" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%UserPhone" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@IP" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%IP" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@StartTime" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%StartTime" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@EndTime" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%EndTime" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@RunTime" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%RunTime" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@ControllerName" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%ControllerName" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@ActionName" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%ActionName" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@ActionParam" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%ActionParam" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@Url" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%Url" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@HttpMethod" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%HttpMethod" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@HttpHeader" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%HttpHeader" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@UserAgent" />
        <dbType value="String" />
        <layout type="Log4NetDemo.CustomLayout">
          <conversionPattern value="%UserAgent" />
        </layout>
      </parameter>

    </appender>
  </log4net>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

4、日志记录中添加自定义字段

public class CustomLayout : log4net.Layout.PatternLayout
    {
        public CustomLayout()
        {
            this.AddConverter("UserPhone", typeof(UserPhonePatternConverter));
            this.AddConverter("IP", typeof(IPPatternConverter));
            this.AddConverter("ControllerName", typeof(ControllerNamePatternConverter));
            this.AddConverter("ActionName", typeof(ActionNamePatternConverter));
            this.AddConverter("ActionParam", typeof(ActionParamPatternConverter));
            this.AddConverter("Url", typeof(UrlPatternConverter));
            this.AddConverter("HttpHeader", typeof(HttpHeaderPatternConverter));
            this.AddConverter("HttpMethod", typeof(HttpMethodPatternConverter));
            this.AddConverter("UserAgent", typeof(UserAgentPatternConverter));
            this.AddConverter("StartTime", typeof(StartTimePatternConverter));
            this.AddConverter("EndTime", typeof(EndTimePatternConverter));
            this.AddConverter("RunTime", typeof(RunTimePatternConverter));
        }
    }

    internal sealed class UserPhonePatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.UserPhone);
        }
    }

    internal sealed class IPPatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.IP);
        }
    }

    internal sealed class ControllerNamePatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.ControllerName);
        }
    }

    internal sealed class ActionNamePatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.ActionName);
        }
    }

    internal sealed class ActionParamPatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.ActionParam);
        }
    }

    internal sealed class UrlPatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.Url);
        }
    }

    internal sealed class HttpHeaderPatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.HttpHeader);
        }
    }

    internal sealed class HttpMethodPatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.HttpMethod);
        }
    }

    internal sealed class UserAgentPatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.UserAgent);
        }
    }

    internal sealed class StartTimePatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.StartTime);
        }
    }

    internal sealed class EndTimePatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.EndTime);
        }
    }

    internal sealed class RunTimePatternConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            AppOpLog logMessage = loggingEvent.MessageObject as AppOpLog;

            if (logMessage != null)
                writer.Write(logMessage.RunTime);
        }
    }

5、程序启动时读取配置文件,控制台程序在Main方法中添加

//注册 log4net,注意这里的路径为绝对路径
log4net.Config.XmlConfigurator.Configure(
        new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\log4net.config")
);

6、记录日志,定义LogHelper.cs

    public class LogHelper
    {
        public static string LoggerName = "DbLogger";

        private static ILog log = LogManager.GetLogger(LoggerName);

        /// <summary>
        /// 记录一般日志
        /// </summary>
        public static void LogInfo(AppOpLog opLog)
        {
            if (log.IsInfoEnabled)
            {
                log.Info(opLog);
            }
        }

        /// <summary>
        /// 记录错误
        /// </summary>
        public static void LogError(AppOpLog opLog, Exception ex)
        {
            if (log.IsErrorEnabled)
            {
                log.Error(opLog, ex);
            }
        }

        /// <summary>
        /// 记录严重错误
        /// </summary>
        public static void LogFatal(AppOpLog opLog, Exception ex)
        {
            if (log.IsFatalEnabled)
            {
                log.Fatal(opLog, ex);
            }
        }

        /// <summary>
        /// 记录警告
        /// </summary>
        public static void LogWarn(AppOpLog opLog)
        {
            if (log.IsWarnEnabled)
            {
                log.Warn(opLog);
            }
        }
    }

7、在程序中调用

        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure(
               new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\log4net.config")
           );

            AppOpLog opLog = new AppOpLog();
            opLog.IP = "192.124.0.0";
            opLog.ActionName = "action";
            opLog.ControllerName = "controller";
            opLog.ActionParam = "param";
            opLog.Url = "url";
            opLog.HttpHeader = "header";
            opLog.HttpMethod = "get";
            opLog.UserAgent = "useragent";
            opLog.StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            opLog.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            opLog.RunTime = (Convert.ToDateTime(opLog.EndTime) - Convert.ToDateTime(opLog.StartTime)).TotalSeconds.ToString();

            LogHelper.LogError(opLog,new Exception("error"));//调用

            Console.ReadKey();
        }    

8、结果

注意:控制台程序调试的时候需要将 log4net.config属性设为始终复制;

附sql脚本

CREATE TABLE [dbo].[AppOpLog](
       [IntId] [int] IDENTITY(1,1) NOT NULL,
       [UserCharId] [varchar](50) NULL,
       [UserPhone] [varchar](20) NULL,
       [LogTime] [datetime] NOT NULL,
       [IP] [varchar](20) NOT NULL,
       [ControllerName] [varchar](20) NOT NULL,
       [ActionName] [varchar](20) NOT NULL,
       [ActionParam] [varchar](max) NOT NULL,
       [Url] [varchar](max) NOT NULL,
       [HttpHeader] [varchar](max) NULL,
       [HttpMethod] [varchar](5) NULL,
       [UserAgent] [varchar](100) NULL,
       [StartTime] [datetime] NOT NULL,
       [EndTime] [datetime] NOT NULL,
       [RunTime] [varchar](50) NOT NULL,
       [Level] [varchar](10) NOT NULL,
       [ThreadId] [int] NOT NULL,
       [Message] [varchar](max) NULL,
       [CreateTime] [datetime] NULL
)

AppOpLog.cs

    public class AppOpLog
    {
        public string IP { get; set; }
        public string ControllerName { get; set; }
        public string ActionName { get; set; }
        public string ActionParam { get; set; }
        public string Url { get; set; }
        public string HttpHeader { get; set; }
        public string HttpMethod { get; set; }
        public string UserAgent { get; set; }
        public string StartTime { get; set; }
        public string EndTime { get; set; }
        public string RunTime { get; set; }
    }

log4net调试:当log4net日志插入失败时,在Web.Config配置文件里添加可以查看错误情况,这里注意对应的目录下要有写文件的权限

<appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
  </appSettings>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add
            name="textWriterTraceListener"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="C:\log4net.txt" />
      </listeners>
    </trace>
  </system.diagnostics>

测试Demo地址:https://github.com/zhrong92/Log4NetDemo

原文地址:https://www.cnblogs.com/zhaorong0912/p/9655130.html

时间: 2024-10-15 04:17:34

基于Log4Net记录日志到SQLServer的相关文章

Log4Net记录日志到SQLServer数据库

Log4Net配置步骤: 1.在项目中添加Log4Net引用,可在nuget包工具中下载 2.在项目中添加Log4Net.config文件并配置 <?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,

基于Log4Net本地日志服务简单实现

背景 项目开发中,我们或多或少会使用诸如NLog,Log4Net,Kafka+ELK等等日志套件: 基于关注点分离原则,业务开发的时候不应该关注日志具体实现:并且后续能方便切换其他日志套件: 这里先实现基于文件的日志服务,在下一篇将实现基于Kafka+ELK: 具体源码:MasterChief Nuget:Install-Package MasterChief.DotNet.Core.Log 欢迎Star,欢迎Issues: 日志接口定义 /// <summary> /// 日志记录接口 //

[开源]基于Log4Net简单实现KafkaAppender

背景 基于之前基于Log4Net本地日志服务简单实现 实现本地日志服务,但是随着项目开发演进,本地日志服务满足不了需求,譬如在预发布环境或者生产环境,不可能让开发人员登录查看本地日志文件分析. Kafka+ELK日志服务套件,可以在线日志服务可以解决上述问题,并且提供丰富报表分析等等: 具体源码:MasterChief Nuget:Install-Package MasterChief.DotNet.Core.KafkaLog 欢迎Star,欢迎Issues: 源码 基于Log4Net来实现与k

基于log4net的日志组件扩展分装,实现自动记录交互日志 XYH.Log4Net.Extend

背景: 随着公司的项目不断的完善,功能越来越复杂,服务也越来越多(微服务),公司迫切需要对整个系统的每一个程序的运行情况进行监控,并且能够实现对自动记录不同服务间的程序调用的交互日志,以及通一个服务或者项目中某一次执行情况的跟踪监控 根据log4net的现有功能满足不了实际需求,所以需要以log4net为基础进行分装完善,现在分装出了一个基础的版本,如有不妥之处,多多指点功能简介: 该组件是在log4net的基础上,进行了一定的扩展封装实现的自动记录交互日志功能 该组件的封装的目的是解决一下几个

[转]C#使用Log4Net记录日志

第一步:下载Log4Net 下载地址:http://logging.apache.org/log4net/download_log4net.cgi 把下载的  log4net-1.2.11-bin-newkey解压后,如下图所示: 双击bin文件夹 双击net文件夹,选择针对.NET FramerWork的不同版本 找到相应版本的log4net.dll. 第二步:应用Log4Net 1.在项目中添加Log4Net.DLL的引用 开发环境:XP Professional Service Pack3

C#使用log4net记录日志 (转)

一.下载log4net.dll(百度云里面有)文件,然后再在项目中添加引用. 二.在AssemblyInfo.cs文件中添加log4net.dll的参数. [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)] 三.右击项目→添加→新建项→xxx配置文件(web或者winform……)

C#Log4net记录日志(转)

C#Log4net记录日志 2018年11月25日 21:26:08 天外雁 阅读数:2084 1.下载log4net地址:http://logging.apache.org/log4net/download_log4net.cgi 下载log4net-2.0.8-bin-oldkey.zip到本地,根据自己的需求选择一个合适的log4net.dll版本即可. 2.log4net.dll放在 \bin\Release或者 \bin\Debug下. 3.解决方案下的引用中去引用该dll. 4.添加

C# 使用Log4Net记录日志

最近在"伯乐在线"上看到这么篇文章<用Log4Net来记录系统的日志信息>,由于没有用到过Log4Net这个东东,就有了下面这篇文章: 第一步:下载Log4Net 下载地址:http://logging.apache.org/log4net/download_log4net.cgi 把下载的  log4net-1.2.13-bin-newkey解压后,如下图所示: 双击bin文件夹,如下图: 双击net文件夹,选择针对.NET FramerWork的不同版本 找到相应版本的

Log4Net记录日志(mvc)

转自:http://blog.csdn.net/zhoufoxcn/article/details/2220533 感谢:柄棋先生 第一步:下载Log4Net 下载地址:http://logging.apache.org/log4net/download_log4net.cgi 把下载的  log4net-1.2.11-bin-newkey解压后,如下图所示: 双击bin文件夹 双击net文件夹,选择针对.NET FramerWork的不同版本 找到相应版本的log4net.dll 第二步:应用