[.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 ...
随机推荐
- webbench 源代码
WebBench源码:https://github.com/EZLippi/WebBench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问 ...
- java输出程序运行时间
做了一个MapReduce的小练习,想测试一下程序运行时间: 代码: long start = System.currentTimeMillis(); /*运行的程序主体*/ long end = S ...
- ubuntu16.04安装opencv3.4.1教程
最近opencv3.4.1发布了,想换个新的试试鲜,于是把配置的过程通过博文的方式记录下来,方便查阅. 本教程原为3.3.0,但经过博主亲测,3.4.0.3.4.1皆适用 1.去官网下载opencv, ...
- JVM常量的含义与反编译助记符详解
1.定义一个常量 public class MyTest2 { public static void main(String[] args) { System.out.println(MyParent ...
- MySQL两地三中心方案初步设计【转】
整体内容会按照如下的方式来进行设计: 首先说下方案的背景,我参考了一些资料(参见附件). 方案背景 随着互联网业务快速发展,多IDC的业务支撑能力和要求也逐步提升,行业内的“两地三中心”方案较为流行. ...
- 关于如何重写Controller和Service技术攻关文档
版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_39784756/articl ...
- 高性能计算 —— 中国金融服务业创新发展的助推剂 & 微软
“高性能计算 —— 中国金融服务业创新发展的助推剂“六大盘点 - 微软 - 博客园https://www.cnblogs.com/stbchina/archive/2011/12/02/HPC-in- ...
- Git 代码撤销、回滚到任意版本(当误提代码到本地或master分支时)
转自https://www.cnblogs.com/lwh-note/p/9639835.html 两种情况(场景) 情况一 代码还只在本地,未push到运程仓库,想把代码还原到上一次com ...
- 002-guava String 操作
一.概述 提供了字符串基本操作 二.使用 2.1.字符串拼接 字符串的拼接采用的是Guava中的Joiner类中的方法 @Test public void testJdkJoin() { List&l ...
- 泡泡一分钟: A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem
张宁 A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem "链接:https ...