[.Net Core] - 使用 NLog 记录日志到 Sql Server
1. 使用 Nuget 安装 NLog。

2. 在 Sql Server 中创建 NLog 数据表。
CREATE TABLE [dbo].[NLogInfo](
[LogId] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NOT NULL,
[Origin] [nvarchar](100) NULL,
[Level] [nvarchar](50) NULL,
[Message] [nvarchar](max) NULL,
[Detail] [nvarchar](max) NULL,
) ON [PRIMARY]
3. 创建并配置 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="Error"
internalLogFile="Nlog.log">
<variable name="detailTemplate" value="
${newline}date: ${date}
${newline}level: ${level}
${newline}logger: ${logger}
${newline}machinename: ${machinename}
${newline}message: ${message}
${newline}appdomain: ${appdomain}
${newline}assembly-version: ${assembly-version}
${newline}basedir: ${basedir}
${newline}callsite: ${callsite}
${newline}callsite-linenumber: ${callsite-linenumber}
${newline}counter: ${counter}
${newline}nlogdir: ${nlogdir}
${newline}processid: ${processid}
${newline}processname: ${processname}
${newline}specialfolder: ${specialfolder}
${newline}stacktrace: ${stacktrace}
${newline}exception: ${exception:format=tostring}" />
<targets>
<target name="blackhole" xsi:type="Null" />
<target name="database" xsi:type="Database">
<connectionString>${var:connectionString}</connectionString>
<commandText>
insert into NLogInfo([Date],[origin],[Level],[Message],[Detail]) values (getdate(), @origin, @logLevel, @message,@detail);
</commandText>
<parameter name="@origin" layout="${callsite}" />
<parameter name="@logLevel" layout="${level}" />
<parameter name="@message" layout="${message}" />
<parameter name="@detail" layout="${detailTemplate}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Warn" writeTo="database" />
</rules>
</nlog>
4. 在 Startup 中配置 NLog
env.ConfigureNLog("nlog.config");
LogManager.Configuration.Variables["connectionString"] = Configuration.GetConnectionString("DefaultConnection");
loggerFactory.AddNLog();

5. 测试:手工记 Log + 全局异常捕获。

参考资料
https://blog.csdn.net/u013667895/article/details/79067828
https://www.cnblogs.com/chen8854/p/6800158.html
https://stackoverflow.com/questions/5346336/nlog-configuration-across-appdomains
[.Net Core] - 使用 NLog 记录日志到 Sql Server的更多相关文章
- ASP.NET Core使用NLog记录日志到Microsoft Sql Server
在之前的文章中介绍了如何在ASP.NET Core使用NLog,本文为您介绍在ASP.NET Core使用NLog记录到Microsoft Sql Server 1.我们需要添加依赖: NLog.We ...
- .Net Core 使用NLog记录日志到文件和数据库
NLog 记录日志是微软官方推荐使用. 接下来,通过配置日志记录到文件和Sql Server数据库. 第一步:首先添加包NLog.Config (可通过微软添加包命令Install-Package 包 ...
- 【netcore入坑记】 .Net core UseRowNumberForPaging 分页报错 SQL Server 2008 R2 EntityFrameworkCore
异常环境: netcore版本:.Net Core 2.1 efcore版本:Microsoft.EntityFrameworkCore.SqlServer 2.1.1 sql sqlserver 版 ...
- TransactionScope事务处理方法介绍及.NET Core中的注意事项 SQL Server数据库漏洞评估了解一下 预热ASP.NET MVC 的VIEW [AUTOMAPPER]反射自动注册AUTOMAPPER PROFILE
TransactionScope事务处理方法介绍及.NET Core中的注意事项 作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/10170712.ht ...
- .net core连接Liunx上MS SQL Server
场景 由于业务要求,需要对甲方的一个在SQL Server上的财务表进行插入操作.研究了半天,因为一个小问题折腾了很久. 过程 .net core端: 1. 利用EF,就需要的导入相关的Nuget包, ...
- asp.net core 2.1 增加Nlog日志到sql server数据库
一.增加引用 <PackageReference Include="NLog.Extensions.Logging" Version="1.2.1" /& ...
- asp.net core 使用NLog记录日志到txt文件
一.使用VisualStudioCode创建一个webapi项目(也可以是mvc等).一个类库(用于封装记录日志方法,当然如果使用依赖注入到控制台项目,就不需要此类库了). 二.在类库中添加NLog. ...
- .Net Core使用NLog记录日志
参见:https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2 大致步骤: Nuget中引用NLog及NLog ...
- ASP.NET Core使用NLog记录日志
1.根目录新建nlog.config配置文件 <?xml version="1.0"?> <nlog xmlns="http://www.nlog-pr ...
随机推荐
- UOJ#468. 【ZJOI2019】Minimax搜索 动态DP
原文链接www.cnblogs.com/zhouzhendong/p/UOJ468.html 前言 毒瘤题 题解 首先,将问题稍加转化,将"等于k"转化为"小于等于k&q ...
- bytearray 字符串转为字节
>>> str="hello world">>> x=bytearray(str)>>> xbytearray(b'hello ...
- kubernetes 1.14安装部署ingress
简单介绍: Ingress是Kubernetes API的标准资源类型之一,它其实就是一组基于DNS名称或URL路径把请求转发至指定的Service资源的规则,用来将集群外部的请求流量转发至集群内部. ...
- 分享7个shell脚本实例--shell脚本练习必备
概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...
- 数据结构Java版之邻接矩阵实现图(十一)
邻接矩阵实现图,是用一个矩阵,把矩阵下标作为一个顶点,如果顶点与顶点之间有边.那么在矩阵对应的点上把值设为 1 .(默认是0) package mygraph; import java.util.Li ...
- Redis 3.2.100 配置注意
新装服务器外网连接报错 /Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstandin ...
- 五笔字典86版wubi拆字图编码查询
五笔字典86版 软件能查询以下数据,五笔编码,汉字拆字图,拼音,部首,笔划,笔顺,解释,五笔口诀等等.这些数据只针对单个汉字查询(大概7000字左右).词组查询只支持五笔编码查询(有60000个词组+ ...
- Idea2019.2破解
1.在 hosts 文件里加入如下的配置:C:\WINDOWS\system32\drivers\etc\hosts 0.0.0.0 https://account.jetbrains.com:443 ...
- tansition-group 使用方法
<transition-group name="breadcrumb"> <el-breadcrumb-item v-for="(item,index) ...
- pip安装各种模块
date: 2019-08-20 19:27:09 pip install requests pip install xpinyin pip install redis