ASP.NET Core MVC 项目 NLog

一:说明

NLog是一个帮助程序员将日志信息输出的工具,它定义了多种日志信息输出模式,可以根据需要将日志输出到控制台,文本文件,Excel文件、Windows日志事件查看器中,包括数据库,邮件等等位置,以便我们快速跟踪程序Bug。

一:引入NuGet包:NLog.Web.AspNetCore。

二:导入配置文件:NLog.config

三:配置文件右键属性复制到输出目录始终复制。

四:导入数据库脚本,并引用NLog.Database和System.Data.SqlClient。不使用数据库可忽略。

五:在Program注册NLog,在控制器中使用。

二:配置文件

配置文件实例:




	
	
		
	
	
	
	
		
		

		
		

		
		

		
		
			
			
			
			
			
			
			
		
	

	
		
		
		
		
		
		
		
		
		
		
		
		
		
	

配置文件说明:

日志等级:

        
		 
		
		
		
		
		
		

三:数据库脚本

USE [master]
GO
/* 
  创建数据库LogManager
*/
CREATE DATABASE [LogManager];

GO
ALTER DATABASE [LogManager] SET COMPATIBILITY_LEVEL = 150
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [LogManager].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [LogManager] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [LogManager] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [LogManager] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [LogManager] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [LogManager] SET ARITHABORT OFF 
GO
ALTER DATABASE [LogManager] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [LogManager] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [LogManager] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [LogManager] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [LogManager] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [LogManager] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [LogManager] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [LogManager] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [LogManager] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [LogManager] SET  DISABLE_BROKER 
GO
ALTER DATABASE [LogManager] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [LogManager] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [LogManager] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [LogManager] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [LogManager] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [LogManager] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [LogManager] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [LogManager] SET RECOVERY FULL 
GO
ALTER DATABASE [LogManager] SET  MULTI_USER 
GO
ALTER DATABASE [LogManager] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [LogManager] SET DB_CHAINING OFF 
GO
ALTER DATABASE [LogManager] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [LogManager] SET TARGET_RECOVERY_TIME = 60 SECONDS 
GO
ALTER DATABASE [LogManager] SET DELAYED_DURABILITY = DISABLED 
GO
ALTER DATABASE [LogManager] SET ACCELERATED_DATABASE_RECOVERY = OFF  
GO
EXEC sys.sp_db_vardecimal_storage_format N'LogManager', N'ON'
GO
ALTER DATABASE [LogManager] SET QUERY_STORE = OFF
GO
USE [LogManager]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/* 
  创建NLog的表
*/
CREATE TABLE [dbo].[NLog](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Application] [nvarchar](50) NOT NULL,
	[Logged] [datetime] NOT NULL,
	[Level] [nvarchar](50) NOT NULL,
	[Message] [nvarchar](max) NOT NULL,
	[Logger] [nvarchar](250) NULL,
	[Callsite] [nvarchar](max) NULL,
	[Exception] [nvarchar](max) NULL,
 CONSTRAINT [PK_dbo.Log] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
USE [master]
GO
ALTER DATABASE [LogManager] SET  READ_WRITE 
GO

四:代码实例

Program.cs注册代码:

using NLog.Extensions.Logging;
builder.Logging.AddNLog("CfgFile/NLog.Config");

控制器代码:

using log4net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging.Log4Net.AspNetCore.Extensions;

namespace Study_ASP.NET_Core_MVC.Controllers
{
    public class LogManagerController : Controller
    {
        /// 
        /// 定义构造函数
        /// 
        private readonly ILogger _logger;
        /// 
        /// 初始化构造函数
        /// 
        /// 
        public LogManagerController(ILogger logger)
        {
            _logger = logger;
            _logger.LogDebug("调试信息日志---LogDebug");
            _logger.LogInformation("一般信息日志---LogInformation");
            _logger.LogWarning("警告信息日志---LogWarning");
            _logger.LogError("错误信息日志---LogError");
            _logger.LogCritical("关键信息日志---LogCritical");
            _logger.LogTrace("跟踪信息日志---LogTrace");
        }
        /// 
        /// Get请求
        /// LogManager控制器
        /// Index方法
        /// 
        /// 
        [HttpGet]
        public IActionResult Index()
        {
            _logger.LogDebug("调试信息日志");
            _logger.LogInformation("一般信息日志");
            _logger.LogWarning("警告信息日志");
            _logger.LogError("错误信息日志");
            _logger.LogCritical("关键信息日志");
            _logger.LogTrace("跟踪信息日志");
            return View();
        }
    }
}

视图代码:

这是LogManager控制器下的Index视图。

你可能感兴趣的:(NET,asp.net,mvc,microsoft)