本文记录通过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=""/>
<!-- 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="" />
<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=""/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level"/>
</layout>
</parameter>
<!--异常 ExceptionLayout 默认的异常输出格式-->
<parameter>
<parameterName value="@exception"/>
<dbType value="String"/>
<size value=""/>
<layout type="log4net.Layout.ExceptionLayout"/>
</parameter> <parameter>
<parameterName value="@message"/>
<dbType value="String"/>
<size value=""/>
<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(,) NOT NULL,
[UserCharId] [varchar]() NULL,
[UserPhone] [varchar]() NULL,
[LogTime] [datetime] NOT NULL,
[IP] [varchar]() NOT NULL,
[ControllerName] [varchar]() NOT NULL,
[ActionName] [varchar]() NOT NULL,
[ActionParam] [varchar](max) NOT NULL,
[Url] [varchar](max) NOT NULL,
[HttpHeader] [varchar](max) NULL,
[HttpMethod] [varchar]() NULL,
[UserAgent] [varchar]() NULL,
[StartTime] [datetime] NOT NULL,
[EndTime] [datetime] NOT NULL,
[RunTime] [varchar]() NOT NULL,
[Level] [varchar]() NOT NULL,
[ThreadId] [int] NOT NULL,
[Message] [varchar](max) NULL,
[Exception] [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

 
 

基于Log4Net记录日志到SQLServer(自定义字段)的更多相关文章

  1. log4net记录日志到数据库自定义字段

    假设数据库中有如下自定义字段:   1.根据自定义字段定义日志信息对象     public class MessageLog     {           /// <summary> ...

  2. log4net 记录日志到sqlserver

    参考:http://blog.csdn.net/niuyongjie/article/details/5777625 demo

  3. log4net自定义字段写入SqlServer数据库 ASP.net

    首先申明,本示例经过本作者亲自试验通过,可以运行 第一步 编写log4net配置文件 此处为Log.xml,该文件放在与Web.config平级的位置 <?xml version="1 ...

  4. Log4net 使用之 自定义字段

    Log4net 是.Net下一个非常优秀的开源日志记录组件.Log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介. 由于业务需要,计划为日志增加2个字段,除了 ...

  5. Log4net 自定义字段到数据库(二)

    这种方法比第一种方法麻烦些 Log4Net.config <?xml version="1.0" encoding="utf-8" ?> <c ...

  6. Log4net 自定义字段到数据库

    今天要求做个log4net自定义字段到数据库,在网上找了好多例子,都运行不成功.最后找了个国外的,很简单的就解决了. log4net它已经定义的字段有 <commandText value=&q ...

  7. Log4net 自定义字段 写入Oracle 使用ODP.NET Managed驱动

    一.环境说明: 开发工具:vs2010   ,数据库:oracle 11g ,版本:log4net的目前最新版本1.2.13.0    :  Oracle.ManagedDataAccess.dll ...

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

    背景 项目开发中,我们或多或少会使用诸如NLog,Log4Net,Kafka+ELK等等日志套件: 基于关注点分离原则,业务开发的时候不应该关注日志具体实现:并且后续能方便切换其他日志套件: 这里先实 ...

  9. 使用log4net记录日志到数据库(含有自定义属性)

    记录日志是管理系统中对用户行为的一种监控与审核,asp.net中记录日志的方式有很多种,这里我只介绍一下最近用到的log4net,关于他的具体介绍网上有很多,我讲一下他的用法. 第一步:在配置文件中的 ...

随机推荐

  1. 笨办法学习Python3练习代码1-10

    ex1.py print("hello world!",end = " ")#不换行 print("hello again") print( ...

  2. 【oracle】-集合运算:UNION,UNION ALL...

    在Oracle中提供了三种类型的集合操作: 并(UNION).交(INTERSECT).差(MINUS) UNION:求并,重复记录只显示一次. UNION ALL:求并集,显示所有记录信息. INT ...

  3. ios_UITextField右侧小圆叉

    inputTF.clearButtonMode = UITextFieldViewModeWhileEditing; 只有在输入了字符时才出现哦

  4. 糟糕,你写的 BUG 要被存1000年了!

    摘要:代码冰封,祖传千年! 把大象放在冰箱需要几步? 三步!把代码放在北极需要几步?纳尼? GitHub刚刚公布了一组照片,你写的代码(BUG)上周已经被打包运往北极保存. 只要你2月2日以前贡献过的 ...

  5. Surface Pro 6 遇到的一系列问题

    当屏幕很烫的时候,触摸屏会部分失灵,越烫越明显,但是 Surface Pen 仍然可以使用,建议这个时候关机,等它冷静了再开机 不过不排除更新导致的触控失灵(新的更新没有考虑到老的硬件,微软也许之后永 ...

  6. Flask 框架小记

    Flask 框架小记 Flask 实例 创建示例的代码 from flask import Flask # __name__ 是模块名, 用于反射导入模块 app = Flask(__name__, ...

  7. 关于import android.support.v4.app.ContextCompat;找不到contextcompat的解决方法

    android迁移到了androidx,那么相关库的import就有问题了,需要转变为androidx的,这里比如 import android.support.v4.app.ContextCompa ...

  8. Java 变量及运算符

    Java概述 Java的发展可以归纳如下的几个阶段: (1)第一阶段(完善期):JDK 1.0 ( 1995年推出)一JDK 1.2 (1998年推出,Java更名为Java 2): (2)第二阶段( ...

  9. 分析现有 WPF / Windows Forms 程序能否顺利迁移到 .NET Core 3.0

    本文转自 https://blog.csdn.net/WPwalter/article/details/82859449 使用 .NET Core 3.0 Desktop API Analyzer 分 ...

  10. 后端排序时去掉element表格排序的null状态

    经常会遇到远程排序,需要去掉null状态的排序,当设置sortable='custom'时,设置sort-orders为['ascending', 'descending']是不生效的.然后查到了一种 ...