1 Star 7 Fork 1

zyknow / 博客项目

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
AFL-3.0

asp.net core 中使用Nlog写入txt 数据库(SqlServer MySql)

编辑于 2020/03/12

背景

熟话说得好,没有日志的监控的项目,上线那是不可能的,足以见得日志监控在整个项目中有它独特的地位,本来想使用Log4做日志监控,可是发现log4在asp.net core中并不支持写入数据库,如果想要写入数据库的话就得自己写,当然我还是有点自知之明的,想了想还是用第三方框架,所以这篇文章是基于Nlog + Sqlserver

NLog简介

NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码。 NLog是一个简单灵活的.NET日志记录类库。通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的(contextual information)调试诊断信息,根据喜好配置其表现样式之后发送到一个或多个输出目标(target)中。 NLog的API非常类似于log4net,且配置方式非常简单。NLog使用路由表(routing table)进行配置,这样就让NLog的配置文件非常容易阅读,并便于今后维护。 NLog支持输出的目标

  • 文件 比如TXT、Excel
  • 文本控制台
  • Email
  • 数据库
  • 网络中的其它计算机(通过TCP或UDP)
  • 基于MSMQ的消息队列
  • Windows系统日志

Nlog官网 最新版本:4.6.8 (2020-03-12)

完成功能

  1. 支持写入到数据库、txt
  2. appsettings.json数据库连接与appsettings.json保持同步并且以为appsettings.json准,保持同步。
  3. 写可自定义字段写日志
  4. 静态方法调用写日志

一. SqlServer

1. 创建数据库

use master

go
if exists (select * from sysdatabases where name='NlogTestDB')
  drop database NlogTestDB  --检查有没有这个数据库,如果有就删除它。
go

create database NlogTestDB
on
(
name=LogDB_data,  ------------ 养成好习惯,数据文件加_data
filename='C:\sqlserverDB\NlogTestDB_data.mdf',  ------------ 一定要是.mdf的文件,代表主数据文件
size=10mb,
maxsize=100mb,
filegrowth=10%
)
log on
(
name=LogDB_log,   ------------ 养成好习惯,日志文件加_log
filename='C:\sqlserverDB\NlogTestDB_log.ldf', ------------ 一定要是.ldf的文件,代表日志文件
size=10mb,
maxsize=100mb,
filegrowth=10%
)

2. 创建表

use NlogTestDB
--drop table logs
create table logs
(
  Id int IDENTITY(1,1) PRIMARY key,
  LogDate datetime not null,
  LogLevel VARCHAR(50) not null,
  LogType VARCHAR(50) DEFAULT null,
  Message VARCHAR(5000) not null,
  MachineName VARCHAR(50) not null,
  MachineIp VARCHAR(50) not null,
  RequestController VARCHAR(50) not null,
  RequestAction VARCHAR(50) not null,
  RequestMethod VARCHAR(100) not null,
  RequestHeaders VARCHAR(500) not null,
  RequestPostBody VARCHAR(2000) not null,
  RequestQuery VARCHAR(100) not null,
  RequestUrl VARCHAR(50) not null,
  UserName VARCHAR(50) not null,
  UserGuid VARCHAR(50) not null,
  EXCEPTION VARCHAR(5000) not null,
)

这样数据库就创建成功了

二. ASP. Net Core

1. 新建ASP. Net Core项目

创建. net core项目,这里就不过多演示,本项目是基于最新的Core 3.1 WebAPI

二. 安装Nuget包

Nlog包 也可以通过双击web项目进行添加还原包

<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />

除此之外,还得装操作数据库的包,博主是基于SqlServer的。 MySql Mysql SqlServer SqlServer

3. 添加配置文件

添加配置文件放置到web项目中的根目录即可 Nlog.config

