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的更多相关文章

  1. ASP.NET Core使用NLog记录日志到Microsoft Sql Server

    在之前的文章中介绍了如何在ASP.NET Core使用NLog,本文为您介绍在ASP.NET Core使用NLog记录到Microsoft Sql Server 1.我们需要添加依赖: NLog.We ...

  2. .Net Core 使用NLog记录日志到文件和数据库

    NLog 记录日志是微软官方推荐使用. 接下来,通过配置日志记录到文件和Sql Server数据库. 第一步:首先添加包NLog.Config (可通过微软添加包命令Install-Package 包 ...

  3. 【netcore入坑记】 .Net core UseRowNumberForPaging 分页报错 SQL Server 2008 R2 EntityFrameworkCore

    异常环境: netcore版本:.Net Core 2.1 efcore版本:Microsoft.EntityFrameworkCore.SqlServer 2.1.1 sql sqlserver 版 ...

  4. TransactionScope事务处理方法介绍及.NET Core中的注意事项 SQL Server数据库漏洞评估了解一下 预热ASP.NET MVC 的VIEW [AUTOMAPPER]反射自动注册AUTOMAPPER PROFILE

    TransactionScope事务处理方法介绍及.NET Core中的注意事项   作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/10170712.ht ...

  5. .net core连接Liunx上MS SQL Server

    场景 由于业务要求,需要对甲方的一个在SQL Server上的财务表进行插入操作.研究了半天,因为一个小问题折腾了很久. 过程 .net core端: 1. 利用EF,就需要的导入相关的Nuget包, ...

  6. asp.net core 2.1 增加Nlog日志到sql server数据库

    一.增加引用 <PackageReference Include="NLog.Extensions.Logging" Version="1.2.1" /& ...

  7. asp.net core 使用NLog记录日志到txt文件

    一.使用VisualStudioCode创建一个webapi项目(也可以是mvc等).一个类库(用于封装记录日志方法,当然如果使用依赖注入到控制台项目,就不需要此类库了). 二.在类库中添加NLog. ...

  8. .Net Core使用NLog记录日志

    参见:https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2 大致步骤: Nuget中引用NLog及NLog ...

  9. ASP.NET Core使用NLog记录日志

    1.根目录新建nlog.config配置文件 <?xml version="1.0"?> <nlog xmlns="http://www.nlog-pr ...

随机推荐

  1. ShellExecute打开文件,打开文件夹的用法

    转自https://www.cnblogs.com/nxopen2018/p/11070031.html //方法1 //转换 //char msg[256]; //sprintf_s(msg, &q ...

  2. 第十四周助教工作总结——NWNU李泓毅

    助教博客链接:https://www.cnblogs.com/NWNU-LHY/ 本次作业的要求:团队项目需求改进与系统设计:https://www.cnblogs.com/nwnu-daizh/p/ ...

  3. JPA 原生SQ查询

    参考文章 https://blog.csdn.net/coding_1994/article/details/84575943 https://blog.csdn.net/m0_37776094/ar ...

  4. 小福bbs-冲刺日志(第一天)

    [小福bbs-冲刺日志(第一天)] 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 小福bbs 这个作业的目标 开会,安排具体工作 作业的正文 小福bbs-冲刺日志(第一 ...

  5. google chrome 浏览器插件

    如果感觉浏览器(chrome)的背景是白色太亮太刺眼,可以先在 设置->外观->主题背景 里选择 oceanic,将浏览器头部颜色设置为海蓝色.然后再安装插件 “眼睛护航”,改变所有网页的 ...

  6. JVM 修改类加载器启动类加载器

    1.类加载器加载路径 public class MyTest18 { public static void main(String[] args) { //系统类加载器加载路径 System.out. ...

  7. pip安装报错Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-e_k8hq6a/pynacl/

    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-e_k8hq6a/pyn ...

  8. PEP 476 -- Enabling certificate verification by default for stdlib http clients

    SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate ch ...

  9. docker 安装redis 并配置外网可以访问

    1, docker 拉去最新版本的redis docker pull redis #后面可以带上tag号, 默认拉取最新版本 2, docker安装redis container 安装之前去定义我们的 ...

  10. [转]Winform打包工具SetupFactory 9 的使用

    写了个WinForm的小程序..以前没打过包..只是直接把Bin里的东西复制出来使用..自己使用是足够.但是发给别人毕竟不太好看(不牛逼)..所以就想着打包.. Vs2012自带的有打包的功能..相信 ...