NLog和Log4net写入文本和SQL Server

Nlog写入sql server

创建表

CREATE TABLE [dbo].[NLog](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [MachineName] [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.NLog] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

引入NuGet包

注意:NLog.Database必须引用,不然无法正常写入数据库 ,这个问题找了一天,网上基本上所有的资料都没说引用这一条。

配置Nlog.config文件

新建“CfgFile”文件夹(网上基本都是这样,也便于分类管理),新建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"
      internalLogLevel="Info"
      internalLogFile="D:\temp\internal-nlog-AspNetCore.txt">

	<!-- enable asp.net core layout renderers -->
	<extensions>
		<add assembly="NLog.Web.AspNetCore"/>
	</extensions>

	<!-- the targets to write to -->
	<targets>
		<!--database-->
		<target name= "allDatabase" xsi:type="Database"
			dbProvider="System.Data.SqlClient.SqlConnection, System.Data.SqlClient"
			connectionString="Data Source=.;Initial Catalog=StudyTag;Persist Security Info=True;User ID=sa;Password=abc123,./">
			<commandText>
				INSERT INTO dbo.NLog ([MachineName], [Logged], [Level], [Message], [Logger], [CallSite],[Exception]) VALUES (@machineName, @logged, @level, @message,@logger, @callSite, @exception);
			</commandText>
			<parameter name="@machineName" layout="${machinename}" />
			<parameter name="@logged" layout="${date}"/>
			<parameter name="@level" layout=" ${level}" />
			<parameter name="@message" layout="${message}"/>
			<parameter name="@logger" layout="${logger}" />
			<parameter name= "@callSite" layout="${callsite:filename=true}" />
			<parameter name="@exception" layout="${exception:tostring}"/>
		</target>
		<!-- File Target for all log messages with basic details -->
		<target xsi:type="File" name="allfile" fileName="NLog\nlog-all-${shortdate}.log"
				layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

		<!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
		<target xsi:type="File" name="ownFile-web" fileName="NLog\nlog-my-${shortdate}.log"
				layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
		<target xsi:type="Null" name="blackhole"/>
	</targets>

	<!-- rules to map from logger name to target -->
	<rules>
		<logger name="*" minlevel="Trace" writeTo="allDatabase" />
		<!--All logs, including from Microsoft-->
		<logger name="*" minlevel="Trace" writeTo="allfile" />
		<!--Output hosting lifetime messages to console target for faster startup detection -->
		<!--<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole,ownFile-web" final="true" />-->
		<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
		<logger name="Microsoft.*" maxlevel="Trace" final="true" />
		<!--<logger name="System.Net.Http.*" maxlevel="Info" final="true" />-->
		<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
	</rules>
</nlog>

 版本号可能不同,所以需要去看官方文档找适合自己项目版本的配置文件(我的是.net core 3.1)

将配置文件属性改为“始终复制”(为了生成的时候把该文件生成到debug文件中)

修改program.cs

在控制器的构造方法中注入ILogger接口

 

结束

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值