<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="NlogRecords.log">
  <!--Nlog内部日志记录为Off关闭。除非纠错,不可以设为Trace否则速度很慢,起码Debug以上-->
  <extensions>
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>
  <targets>
    <!--通过数据库记录日志 配置
    dbProvider请选择mysql或是sqlserver,同时注意连接字符串,需要安装对应的sql数据提供程序
    MYSQL:
    dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"
    connectionString="server=localhost;database=BaseMIS;user=root;password=123456"
    MSSQL:
    dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient"
    connectionString="Server=127.0.0.1;Database=BaseMIS;User ID=sa;Password=123456"
    -->
    <target name="log_database" xsi:type="Database" dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient"
    connectionString="Server=127.0.0.1;Database=BaseMIS;User ID=sa;Password=123456"
      <commandText>
        INSERT INTO logs
        (LogDate,LogLevel,LogType,Message,MachineName,MachineIp,RequestController,RequestAction,RequestMethod,RequestHeaders
        ,RequestPostBody,RequestQuery,RequestUrl,UserName,UserGuid,Exception)
        VALUES
        (@LogDate,@LogLevel,@LogType,@Message,@MachineName,@MachineIp,@RequestController,@RequestAction,@RequestMethod
        ,@RequestHeaders,@RequestPostBody,@RequestQuery,@RequestUrl,@UserName,@UserGuid,@Exception);
      </commandText>
      <parameter name="@LogDate" layout="${date}" />
      <parameter name="@LogLevel" layout="${level}" />
      <parameter name="@LogType" layout="${event-properties:item=LogType}" />
      <parameter name="@Message" layout="${message}" />
      <parameter name="@MachineName" layout="${machinename}" />
      <parameter name="@MachineIp" layout="${aspnet-request-ip}" />

      <parameter name="@RequestController" layout="${aspnet-mvc-controller}" />
      <parameter name="@RequestAction" layout="${aspnet-mvc-action}" />
      <parameter name="@RequestMethod" layout="${aspnet-request-method}" />
      <parameter name="@RequestHeaders" layout="${aspnet-request-headers}" />
      <parameter name="@RequestPostBody" layout="${aspnet-request-posted-body}" />
      <parameter name="@RequestQuery" layout="${aspnet-request-querystring}" />

      <parameter name="@RequestUrl" layout="${aspnet-request-url}" />
      <parameter name="@UserName" layout="${event-properties:item=UserName}" />
      <parameter name="@UserGuid" layout="${event-properties:item=UserGuid}" />
      <parameter name="@Exception" layout="${exception:tostring}" />
    </target>
    <target name="log_file" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} | ${level:uppercase=false} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
  </targets>
  <rules>
    <!--跳过所有级别的Microsoft组件的日志记录-->
    <logger name="Microsoft.*" final="true" />
    <!-- BlackHole without writeTo -->
    <!--只通过数据库记录日志,如果给了name名字,cs里用日志记录的时候,取logger需要把name当做参数-->
    <logger name="logdb" writeTo="log_database" />
    <logger name="logfile" writeTo="log_file" />
  </rules>
</nlog>

解读配置文件

  • nlog根节点:

    1. autoReload属性,true时,如果NLog.config文件有变动,会自动应用新配置(但是会有延迟,过几秒才会应用起来)
    2. internalLogLevel属性,设定后,输出的是NLog内部自己的日志记录,如果遇到NLog异常/配置文件没配好,可以把Off改为Trace或Debug来查看NlogRecords.log里的内容
    3. internalLogFile属性,可以设定路径,例如默认的D:\Logs\aspNet.log
  • extensions节点,引用了NLog.Web.AspNetCore

  • targets是比较重要的节点,里面是整个连接数据的配置

  • 第一个target节点,可以看到name是log_database,这里的name和下方logger中writeTo属性对应

    1. xsi:type="Database" 写入的是数据库,填File就是txt
    2. dbProvider属性是数据库适配器 SQL server是 Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient MySQL是 MySql.Data.MySqlClient.MySqlConnection, MySql.Data 其他数据库适配器在Nlog官网gitHub查看
    3. connectionString:数据库连接字符串
    4. commandText:插入到数据库的脚本
    5. parameter数据库脚本的参数,统一就行了
    • name="@LogType"是参数,layout="${event-properties:item=LogType}表示@LogType参数的值从event-properties中的LogType中取,详见后文
    • 其余参数均是NLog自带的内容,更多的使用方法查看layout render官方文档
  • 第二个target节点是写入txt文件的节点

    1. xsi:type="File":写入txt
    2. fileName是写入文件的文件名并按日期添加后缀
    3. layout属性是写入日志的格式
  • rules节点是各个日志记录器logger的配置

    1. 第一个logger配置跳过所有Microsoft日志记录,final 属性是否为最后一个规则,如果为true,其后的规则即便被匹配也不会被运行。
    2. 第二个logger name="logdb",该日志记录器名为logdb,是适配log_database规则,即写入数据库,如果要适配多条规则,用逗号隔开

Nlog文件位置 在这里务必要选中始终复制 Nlog文件属性

4. 新建 .Net core 类库项目命名为Utils

创建文件夹Nlog和文件NlogUtil.cs Utils

NlogUtil.cs

using NLog;
using NLog.Config;
using System;
using System.ComponentModel;
using System.Linq;
using System.Xml.Linq;

namespace Utils.Nlog
{
    public enum LogType
    {
        [Description("网站")]
        Web,
        [Description("数据库")]
        DataBase,
        [Description("Api接口")]
        ApiRequest,
        [Description("中间件")]
        Middleware
    }
    public static class NLogUtil
    {
        public static Logger MSdbLogger = LogManager.GetLogger("logdb");
        public static Logger fileLogger = LogManager.GetLogger("logfile");
        /// <summary>
        /// 写日志到数据库
        /// </summary>
        /// <param name="logLevel">日志等级</param>
        /// <param name="message">信息</param>
        /// <param name="userName">请求用户名</param>
        /// <param name="userGuid">请求用户Guid</param>
        /// <param name="exception">异常</param>
        public static void WriteDBLog(LogLevel logLevel, string message,string userName = null,string userGuid = null, Exception exception = null)
        {
            LogEventInfo theEvent = new LogEventInfo(logLevel, MSdbLogger.Name, message);
            theEvent.Properties["LogType"] = LogType.Web.ToString();
            theEvent.Properties["UserName"] = userName;
            theEvent.Properties["UserGuid"] = userGuid;
            theEvent.Exception = exception;
            MSdbLogger.Log(theEvent);
        }
        /// <summary>
        /// 写日志到文件
        /// </summary>
        /// <param name="logLevel">日志等级</param>
        /// <param name="logType">日志类型</param>
        /// <param name="message">信息</param>
        /// <param name="exception">异常</param>
        public static void WriteFileLog(LogLevel logLevel, string message, Exception exception = null)
        {
            LogEventInfo theEvent = new LogEventInfo(logLevel, fileLogger.Name, message);
            theEvent.Properties["LogType"] = LogType.Web.ToString();
            theEvent.Exception = exception;
            fileLogger.Log(theEvent);
        }

        /// <summary>
        /// 确保NLog配置文件sql连接字符串正确
        /// </summary>
        /// <param name="nlogPath"></param>
        /// <param name="sqlConnectionStr"></param>
        public static void EnsureNlogConfig(string nlogPath, string sqlConnectionStr)
        {
            XDocument xd = XDocument.Load(nlogPath);
            if (xd.Root.Elements().FirstOrDefault(a => a.Name.LocalName == "targets")
                is XElement targetsNode && targetsNode != null &&
                targetsNode.Elements().FirstOrDefault(a => a.Name.LocalName == "target" && a.Attribute("name").Value == "log_database")
                is XElement targetNode && targetNode != null)
            {
                if (!targetNode.Attribute("connectionString").Value.Equals(sqlConnectionStr))//不一致则修改
                {
                    //这里暂时没有考虑dbProvider的变动
                    targetNode.Attribute("connectionString").Value = sqlConnectionStr;
                    xd.Save(nlogPath);
                    //编辑后重新载入配置文件(不依靠NLog自己的autoReload,有延迟)
                    LogManager.Configuration = new XmlLoggingConfiguration(nlogPath);
                }
            }
        }
    }
}

5. 在appsettings.json中配置sql连接字符串

其实只需要关心这个连接就可以了,Nlog的连接会被自动同步为该连接

"ConectionStrings": {
    "MySqlserverConnection": "Data Source=127.0.0.1;Initial Catalog=NlogTestDB;Persist Security Info=True;User ID=sa;Password=123456"
  }

6. 在Program.cs中注册Nlog并添加Utils的引用

public class Program
{
    public static void Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();
        try
        {
            using (IServiceScope scope = host.Services.CreateScope())
            {
                IConfiguration configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
                //获取到appsettings.json中的连接字符串
                string sqlString = configuration.GetSection("ConectionStrings:MySqlserverConnection").Value;
                //确保NLog.config中连接字符串与appsettings.json中同步
                NLogUtil.EnsureNlogConfig("NLog.config", sqlString);
            }
            //throw new Exception("测试异常");//for test

            //其他项目启动时需要做的事情
            //code
            NLogUtil.WriteDBLog(NLog.LogLevel.Info, "网站启动成功");
            host.Run();
        }
        catch (Exception ex)
        {
            //使用nlog写到本地日志文件(万一数据库没创建/连接成功)
            string errorMessage = "网站启动初始化数据异常";
            NLogUtil.WriteFileLog(NLog.LogLevel.Error, errorMessage, new Exception(errorMessage, ex));
            NLogUtil.WriteDBLog(NLog.LogLevel.Info, errorMessage);
            throw;
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)

        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })

        //using NLog.Web;
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
        }).UseNLog();  // NLog: 依赖注入Nlog
}

7. 使用

在core webapi 默认创建的WeatherForecast控制器中get方法使用Nlog记录日志 Nlog使用

在web项目bin目录下的项目路径和数据库都会写入数据 Bin目录下的txt 路径

文件 数据库 Nlog写入数据库

源码地址

下载Nlog分支即可

码云

Academic Free License (“AFL”) v. 3.0 This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: Licensed under the Academic Free License version 3.0 1) Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: a) to reproduce the Original Work in copies, either alone or as part of a collective work; b) to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; c) to distribute or communicate copies of the Original Work and Derivative Works to the public, under any license of your choice that does not contradict the terms and conditions, including Licensor’s reserved rights and remedies, in this Academic Free License; d) to perform the Original Work publicly; and e) to display the Original Work publicly. 2) Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. 3) Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. 4) Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor’s trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. 5) External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). 6) Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. 8) Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. 9) Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including “fair use” or “fair dealing”). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). 10) Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. 12) Attorneys’ Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 13) Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 14) Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 15) Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. 16) Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Academic Free License" or "AFL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under <insert your license name here>" or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process.

简介

博客项目demo 展开 收起
C#
AFL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/zyknow/blog_project.git
git@gitee.com:zyknow/blog_project.git
zyknow
blog_project
博客项目
Nlog

搜索帮助